random-test.py 6.8 KB
Newer Older
S
Shuduo Sang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
###################################################################
#           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 random
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *


class Test:
    def __init__(self):
        self.last_tb = ""
S
Shuduo Sang 已提交
25
        self.last_stb = ""
S
Shuduo Sang 已提交
26 27 28
        self.written = 0

    def create_table(self):
S
Shuduo Sang 已提交
29 30
        tdLog.info("create_table")
        current_tb = "tb%d" % int(round(time.time() * 1000))
S
Shuduo Sang 已提交
31

S
Shuduo Sang 已提交
32
        if (current_tb == self.last_tb):
S
Shuduo Sang 已提交
33 34
            return
        else:
S
Shuduo Sang 已提交
35
            tdLog.info("will create table %s" % current_tb)
S
Shuduo Sang 已提交
36
            tdSql.execute(
S
Shuduo Sang 已提交
37 38 39
                'create table %s (ts timestamp, c1 int, c2 nchar(10))' %
                current_tb)
            self.last_tb = current_tb
S
Shuduo Sang 已提交
40 41 42
            self.written = 0

    def insert_data(self):
S
Shuduo Sang 已提交
43 44
        tdLog.info("insert_data")
        if (self.last_tb == ""):
S
Shuduo Sang 已提交
45 46 47
            tdLog.info("no table, create first")
            self.create_table()

S
Shuduo Sang 已提交
48
        tdLog.info("will insert data to table")
S
Shuduo Sang 已提交
49 50 51 52
        insertRows = 10
        tdLog.info("insert %d rows to %s" % (insertRows, self.last_tb))
        for i in range(0, insertRows):
            ret = tdSql.execute(
S
Shuduo Sang 已提交
53 54
                'insert into %s values (now + %dm, %d, "%s")' %
                (self.last_tb, i, i, "->" + str(i)))
S
Shuduo Sang 已提交
55 56 57
            self.written = self.written + 1

        tdLog.info("insert earlier data")
S
Shuduo Sang 已提交
58 59 60
        tdSql.execute(
            'insert into %s values (now - 5m , 10, " - 5m")' %
            self.last_tb)
S
Shuduo Sang 已提交
61
        self.written = self.written + 1
S
Shuduo Sang 已提交
62 63 64
        tdSql.execute(
            'insert into %s values (now - 6m , 10, " - 6m")' %
            self.last_tb)
S
Shuduo Sang 已提交
65
        self.written = self.written + 1
S
Shuduo Sang 已提交
66 67 68
        tdSql.execute(
            'insert into %s values (now - 7m , 10, " - 7m")' %
            self.last_tb)
S
Shuduo Sang 已提交
69
        self.written = self.written + 1
S
Shuduo Sang 已提交
70 71 72
        tdSql.execute(
            'insert into %s values (now - 8m , 10, " - 8m")' %
            self.last_tb)
S
Shuduo Sang 已提交
73 74 75
        self.written = self.written + 1

    def query_data(self):
S
Shuduo Sang 已提交
76
        tdLog.info("query_data")
S
Shuduo Sang 已提交
77 78 79 80 81 82
        if (self.written > 0):
            tdLog.info("query data from table")
            tdSql.query("select * from %s" % self.last_tb)
            tdSql.checkRows(self.written)

    def create_stable(self):
S
Shuduo Sang 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        tdLog.info("create_stable")
        current_stb = "stb%d" % int(round(time.time() * 1000))

        if (current_stb == self.last_stb):
            return
        else:
            tdLog.info("will create stable %s" % current_stb)
            tdSql.execute(
                'create table %s(ts timestamp, c1 int, c2 nchar(10)) tags (t1 int, t2 nchar(10))' %
                current_stb)
            self.last_stb = current_stb

            current_tb = "tb%d" % int(round(time.time() * 1000))
            tdSql.execute(
                "create table %s using %s tags (1, '表1')" %
                (current_tb, self.last_stb))
            self.last_tb = current_tb
            tdSql.execute(
                "insert into %s values (now, 27, '我是nchar字符串')" %
                self.last_tb)
            self.written = self.written + 1

    def drop_stable(self):
        tdLog.info("drop_stable")
        if (self.last_stb == ""):
            tdLog.info("no super table")
            return
        else:
            tdLog.info("will drop last super table")
            tdSql.execute('drop table %s' % self.last_stb)
            self.last_stb = ""
S
Shuduo Sang 已提交
114 115
            self.last_tb = ""
            self.written = 0
S
Shuduo Sang 已提交
116 117 118 119 120 121 122 123 124 125

    def query_data_from_stable(self):
        tdLog.info("query_data_from_stable")
        if (self.last_stb == ""):
            tdLog.info("no super table")
            return
        else:
            tdLog.info("will query data from super table")
            tdSql.execute('select * from %s' % self.last_stb)

