schemalessInsertPerformance.py 10.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
###################################################################
#           Copyright (c) 2021 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 random
import time
from copy import deepcopy
from util.log import *
from util.cases import *
from util.sql import *
from util.common import tdCom
import threading
22
import itertools
23 24 25 26 27
class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)
        self._conn = conn 
28
        self.lock = threading.Lock()
29 30

    def genMultiColStr(self, int_count=4, double_count=0, binary_count=0):
31 32 33 34 35 36
        '''
            related to self.getPerfSql()
            :count = 4 ---> 4 int
            :count = 1000 ---> 400 int 400 double 200 binary(128)
            :count = 4000 ---> 1900 int 1900 double 200 binary(128)
        '''
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        col_str = ""
        if double_count == 0 and binary_count == 0:
            for i in range(0, int_count):
                if i < (int_count-1):
                    col_str += f'c{i}={random.randint(0, 255)}i32,'
                else:
                    col_str += f'c{i}={random.randint(0, 255)}i32 '
        elif double_count > 0 and binary_count == 0:
            for i in range(0, int_count):
                col_str += f'c{i}={random.randint(0, 255)}i32,'
            for i in range(0, double_count):
                if i < (double_count-1):
                    col_str += f'c{i+int_count}={random.randint(1, 255)}.{i}f64,'
                else:
                    col_str += f'c{i+int_count}={random.randint(1, 255)}.{i}f64 '
        elif double_count == 0 and binary_count > 0:
            for i in range(0, int_count):
                col_str += f'c{i}={random.randint(0, 255)}i32,'
            for i in range(0, binary_count):
                if i < (binary_count-1):
                    col_str += f'c{i+int_count}=\"{tdCom.getLongName(5, "letters")}\",'
                else:
                    col_str += f'c{i+int_count}=\"{tdCom.getLongName(5, "letters")}\" '
        elif double_count > 0 and binary_count > 0:
            for i in range(0, int_count):
                col_str += f'c{i}={random.randint(0, 255)}i32,'
            for i in range(0, double_count):
                col_str += f'c{i+int_count}={random.randint(1, 255)}.{i}f64,'
            for i in range(0, binary_count):
                if i < (binary_count-1):
                    col_str += f'c{i+int_count+double_count}=\"{tdCom.getLongName(5, "letters")}\",'
                else:
                    col_str += f'c{i+int_count+double_count}=\"{tdCom.getLongName(5, "letters")}\" '
        return col_str

    def genLongSql(self, int_count=4, double_count=0, binary_count=0, init=False):
73 74 75
        '''
            :init ---> stb insert line
        '''
76 77 78 79
        if init:
            tag_str = f'id="init",t0={random.randint(0, 65535)}i32,t1=\"{tdCom.getLongName(10, "letters")}\"'
        else:
            tag_str = f'id="sub_{tdCom.getLongName(5, "letters")}_{tdCom.getLongName(5, "letters")}",t0={random.randint(0, 65535)}i32,t1=\"{tdCom.getLongName(10, "letters")}\"'
80
        col_str = self.genMultiColStr(int_count=int_count, double_count=double_count, binary_count=binary_count)
81 82 83 84
        long_sql = 'stb' + ',' + tag_str + ' ' + col_str + '0'
        return long_sql

    def getPerfSql(self, count=4, init=False):
85 86 87 88 89
        '''
            :count = 4 ---> 4 int
            :count = 1000 ---> 400 int 400 double 200 binary(128)
            :count = 4000 ---> 1900 int 1900 double 200 binary(128)
        '''
90 91 92 93 94 95 96 97 98
        if count == 4:
            input_sql = self.genLongSql(init=init)
        elif count == 1000:
            input_sql = self.genLongSql(400, 400, 200, init=init)
        elif count == 4000:
            input_sql = self.genLongSql(1900, 1900, 200, init=init)
        return input_sql

    def replaceLastStr(self, str, new):
99 100 101
        '''
            replace last element of str to new element 
        '''
102 103 104 105
        list_ori = list(str)
        list_ori[-1] = new
        return ''.join(list_ori)

106 107 108 109 110
    def createStb(self, count=4):
        '''
            create 1 stb
        '''
        input_sql = self.getPerfSql(count=count, init=True)
111
        print(threading.current_thread().name, "create stb line:", input_sql)
112
        self._conn.insert_lines([input_sql])
113
        print(threading.current_thread().name, "create stb end")
114

115 116 117 118
    def batchCreateTable(self, batch_list):
        '''
            schemaless insert api
        '''
119
        print(threading.current_thread().name, "length=", len(batch_list))
S
shenglian zhou 已提交
120 121
        print(threading.current_thread().name, 'firstline', batch_list[0][0:50], '...', batch_list[0][-50:-1])
        print(threading.current_thread().name, 'lastline:', batch_list[-1][0:50], '...', batch_list[-1][-50:-1])
122
        begin = time.time_ns();
123
        self._conn.insert_lines(batch_list)
124 125
        end = time.time_ns();
        print(threading.current_thread().name, 'end time:', (end-begin)/10**9)
126

J
jiajingbin 已提交
127
    def splitGenerator(self, table_list, thread_count):
128 129 130 131 132
        '''
            split a list to n piece of sub_list
            [a, b, c, d] ---> [[a, b], [c, d]]
            yield type ---> generator
        '''
J
jiajingbin 已提交
133
        sub_list_len = int(len(table_list)/thread_count)
