random-test.py 6.4 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 124
        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 已提交
125 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 134

    def force_restart_database(self):
        tdLog.info("force_restart_database")
S
Shuduo Sang 已提交
135 136 137
        tdDnodes.forcestop(1)
        tdDnodes.start(1)
        tdLog.sleep(5)
S
Shuduo Sang 已提交
138
        tdSql.prepare()
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 162
        tdSql.prepare()

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

        self.last_tb = ""
        self.written = 0
        tdDnodes.start(1)
        tdSql.prepare()
S
Shuduo Sang 已提交
174

175

S
Shuduo Sang 已提交
176
class TDTestCase:
S
Shuduo Sang 已提交
177
    def init(self, conn, logSql):
S
Shuduo Sang 已提交
178
        tdLog.debug("start to execute %s" % __file__)
S
Shuduo Sang 已提交
179
        tdSql.init(conn.cursor(), logSql)
S
Shuduo Sang 已提交
180 181 182 183 184 185 186 187 188 189 190 191

    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 已提交
192
            6: test.force_restart_database,
S
Shuduo Sang 已提交
193 194 195
            7: test.drop_table,
            8: test.reset_query_cache,
            9: test.reset_database,
S
Shuduo Sang 已提交
196
            10: test.delete_datafiles,
S
Shuduo Sang 已提交
197 198
            11: test.query_data_from_stable,
            12: test.drop_stable,
S
Shuduo Sang 已提交
199 200
        }

S
Shuduo Sang 已提交
201 202
        for x in range(1, 1000):
            r = random.randint(1, 12)
S
Shuduo Sang 已提交
203 204 205 206 207 208 209 210 211 212
            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())