queryPerformance.py 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
###################################################################
#           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 os
import taos
import time
import argparse
import json


class taosdemoQueryPerformace:
    def __init__(self, clearCache, commitID, dbName, stbName, tbPerfix, branch, type):
        self.clearCache = clearCache
        self.commitID = commitID
        self.dbName = dbName
        self.stbName = stbName
        self.tbPerfix = tbPerfix
        self.branch = branch
        self.type = type
        self.host = "127.0.0.1"
        self.user = "root"
        self.password = "taosdata"
P
Ping Xiao 已提交
35
        self.config = "/etc/%s" % self.branch
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        self.conn = taos.connect(
            self.host,
            self.user,
            self.password,
            self.config)
        self.host2 = "192.168.1.179"    
        self.conn2 = taos.connect(
            host = self.host2,
            user = self.user,
            password = self.password,
            config = self.config)

    def createPerfTables(self):
        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_avg float, query_time_max float, query_time_min float, commit_id binary(50), branch binary(50), type binary(20)) tags(query_id int, query_sql binary(300))" % self.stbName)

        sql = "select count(*) from test.meters"
        tableid = 1
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
        
P
Ping Xiao 已提交
58
        sql = "select avg(current), max(voltage), min(phase) from test.meters"
59 60 61
        tableid = 2
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
        
P
Ping Xiao 已提交
62
        sql = "select count(*) from test.meters where location='beijing'"
63 64 65
        tableid = 3
        cursor2.execute("create table if not exists %s%d using %s tags(%d, \"%s\")" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
        
P
Ping Xiao 已提交
66
        sql = "select avg(current), max(voltage), min(phase) from test.meters where groupid=10"
67 68 69
        tableid = 4
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
        
P
Ping Xiao 已提交
70
        sql = "select avg(current), max(voltage), min(phase) from test.d10 interval(10s)"
71 72 73
        tableid = 5
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
        
P
Ping Xiao 已提交
74
        sql = "select last_row(*) from test.meters"
75 76 77
        tableid = 6
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
        
P
Ping Xiao 已提交
78
        sql = "select * from test.meters limit 10000"
79 80 81
        tableid = 7
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))
        
P
Ping Xiao 已提交
82
        sql = "select avg(current), max(voltage), min(phase) from meters where ts <= '2017-07-15 10:40:01.000' and ts <= '2017-07-15 14:00:40.000'"
83 84 85
        tableid = 8
        cursor2.execute("create table if not exists %s%d using %s tags(%d, \"%s\")" % (self.tbPerfix, tableid, self.stbName, tableid, sql))

P
Ping Xiao 已提交
86
        sql = "select last(*) from test.meters"
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        tableid = 9
        cursor2.execute("create table if not exists %s%d using %s tags(%d, '%s')" % (self.tbPerfix, tableid, self.stbName, tableid, sql))

        cursor2.close()
    
    def generateQueryJson(self):
        
        sqls = []
        cursor2 = self.conn2.cursor()
        cursor2.execute("select query_id, query_sql from %s.%s" % (self.dbName, self.stbName))
        i = 0
        for data in cursor2:
            sql = {
                "sql": data[1],
                "result_mode": "onlyformat",
                "result_file": "./query_sql_res%d.txt" % i
            }
            sqls.append(sql)
            i += 1

        query_data = {
            "filetype": "query",
P
Ping Xiao 已提交
109
            "cfgdir": "/etc/%s" % self.branch,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            "host": "127.0.0.1",
            "port": 6030,
            "user": "root",
            "password": "taosdata",
            "databases": "test",
            "specified_table_query": {
                "query_times": 100,
                "concurrent": 1,
                "sqls": sqls
            }
        }        

        query_json_file = f"/tmp/query.json"

        with open(query_json_file, 'w') as f:
            json.dump(query_data, f)
        return query_json_file

    def getBuildPath(self):
P
Ping Xiao 已提交
129
        buildPath=""
130 131 132 133 134 135 136 137
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
P
Ping Xiao 已提交
138
            if ("perfMonitor" in files):
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath
    
    def getCMDOutput(self, cmd):
        cmd = os.popen(cmd)
        output = cmd.read()
        cmd.close()
        return output

    def query(self): 
        buildPath = self.getBuildPath()
        if (buildPath == ""):
P
Ping Xiao 已提交
154
            print("perfMonitor not found!")
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
            sys.exit(1)
            
        binPath = buildPath + "/build/bin/"
        os.system(
            "%sperfMonitor -f %s > query_res.txt" %
            (binPath, self.generateQueryJson()))

        cursor = self.conn2.cursor() 
        print("==================== query performance ====================")
        cursor.execute("use %s" % self.dbName)
        cursor.execute("select tbname, query_sql from %s" % self.stbName)      

        i = 0
        for data in cursor:
            table_name = data[0]
            sql = data[1]

            self.avgDelay = self.getCMDOutput("grep 'avgDelay' query_res.txt | awk 'NR==%d{print $2}'" % (i + 1))
            self.maxDelay = self.getCMDOutput("grep 'avgDelay' query_res.txt | awk 'NR==%d{print $5}'" % (i + 1))
            self.minDelay = self.getCMDOutput("grep 'avgDelay' query_res.txt | awk 'NR==%d{print $8}'" % (i + 1))
            i += 1
            
            print("query time for: %s %f seconds" % (sql, float(self.avgDelay)))
            c = self.conn2.cursor()
            c.execute("insert into %s.%s values(now, %f, %f, %f, '%s', '%s', '%s')" % (self.dbName, table_name, float(self.avgDelay), float(self.maxDelay), float(self.minDelay), self.commitID, self.branch, self.type))            
            
        c.close()    
        cursor.close()

if __name__ == '__main__':
    parser = argparse.ArgumentParser()  
    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)')
    parser.add_argument(
        '-b',
        '--git-branch',
        action='store',
P
Ping Xiao 已提交
224
        default='2.4',
225
        type=str,
P
Ping Xiao 已提交
226
        help='git branch (default: 2.4)')
227 228 229 230 231 232 233 234 235 236 237 238
    parser.add_argument(
        '-T',
        '--build-type',
        action='store',
        default='glibc',
        type=str,
        help='build type (default: glibc)')
    
    args = parser.parse_args()
    perftest = taosdemoQueryPerformace(args.remove_cache, args.commit_id, args.database_name, args.stable_name, args.table_perfix, args.git_branch, args.build_type)
    perftest.createPerfTables()
    perftest.query()