taosdemoTestTblAlt.py 4.8 KB
Newer Older
P
Ping Xiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
import os
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *
import threading
import time


class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)

29
        self.numberOfTables = 8
P
Ping Xiao 已提交
30 31
        self.numberOfRecords = 1000000

32
    def getBuildPath(self):
33
        buildPath=""
34 35 36 37 38 39 40 41
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
42
            if ("taosd" in files and "taosBenchmark" in files):
43 44 45 46 47 48
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath

P
Ping Xiao 已提交
49
    def insertDataAndAlterTable(self, threadID):
50 51
        buildPath = self.getBuildPath()
        if (buildPath == ""):
52
            tdLog.exit("taosd or staosBenchmark not found!")
53 54 55 56
        else:
            tdLog.info("taosd found in %s" % buildPath)
        binPath = buildPath + "/build/bin/"

P
Ping Xiao 已提交
57
        if(threadID == 0):
58
            print("%staosBenchmark -y -t %d -n %d -b INT,INT,INT,INT" %
59
                      (binPath, self.numberOfTables, self.numberOfRecords))
60
            os.system("%staosBenchmark -y -t %d -n %d -b INT,INT,INT,INT" %
61
                      (binPath, self.numberOfTables, self.numberOfRecords))
P
Ping Xiao 已提交
62
        if(threadID == 1):
P
Ping Xiao 已提交
63
            time.sleep(2)
P
Ping Xiao 已提交
64
            print("use test")
65 66 67
            max_try = 100
            count = 0
            while (count < max_try):
68 69 70 71 72
                try:
                    tdSql.execute("use test")
                    break
                except Exception as e:
                    tdLog.info("use database test failed")
73 74 75
                    time.sleep(2)
                    count += 1
                    print("try %d times" % count)
76 77
                    continue

P
Ping Xiao 已提交
78
            # check if all the tables have heen created
79 80
            count = 0
            while (count < max_try):
81 82 83 84
                try:
                    tdSql.query("show tables")
                except Exception as e:
                    tdLog.info("show tables test failed")
85 86 87
                    time.sleep(2)
                    count += 1
                    print("try %d times" % count)
88 89
                    continue

P
Ping Xiao 已提交
90
                rows = tdSql.queryRows
P
Ping Xiao 已提交
91 92 93 94 95
                print("number of tables: %d" % rows)
                if(rows == self.numberOfTables):
                    break
                time.sleep(1)
            # check if there are any records in the last created table
96 97
            count = 0
            while (count < max_try):
P
Ping Xiao 已提交
98
                print("query started")
99
                print("try %d times" % count)
100
                try:
101
                    tdSql.query("select * from test.d7")
102 103 104
                except Exception as e:
                    tdLog.info("select * test failed")
                    time.sleep(2)
105 106
                    count += 1
                    print("try %d times" % count)
107 108
                    continue

P
Ping Xiao 已提交
109
                rows = tdSql.queryRows
P
Ping Xiao 已提交
110
                print("number of records: %d" % rows)
P
Ping Xiao 已提交
111 112
                if(rows > 0):
                    break
113
                time.sleep(1)
114

115 116
            print("alter table test.meters add column c10 int")
            tdSql.execute("alter table test.meters add column c10 int")
117 118
            print("insert into test.d7 values (now, 1, 2, 3, 4, 0)")
            tdSql.execute("insert into test.d7 values (now, 1, 2, 3, 4, 0)")
P
Ping Xiao 已提交
119

120
    def run(self):
P
Ping Xiao 已提交
121 122 123 124 125
        tdSql.prepare()

        t1 = threading.Thread(target=self.insertDataAndAlterTable, args=(0, ))
        t2 = threading.Thread(target=self.insertDataAndAlterTable, args=(1, ))

126
        t1.start()
P
Ping Xiao 已提交
127 128 129 130
        t2.start()
        t1.join()
        t2.join()

131 132
        time.sleep(3)

P
Ping Xiao 已提交
133 134 135 136 137 138 139 140 141
        tdSql.query("select count(*) from test.meters")
        tdSql.checkData(0, 0, self.numberOfRecords * self.numberOfTables + 1)

    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
142
tdCases.addLinux(__file__, TDTestCase())