S
Shuduo Sang 已提交
126
    def restart_database(self):
S
Shuduo Sang 已提交
127
        tdLog.info("restart_databae")
S
Shuduo Sang 已提交
128 129 130 131
        tdDnodes.stop(1)
        tdDnodes.start(1)
        tdLog.sleep(5)

S
Shuduo Sang 已提交
132 133
    def force_restart_database(self):
        tdLog.info("force_restart_database")
S
Shuduo Sang 已提交
134 135 136
        tdDnodes.forcestop(1)
        tdDnodes.start(1)
        tdLog.sleep(5)
S
Shuduo Sang 已提交
137
        tdSql.prepare()
138 139 140
        self.last_tb = ""
        self.last_stb = ""
        self.written = 0
S
Shuduo Sang 已提交
141 142

    def drop_table(self):
S
Shuduo Sang 已提交
143 144 145 146
        tdLog.info("drop_table")
        if (self.last_tb != ""):
            tdLog.info("drop last tb %s" % self.last_tb)
            tdSql.execute("drop table %s" % self.last_tb)
S
Shuduo Sang 已提交
147 148 149 150
            self.last_tb = ""
            self.written = 0

    def reset_query_cache(self):
S
Shuduo Sang 已提交
151
        tdLog.info("reset_query_cache")
S
Shuduo Sang 已提交
152 153 154 155
        tdSql.execute("reset query cache")
        tdLog.sleep(1)

    def reset_database(self):
S
Shuduo Sang 已提交
156
        tdLog.info("reset_database")
S
Shuduo Sang 已提交
157 158 159 160 161
        tdDnodes.forcestop(1)
        tdDnodes.deploy(1)
        self.last_tb = ""
        self.written = 0
        tdDnodes.start(1)
S
Shuduo Sang 已提交
162
        tdLog.sleep(5)
S
Shuduo Sang 已提交
163
        tdSql.prepare()
164 165 166
        self.last_tb = ""
        self.last_stb = ""
        self.written = 0
S
Shuduo Sang 已提交
167

S
Shuduo Sang 已提交
168
    def delete_datafiles(self):
S
Shuduo Sang 已提交
169
        tdLog.info("delete_datafiles")
S
Shuduo Sang 已提交
170
        dnodesDir = tdDnodes.getDnodesRootDir()
171 172
        tdDnodes.forcestop(1)
        dataDir = dnodesDir + '/dnode1/data/*'
S
Shuduo Sang 已提交
173 174 175 176
        deleteCmd = 'rm -rf %s' % dataDir
        os.system(deleteCmd)

        self.last_tb = ""
S
Shuduo Sang 已提交
177
        self.last_stb = ""
S
Shuduo Sang 已提交
178 179
        self.written = 0
        tdDnodes.start(1)
S
Shuduo Sang 已提交
180
        tdLog.sleep(10)
S
Shuduo Sang 已提交
181
        tdSql.prepare()
182 183 184
        self.last_tb = ""
        self.last_stb = ""
        self.written = 0
S
Shuduo Sang 已提交
185

186

S
Shuduo Sang 已提交
187
class TDTestCase:
S
Shuduo Sang 已提交
188
    def init(self, conn, logSql):
S
Shuduo Sang 已提交
189
        tdLog.debug("start to execute %s" % __file__)
S
Shuduo Sang 已提交
190
        tdSql.init(conn.cursor(), logSql)
S
Shuduo Sang 已提交
191 192 193 194 195 196 197 198 199 200 201 202

    def run(self):
        tdSql.prepare()

        test = Test()

        switch = {
            1: test.create_table,
            2: test.insert_data,
            3: test.query_data,
            4: test.create_stable,
            5: test.restart_database,
S
Shuduo Sang 已提交
203
            6: test.force_restart_database,
S
Shuduo Sang 已提交
204 205 206
            7: test.drop_table,
            8: test.reset_query_cache,
            9: test.reset_database,
S
Shuduo Sang 已提交
207
            10: test.delete_datafiles,
S
Shuduo Sang 已提交
208 209
            11: test.query_data_from_stable,
            12: test.drop_stable,
S
Shuduo Sang 已提交
210 211
        }

S
Shuduo Sang 已提交
212 213
        for x in range(1, 1000):
            r = random.randint(1, 12)
S
Shuduo Sang 已提交
214 215 216 217 218 219 220 221 222 223
            tdLog.notice("iteration %d run func %d" % (x, r))
            switch.get(r, lambda: "ERROR")()

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


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