taosdemoPerformance.py 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
###################################################################
#           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 taos
import pandas as pd
import argparse
import os.path
18
import json
P
Ping Xiao 已提交
19
import sys
20 21

class taosdemoPerformace:
22
    def __init__(self, commitID, dbName, branch, type, numOfTables, numOfRows, numOfInt, numOfDouble, numOfBinary):
23
        self.commitID = commitID
24
        self.dbName = dbName
25 26
        self.branch = branch
        self.type = type
27 28 29 30 31
        self.numOfTables = numOfTables
        self.numOfRows = numOfRows
        self.numOfInt = numOfInt
        self.numOfDouble = numOfDouble
        self.numOfBinary = numOfBinary 
32 33 34
        self.host = "127.0.0.1"
        self.user = "root"
        self.password = "taosdata"
35
        self.config = "/etc/perf"
36 37 38 39
        self.conn = taos.connect(
            self.host,
            self.user,
            self.password,
40
            self.config)
41
        self.insertDB = "insertDB"
42 43 44 45 46 47
        self.host2 = "192.168.1.179"    
        self.conn2 = taos.connect(
            host = self.host2,
            user = self.user,
            password = self.password,
            config = self.config)
48

49 50 51
    def generateJson(self):
        db = {
            "name": "%s" % self.insertDB,
52
            "drop": "yes"
53 54 55 56
        }

        stb = {
            "name": "meters",
57
            "childtable_count": self.numOfTables,
58 59
            "childtable_prefix": "stb_",
            "batch_create_tbl_num": 10,
60
            "insert_mode": "rand",
61
            "insert_rows": self.numOfRows,
62 63
            "batch_rows": 1000000,
            "max_sql_len": 1048576,
64 65 66 67
            "timestamp_step": 1,
            "start_timestamp": "2020-10-01 00:00:00.000",
            "sample_format": "csv",
            "sample_file": "./sample.csv",
68
            "tags_file": "",
69
            "columns": [
70 71 72
                {"type": "INT", "count": self.numOfInt},
                {"type": "DOUBLE", "count": self.numOfDouble},
                {"type": "BINARY", "len": 128, "count": self.numOfBinary}
73
            ],
74
            "tags": [
75
                {"type": "INT", "count": 1},
76 77
                {"type": "BINARY", "len": 16}
            ]
78 79
        }

80

81 82 83 84 85 86 87 88 89 90
        stables = []
        stables.append(stb)

        db = {
            "dbinfo": db,
            "super_tables": stables
        }

        insert_data = {
            "filetype": "insert",
91
            "cfgdir": "/etc/perf",
92 93 94 95 96
            "host": "127.0.0.1",
            "port": 6030,
            "user": "root",
            "password": "taosdata",
            "thread_count": 10,
97
            "thread_count_create_tbl": 4,
98
            "result_file": "./insert_res.txt",
99
            "databases": [db]
100 101
        }

