taosdemoPerformance.py 6.8 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
19 20
from util.log import tdLog
from util.sql import tdSql
21 22

class taosdemoPerformace:
23
    def __init__(self, commitID, dbName):
24
        self.commitID = commitID
25
        self.dbName = dbName
26 27 28
        self.host = "127.0.0.1"
        self.user = "root"
        self.password = "taosdata"
29
        self.config = "/etc/perf"
30 31 32 33
        self.conn = taos.connect(
            self.host,
            self.user,
            self.password,
34
            self.config)
35 36
        self.insertDB = "insertDB"

37 38 39 40 41 42 43 44 45
    def generateJson(self):
        db = {
            "name": "%s" % self.insertDB,
            "drop": "yes",
            "replica": 1
        }

        stb = {
            "name": "meters",
46
            "child_table_exists": "no",
47 48 49 50 51 52 53
            "childtable_count": 10000,
            "childtable_prefix": "stb_",
            "auto_create_table": "no",
            "data_source": "rand",
            "batch_create_tbl_num": 10,
            "insert_mode": "taosc",
            "insert_rows": 100000,
54
            "interlace_rows": 100,
55 56 57 58 59 60 61
            "max_sql_len": 1024000,
            "disorder_ratio": 0,
            "disorder_range": 1000,
            "timestamp_step": 1,
            "start_timestamp": "2020-10-01 00:00:00.000",
            "sample_format": "csv",
            "sample_file": "./sample.csv",
62
            "tags_file": "",
63 64
            "columns": [
                {"type": "INT", "count": 4}
65
            ],
66
            "tags": [
67
                {"type": "INT", "count": 1},
68 69
                {"type": "BINARY", "len": 16}
            ]
70 71 72 73 74 75 76 77 78 79 80 81
        }

        stables = []
        stables.append(stb)

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

        insert_data = {
            "filetype": "insert",
82
            "cfgdir": "/etc/perf",
83 84 85 86 87 88 89 90 91 92
            "host": "127.0.0.1",
            "port": 6030,
            "user": "root",
            "password": "taosdata",
            "thread_count": 10,
            "thread_count_create_tbl": 10,
            "result_file": "./insert_res.txt",
            "confirm_parameter_prompt": "no",
            "insert_interval": 0,
            "num_of_records_per_req": 30000,
93
            "databases": [db]
94 95
        }

96 97 98 99 100 101 102 103 104 105 106 107
        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

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    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):
S
Shengliang Guan 已提交
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
        buildPath = self.getBuildPath()
        if (buildPath == ""):
            tdLog.exit("taosdemo not found!")
        else:
            tdLog.info("taosdemo found in %s" % buildPath)
        binPath = buildPath + "/build/bin/"

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

        os.system("[ -f taosdemoperf.txt ] && rm taosdemoperf.txt")
154

155 156
    def createTablesAndStoreData(self):
        cursor = self.conn.cursor()
157

158 159
        cursor.execute("create database if not exists %s" % self.dbName)
        cursor.execute("use %s" % self.dbName)
160
        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)")
161
        print("==================== taosdemo performance ====================")
162 163 164 165 166
        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))
167 168 169 170 171 172 173 174 175 176
        print("min delay: %f" % float(self.minDelay))
        cursor.execute(
            "insert into taosdemo_perf values(now, %f, %f, %f, '%s', %f, %f, %f)" %
            (float(
                self.createTableTime), float(
                self.insertRecordsTime), float(
                self.recordsPerSecond), self.commitID, float(
                    self.avgDelay), float(
                        self.maxDelay), float(
                            self.minDelay)))
177
        cursor.execute("drop database if exists %s" % self.insertDB)
178 179 180

        cursor.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
    args = parser.parse_args()

200
    perftest = taosdemoPerformace(args.commit_id, args.database_name)
201
    perftest.insertData()
202
    perftest.createTablesAndStoreData()