queryMetaPerformace.py 8.5 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

###################################################################
#           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 taos
import threading
import time
from datetime import datetime
import numpy as np

22

dengyihao's avatar
dengyihao 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
class MyThread(threading.Thread):

    def __init__(self, func, args=()):
        super(MyThread, self).__init__()
        self.func = func
        self.args = args

    def run(self):
        self.result = self.func(*self.args)

    def get_result(self):
        try:
            return self.result   # 如果子线程不使用join方法,此处可能会报没有self.result的错误
        except Exception:
            return None

39

dengyihao's avatar
dengyihao 已提交
40 41 42 43
class MetadataQuery:
    def initConnection(self):
        self.tables = 100
        self.records = 10
44
        self.numOfTherads = 5
dengyihao's avatar
dengyihao 已提交
45 46 47 48 49
        self.ts = 1537146000000
        self.host = "127.0.0.1"
        self.user = "root"
        self.password = "taosdata"
        self.config = "/etc/taos"
50 51 52 53 54 55
        self.conn = taos.connect(
            self.host,
            self.user,
            self.password,
            self.config)

dengyihao's avatar
dengyihao 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    def connectDB(self):
        return self.conn.cursor()

    def createStable(self):
        print("================= Create stable meters =================")
        cursor = self.connectDB()
        cursor.execute("drop database if exists test")
        cursor.execute("create database test")
        cursor.execute("use test")
        cursor.execute('''create table if not exists meters (ts timestamp, speed int) tags(
                    tgcol1 tinyint, tgcol2 smallint, tgcol3 int, tgcol4 bigint, tgcol5 float, tgcol6 double, tgcol7 bool, tgcol8 binary(20), tgcol9 nchar(20),
                    tgcol10 tinyint, tgcol11 smallint, tgcol12 int, tgcol13 bigint, tgcol14 float, tgcol15 double, tgcol16 bool, tgcol17 binary(20), tgcol18 nchar(20),
                    tgcol19 tinyint, tgcol20 smallint, tgcol21 int, tgcol22 bigint, tgcol23 float, tgcol24 double, tgcol25 bool, tgcol26 binary(20), tgcol27 nchar(20),
                    tgcol28 tinyint, tgcol29 smallint, tgcol30 int, tgcol31 bigint, tgcol32 float, tgcol33 double, tgcol34 bool, tgcol35 binary(20), tgcol36 nchar(20),
                    tgcol37 tinyint, tgcol38 smallint, tgcol39 int, tgcol40 bigint, tgcol41 float, tgcol42 double, tgcol43 bool, tgcol44 binary(20), tgcol45 nchar(20),
                    tgcol46 tinyint, tgcol47 smallint, tgcol48 int, tgcol49 bigint, tgcol50 float, tgcol51 double, tgcol52 bool, tgcol53 binary(20), tgcol54 nchar(20))''')
        cursor.close()

    def createTablesAndInsertData(self, threadID):
        cursor = self.connectDB()
        cursor.execute("use test")
        base = threadID * self.tables

79
        tablesPerThread = int(self.tables / self.numOfTherads)
dengyihao's avatar
dengyihao 已提交
80 81 82 83 84 85 86 87 88
        for i in range(tablesPerThread):
            cursor.execute(
                '''create table t%d using meters tags(
                %d, %d, %d, %d, %f, %f, %d, 'taosdata%d', '涛思数据%d',
                %d, %d, %d, %d, %f, %f, %d, 'taosdata%d', '涛思数据%d',
                %d, %d, %d, %d, %f, %f, %d, 'taosdata%d', '涛思数据%d',
                %d, %d, %d, %d, %f, %f, %d, 'taosdata%d', '涛思数据%d',
                %d, %d, %d, %d, %f, %f, %d, 'taosdata%d', '涛思数据%d',
                %d, %d, %d, %d, %f, %f, %d, 'taosdata%d', '涛思数据%d')''' %
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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                (base + i + 1, (base + i) %
                 100, (base + i) %
                    10000, (base + i) %
                    1000000, (base + i) %
                    100000000, (base + i) %
                    100 * 1.1, (base + i) %
                    100 * 2.3, (base + i) %
                    2, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    10000, (base + i) %
                    1000000, (base + i) %
                    100000000, (base + i) %
                    100 * 1.1, (base + i) %
                    100 * 2.3, (base + i) %
                    2, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    10000, (base + i) %
                    1000000, (base + i) %
                    100000000, (base + i) %
                    100 * 1.1, (base + i) %
                    100 * 2.3, (base + i) %
                    2, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    10000, (base + i) %
                    1000000, (base + i) %
                    100000000, (base + i) %
                    100 * 1.1, (base + i) %
                    100 * 2.3, (base + i) %
                    2, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    10000, (base + i) %
                    1000000, (base + i) %
                    100000000, (base + i) %
                    100 * 1.1, (base + i) %
                    100 * 2.3, (base + i) %
                    2, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    100, (base + i) %
                    10000, (base + i) %
                    1000000, (base + i) %
                    100000000, (base + i) %
                    100 * 1.1, (base + i) %
                    100 * 2.3, (base + i) %
                    2, (base + i) %
                    100, (base + i) %
                    100))
