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
###################################################################
#           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 *
L
liu0x54 已提交
20
import codecs
S
Shuduo Sang 已提交
21 22 23 24 25


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

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

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

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

S
Shuduo Sang 已提交
49
        tdLog.info("will insert data to table")
S
Shuduo Sang 已提交
50 51 52 53
        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 已提交
54 55
                'insert into %s values (now + %dm, %d, "%s")' %
                (self.last_tb, i, i, "->" + str(i)))
S
Shuduo Sang 已提交
56 57 58
            self.written = self.written + 1

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

    def query_data(self):
S
Shuduo Sang 已提交
77
        tdLog.info("query_data")
S
Shuduo Sang 已提交
78 79 80 81 82 83
        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 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96
        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))
L
liu0x54 已提交
97 98
            sqlcmd = "create table %s using %s tags (1, 'test')" %(current_tb, self.last_stb)
            tdSql.execute(sqlcmd)
S
Shuduo Sang 已提交
99
            self.last_tb = current_tb
S
Shuduo Sang 已提交
100 101
            self.written = 0

S
Shuduo Sang 已提交
102
            tdSql.execute(
L
merge  
liu0x54 已提交
103
                "insert into %s values (now, 27, 'wsnchar')" %
S
Shuduo Sang 已提交
104 105 106 107 108 109 110 111 112 113 114 115
                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 已提交
116 117
            self.last_tb = ""
            self.written = 0
S
Shuduo Sang 已提交
118 119 120 121 122 123 124 125 126 127

    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 已提交
128
    def restart_database(self):
S
Shuduo Sang 已提交
129
        tdLog.info("restart_databae")
S
Shuduo Sang 已提交
130 131 132 133
        tdDnodes.stop(1)
        tdDnodes.start(1)
        tdLog.sleep(5)

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

    def drop_table(self):
S
Shuduo Sang 已提交
145 146 147 148
        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 已提交
149 150 151 152
            self.last_tb = ""
            self.written = 0

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

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

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

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

188

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

    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 已提交
205
            6: test.force_restart_database,
S
Shuduo Sang 已提交
206 207 208
            7: test.drop_table,
            8: test.reset_query_cache,
            9: test.reset_database,
S
Shuduo Sang 已提交
209
            10: test.delete_datafiles,
S
Shuduo Sang 已提交
210 211
            11: test.query_data_from_stable,
            12: test.drop_stable,
S
Shuduo Sang 已提交
212 213
        }

S
Shuduo Sang 已提交
214 215
        for x in range(1, 1000):
            r = random.randint(1, 12)
S
Shuduo Sang 已提交
216 217 218 219 220 221 222 223 224 225
            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())