134 135 136
        for i in range(0, len(table_list), sub_list_len):
            yield table_list[i:i + sub_list_len]

J
jiajingbin 已提交
137
    def genTbListGenerator(self, table_list, thread_count):
138
        '''
J
jiajingbin 已提交
139
            split table_list, after split
140
        '''
J
jiajingbin 已提交
141
        table_list_generator = self.splitGenerator(table_list, thread_count)
142
        return table_list_generator
143

144 145 146 147 148 149 150 151
    def genTableList(self, count=4, table_count=10000):
        '''
            gen len(table_count) table_list
        '''
        table_list = list()
        for i in range(table_count):
            table_list.append(self.getPerfSql(count=count))
        return table_list
152

153 154 155 156
    def threadCreateTables(self, table_list_generator, thread_count=10):
        '''
            thread create tables
        '''
157 158
        threads = list()
        for i in range(thread_count):
159
            t = threading.Thread(target=self.batchCreateTable, args=(next(table_list_generator),))
160 161 162
            threads.append(t)
        return threads

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    def batchInsertRows(self, table_list, rows_count):
        '''
            add rows in each table ---> count=rows_count
        '''
        for input_sql in table_list:
            ts = int(time.time())
            input_sql_list = list()
            for i in range(rows_count-1):
                ts -= 1
                elm_new = self.replaceLastStr(input_sql, str(ts)) + 's'
                input_sql_list.append(elm_new)
            self.batchCreateTable(input_sql_list)

    def threadsInsertRows(self, rows_generator, rows_count=1000, thread_count=10):
        '''
            multi insert rows in each table
        '''
180 181
        threads = list()
        for i in range(thread_count):
182 183
            self.lock.acquire()
            t = threading.Thread(target=self.batchInsertRows, args=(next(rows_generator), rows_count,))
184
            threads.append(t)
185
            self.lock.release()
186 187 188
        return threads

    def multiThreadRun(self, threads):
189 190 191
        '''
            multi run threads
        '''
192 193 194 195 196
        for t in threads:
            t.start()
        for t in threads:
            t.join()

J
jiajingbin 已提交
197
    def createTables(self, count, table_count=10000, thread_count=10):
198 199 200 201 202
        '''
            create stb and tb
        '''
        table_list = self.genTableList(count=count, table_count=table_count)
        create_tables_start_time = time.time()
203
        self.createStb(count=count)
J
jiajingbin 已提交
204
        table_list_generator = self.genTbListGenerator(table_list, thread_count)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        create_tables_generator, insert_rows_generator = itertools.tee(table_list_generator, 2)
        self.multiThreadRun(self.threadCreateTables(table_list_generator=create_tables_generator, thread_count=thread_count))
        create_tables_end_time = time.time()
        create_tables_time = int(create_tables_end_time - create_tables_start_time)
        return_str = f'create tables\' time of {count} columns  ---> {create_tables_time}s'
        return insert_rows_generator, create_tables_time, return_str

    def insertRows(self, count, rows_generator, rows_count=1000, thread_count=10):
        '''
            insert rows 
        '''
        insert_rows_start_time = time.time()
        self.multiThreadRun(self.threadsInsertRows(rows_generator=rows_generator, rows_count=rows_count, thread_count=thread_count))
        insert_rows_end_time = time.time()
        insert_rows_time = int(insert_rows_end_time - insert_rows_start_time)
        return_str = f'insert rows\' time of {count} columns  ---> {insert_rows_time}s'
        return insert_rows_time, return_str

J
jiajingbin 已提交
223
    def schemalessPerfTest(self, count, table_count=10000, thread_count=10, rows_count=1000):
224 225 226
        '''
            get performance
        '''
J
jiajingbin 已提交
227 228
        insert_rows_generator = self.createTables(count=count, table_count=table_count, thread_count=thread_count)[0]
        return self.insertRows(count=count, rows_generator=insert_rows_generator, rows_count=rows_count, thread_count=thread_count)
229

J
jiajingbin 已提交
230
    def getPerfResults(self, test_times=3, table_count=10000, thread_count=10):
231 232 233 234 235
        col4_time = 0
        col1000_time = 0
        col4000_time = 0

        for i in range(test_times):
J
jiajingbin 已提交
236 237 238 239 240 241 242 243 244 245 246 247
            tdCom.cleanTb()
            time_used = self.schemalessPerfTest(count=4, table_count=table_count, thread_count=thread_count)[0]
            col4_time += time_used
        col4_time /= test_times
        print(col4_time)

        # for i in range(test_times):
        #     tdCom.cleanTb()
        #     time_used = self.schemalessPerfTest(count=1000, table_count=table_count, thread_count=thread_count)[0]
        #     col1000_time += time_used
        # col1000_time /= test_times    
        # print(col1000_time)
248
        
J
jiajingbin 已提交
249 250 251 252 253 254
        # for i in range(test_times):
        #     tdCom.cleanTb()
        #     time_used = self.schemalessPerfTest(count=4000, table_count=table_count, thread_count=thread_count)[0]
        #     col4000_time += time_used
        # col4000_time /= test_times
        # print(col4000_time)
255

256
        return col4_time, col1000_time, col4000_time
257 258 259 260

    def run(self):
        print("running {}".format(__file__))
        tdSql.prepare()
J
jiajingbin 已提交
261
        result = self.getPerfResults(test_times=1, table_count=1000, thread_count=10)
262
        print(result)
263 264 265 266 267 268 269

    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())