dengyihao's avatar
dengyihao 已提交
144 145 146 147 148
            for j in range(self.records):
                cursor.execute(
                    "insert into t%d values(%d, %d)" %
                    (base + i + 1, self.ts + j, j))
        cursor.close()
149 150 151

    def queryWithTagId(self, threadId, tagId, queryNum):
        print("---------thread%d start-----------" % threadId)
dengyihao's avatar
dengyihao 已提交
152 153 154 155 156 157 158 159 160 161
        query = '''select tgcol1, tgcol2, tgcol3, tgcol4, tgcol5, tgcol6, tgcol7, tgcol8, tgcol9,
                tgcol10, tgcol11, tgcol12, tgcol13, tgcol14, tgcol15, tgcol16, tgcol17, tgcol18,
                tgcol19, tgcol20, tgcol21, tgcol22, tgcol23, tgcol24, tgcol25, tgcol26, tgcol27,
                tgcol28, tgcol29, tgcol30, tgcol31, tgcol32, tgcol33, tgcol34, tgcol35, tgcol36,
                tgcol37, tgcol38, tgcol39, tgcol40, tgcol41, tgcol42, tgcol43, tgcol44, tgcol45,
                tgcol46, tgcol47, tgcol48, tgcol49, tgcol50, tgcol51, tgcol52, tgcol53, tgcol54
                from meters where tgcol{id} > {condition}'''
        latancy = []
        cursor = self.connectDB()
        cursor.execute("use test")
162
        for i in range(queryNum):
dengyihao's avatar
dengyihao 已提交
163
            startTime = time.time()
164
            cursor.execute(query.format(id=tagId, condition=i))
dengyihao's avatar
dengyihao 已提交
165
            cursor.fetchall()
166 167
            latancy.append((time.time() - startTime))
        print("---------thread%d end-----------" % threadId)
dengyihao's avatar
dengyihao 已提交
168
        return latancy
169

dengyihao's avatar
dengyihao 已提交
170 171 172 173
    def queryData(self, query):
        cursor = self.connectDB()
        cursor.execute("use test")

174
        print("================= query tag data =================")
dengyihao's avatar
dengyihao 已提交
175 176 177 178 179 180 181 182 183
        startTime = datetime.now()
        cursor.execute(query)
        cursor.fetchall()
        endTime = datetime.now()
        print(
            "Query time for the above query is %d seconds" %
            (endTime - startTime).seconds)

        cursor.close()
184
        # self.conn.close()
dengyihao's avatar
dengyihao 已提交
185 186 187 188 189 190 191


if __name__ == '__main__':

    t = MetadataQuery()
    t.initConnection()

192 193
    latancys = []
    threads = []
dengyihao's avatar
dengyihao 已提交
194
    tagId = 1
195
    queryNum = 1000
dengyihao's avatar
dengyihao 已提交
196
    for i in range(t.numOfTherads):
197 198
        thread = MyThread(t.queryWithTagId, args=(i, tagId, queryNum))
        threads.append(thread)
dengyihao's avatar
dengyihao 已提交
199
        thread.start()
200
    for i in range(t.numOfTherads):
dengyihao's avatar
dengyihao 已提交
201
        threads[i].join()
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        latancys.extend(threads[i].get_result())
    print("Total query: %d" % (queryNum * t.numOfTherads))
    print(
        "statistic(s): mean= %f, P50 = %f, P75 = %f, P95 = %f, P99 = %f" %
        (sum(latancys) /
         (
            queryNum *
            t.numOfTherads),
            np.percentile(
            latancys,
            50),
            np.percentile(
            latancys,
            75),
            np.percentile(
            latancys,
            95),
            np.percentile(
            latancys,
            99)))