random-test.py 6.7 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 114 115 116 117 118 119 120 121 122 123
        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 = ""

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

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

    def drop_table(self):
S
Shuduo Sang 已提交
141 142 143 144
        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 已提交
145 146 147 148
            self.last_tb = ""
            self.written = 0

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

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

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

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

184

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

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

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