102 103 104 105 106 107 108 109 110 111 112 113
        insert_json_file = f"/tmp/insert.json"

        with open(insert_json_file, 'w') as f:
            json.dump(insert_data, f)
        return insert_json_file

    def getCMDOutput(self, cmd):
        cmd = os.popen(cmd)
        output = cmd.read()
        cmd.close()
        return output

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    def getBuildPath(self):
        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):
            if ("taosdemo" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath

    def insertData(self):
        buildPath = self.getBuildPath()
        if (buildPath == ""):
P
Ping Xiao 已提交
133 134 135
            print("taosdemo not found!")
            sys.exit(1)
            
136 137 138
        binPath = buildPath + "/build/bin/"

        os.system(
139
            "%staosdemo -f %s > /dev/null 2>&1" %
140 141
            (binPath, self.generateJson()))
        self.createTableTime = self.getCMDOutput(
142
            "grep 'Spent' insert_res.txt | awk 'NR==1{print $2}'")
143
        self.insertRecordsTime = self.getCMDOutput(
144
            "grep 'Spent' insert_res.txt | awk 'NR==2{print $2}'")
145
        self.recordsPerSecond = self.getCMDOutput(
146
            "grep 'Spent' insert_res.txt | awk 'NR==2{print $16}'")
147
        self.commitID = self.getCMDOutput("git rev-parse --short HEAD")
148
        delay = self.getCMDOutput(
149
            "grep 'delay' insert_res.txt | awk '{print $4}'")
150
        self.avgDelay = delay[:-4]
151
        delay = self.getCMDOutput(
152
            "grep 'delay' insert_res.txt | awk '{print $6}'")
153
        self.maxDelay = delay[:-4]
154
        delay = self.getCMDOutput(
155
            "grep 'delay' insert_res.txt | awk '{print $8}'")
156 157
        self.minDelay = delay[:-3]

158
        os.system("[ -f insert_res.txt ] && rm insert_res.txt")
159

160
    def createTablesAndStoreData(self):
161
        cursor = self.conn2.cursor()
162

163 164
        cursor.execute("create database if not exists %s" % self.dbName)
        cursor.execute("use %s" % self.dbName)
165
        cursor.execute("create table if not exists taosdemo_perf (ts timestamp, create_table_time float, insert_records_time float, records_per_second float, commit_id binary(50), avg_delay float, max_delay float, min_delay float, branch binary(50), type binary(20), numoftables int, numofrows int, numofint int, numofdouble int, numofbinary int)")
166 167 168 169 170
        print("create tables time: %f" % float(self.createTableTime))
        print("insert records time: %f" % float(self.insertRecordsTime))
        print("records per second: %f" % float(self.recordsPerSecond))
        print("avg delay: %f" % float(self.avgDelay))
        print("max delay: %f" % float(self.maxDelay))
171
        print("min delay: %f" % float(self.minDelay))
172
        cursor.execute("insert into taosdemo_perf values(now, %f, %f, %f, '%s', %f, %f, %f, '%s', '%s', %d, %d, %d, %d, %d)" %
173
            (float(self.createTableTime), float(self.insertRecordsTime), float(self.recordsPerSecond), 
174 175
            self.commitID, float(self.avgDelay), float(self.maxDelay), float(self.minDelay), self.branch, 
            self.type, self.numOfTables, self.numOfRows, self.numOfInt, self.numOfDouble, self.numOfBinary))
176 177
        cursor.close()

178
        cursor1 = self.conn.cursor()
179
        cursor1.execute("drop database if exists %s" % self.insertDB)
180
        cursor1.close()
181

182 183 184 185 186
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '-c',
        '--commit-id',
187
        action='store',
188 189 190 191 192 193 194 195 196
        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)')
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    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)')
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    parser.add_argument(
        '-i',
        '--num-of-int',
        action='store',
        default=4,
        type=int,
        help='num of int columns (default: 4)')
    parser.add_argument(
        '-D',
        '--num-of-double',
        action='store',
        default=0,
        type=int,
        help='num of double columns (default: 4)')
    parser.add_argument(
        '-B',
        '--num-of-binary',
        action='store',
        default=0,
        type=int,
        help='num of binary columns (default: 4)')
    parser.add_argument(
        '-t',
        '--num-of-tables',
        action='store',
        default=10000,
        type=int,
        help='num of tables (default: 10000)')
    parser.add_argument(
        '-r',
        '--num-of-rows',
        action='store',
        default=100000,
        type=int,
        help='num of rows (default: 100000)')
246 247
    args = parser.parse_args()

248
    perftest = taosdemoPerformace(args.commit_id, args.database_name, args.git_branch, args.build_type, args.num_of_tables, args.num_of_rows, args.num_of_int, args.num_of_double, args.num_of_binary)
249
    perftest.insertData()
250
    perftest.createTablesAndStoreData()