queryPerformance.py 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
###################################################################
#           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 -*-

R
root 已提交
14

15 16 17 18
import sys
import os
import taos
import time
19
import argparse
20 21 22


class taosdemoQueryPerformace:
23
    def __init__(self, clearCache, commitID, dbName, stbName, tbPerfix, branch, type):
24 25 26 27 28
        self.clearCache = clearCache
        self.commitID = commitID
        self.dbName = dbName
        self.stbName = stbName
        self.tbPerfix = tbPerfix
29 30
        self.branch = branch
        self.type = type
31 32 33
        self.host = "127.0.0.1"
        self.user = "root"
        self.password = "taosdata"
P
Ping Xiao 已提交
34
        self.config = "/etc/taosperf"
35 36 37 38 39
        self.conn = taos.connect(
            self.host,
            self.user,
            self.password,
            self.config)
40 41 42 43 44 45
        self.host2 = "192.168.1.179"    
        self.conn2 = taos.connect(
            host = self.host2,
            user = self.user,
            password = self.password,
            config = self.config)
46

47
    def createPerfTables(self):
48 49 50 51
        cursor2 = self.conn2.cursor()
        cursor2.execute("create database if not exists %s" % self.dbName)
        cursor2.execute("use %s" % self.dbName)
        cursor2.execute("create table if not exists %s(ts timestamp, query_time float, commit_id binary(50), branch binary(50), type binary(20)) tags(query_id int, query_sql binary(300))" % self.stbName)
52 53 54

        sql = "select count(*) from test.meters"
        tableid = 1
55
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
56
        
57 58
        sql = "select avg(f1), max(f2), min(f3) from test.meters"
        tableid = 2
59
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
60
        
61 62
        sql = "select count(*) from test.meters where loc='beijing'"
        tableid = 3
63
        cursor2.execute("create table if not exists %s%d using %s tags(%d, \"%s\")" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
64
        
65 66
        sql = "select avg(f1), max(f2), min(f3) from test.meters where areaid=10"
        tableid = 4
67
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
68
        
69 70
        sql = "select avg(f1), max(f2), min(f3) from test.t10 interval(10s)"
        tableid = 5
71
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
72
        
73 74
        sql = "select last_row(*) from meters"
        tableid = 6
75
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
76
        
77 78
        sql = "select * from meters"
        tableid = 7
79
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
80
        
81 82
        sql = "select avg(f1), max(f2), min(f3) from meters where ts <= '2017-07-15 10:40:01.000' and ts <= '2017-07-15 14:00:40.000'"
        tableid = 8
83 84
        cursor2.execute("create table if not exists %s%d using %s tags(%d, \"%s\")" % (self.tbPerfix, tableid, self.stbName, tableid, sql))

85 86
        sql = "select last(*) from meters"
        tableid = 9
87
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
88

89
        cursor2.close()
90 91

    def query(self): 
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        cursor = self.conn.cursor() 
        print("==================== query performance ====================")

        cursor.execute("use %s" % self.dbName)
        cursor.execute("select tbname, query_id, query_sql from %s" % self.stbName)      

        for data in cursor:
            table_name = data[0]
            query_id = data[1]
            sql = data[2]            
            
            totalTime = 0            
            cursor2 = self.conn.cursor()
            cursor2.execute("use test")
            for i in range(100):       
                if(self.clearCache == True):
                    # root permission is required
                    os.system("echo 3 > /proc/sys/vm/drop_caches")                
                
111
                startTime = time.time()      
112 113
                cursor2.execute(sql)
                totalTime += time.time() - startTime
114
            cursor2.close()
115 116
            print("query time for: %s %f seconds" % (sql, totalTime / 100))
                        
117 118
            cursor3 = self.conn2.cursor()
            cursor3.execute("insert into %s.%s values(now, %f, '%s', '%s', '%s')" % (self.dbName, table_name, totalTime / 100, self.commitID, self.branch, self.type))
119 120 121

        cursor3.close()
        cursor.close()
R
root 已提交
122

123
if __name__ == '__main__':
124
    parser = argparse.ArgumentParser()  
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    parser.add_argument(
        '-r',
        '--remove-cache',                
        action='store_true',
        default=False,
        help='clear cache before query (default: False)')
    parser.add_argument(
        '-c',
        '--commit-id',
        action='store',
        default='null',
        type=str,
        help='git commit id (default: null)')
    parser.add_argument(
        '-d',
        '--database-name',
        action='store',
        default='perf',
        type=str,
        help='Database name to be created (default: perf)')
    parser.add_argument(
        '-t',
        '--stable-name',
        action='store',
        default='query_tb',
        type=str,
        help='table name to be created (default: query_tb)')
    parser.add_argument(
        '-p',
        '--table-perfix',
        action='store',
        default='q',
        type=str,
        help='table name perfix (default: q)')
159 160 161 162 163 164 165 166 167 168 169 170 171 172
    parser.add_argument(
        '-b',
        '--git-branch',
        action='store',
        default='master',
        type=str,
        help='git branch (default: master)')
    parser.add_argument(
        '-T',
        '--build-type',
        action='store',
        default='glibc',
        type=str,
        help='build type (default: glibc)')
173 174
    
    args = parser.parse_args()
175
    perftest = taosdemoQueryPerformace(args.remove_cache, args.commit_id, args.database_name, args.stable_name, args.table_perfix, args.git_branch, args.build_type)
176 177
    perftest.createPerfTables()
    perftest.query()