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

    def genMultiColStr(self, int_count=4, double_count=0, binary_count=0):
32 33 34 35 36 37
        '''
            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)
        '''
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 73
        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):
74 75 76
        '''
            :init ---> stb insert line
        '''
77 78 79 80
        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")}\"'
81
        col_str = self.genMultiColStr(int_count=int_count, double_count=double_count, binary_count=binary_count)
82 83 84 85
        long_sql = 'stb' + ',' + tag_str + ' ' + col_str + '0'
        return long_sql

    def getPerfSql(self, count=4, init=False):
86 87 88 89 90
        '''
            :count = 4 ---> 4 int
            :count = 1000 ---> 400 int 400 double 200 binary(128)
            :count = 4000 ---> 1900 int 1900 double 200 binary(128)
        '''
91 92 93 94 95 96 97 98 99
        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):
100 101 102
        '''
            replace last element of str to new element 
        '''
103 104 105 106
        list_ori = list(str)
        list_ori[-1] = new
        return ''.join(list_ori)

107 108 109 110 111
    def createStb(self, count=4):
        '''
            create 1 stb
        '''
        input_sql = self.getPerfSql(count=count, init=True)
S
shenglian zhou 已提交
112
        self._conn.insertLines([input_sql])
113

114 115 116 117
    def batchCreateTable(self, batch_list):
        '''
            schemaless insert api
        '''
118
        print(threading.current_thread().name, "length=", len(batch_list))
S
shenglian zhou 已提交
119 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])
        self._conn.insertLines(batch_list)
122
        print(threading.current_thread().name, 'end')
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

    def splitGenerator(self, table_list, sub_list_len):
        '''
            split a list to n piece of sub_list
            [a, b, c, d] ---> [[a, b], [c, d]]
            yield type ---> generator
        '''
        for i in range(0, len(table_list), sub_list_len):
            yield table_list[i:i + sub_list_len]

    def genTbListGenerator(self, table_list, sub_list_len):
        '''
            split table_list, after split, every sub_list len is sub_list_len
        '''
        table_list_generator = self.splitGenerator(table_list, sub_list_len)
        return table_list_generator
139

140 141 142 143 144 145 146 147
    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
148

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

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    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
        '''
176 177
        threads = list()
        for i in range(thread_count):
178 179
            self.lock.acquire()
            t = threading.Thread(target=self.batchInsertRows, args=(next(rows_generator), rows_count,))
180
            threads.append(t)
181
            self.lock.release()
182 183 184
        return threads

    def multiThreadRun(self, threads):
185 186 187
        '''
            multi run threads
        '''
188 189 190 191 192
        for t in threads:
            t.start()
        for t in threads:
            t.join()

193 194 195 196 197 198
    def createTables(self, count, table_count=10000, sub_list_len=1000, thread_count=10):
        '''
            create stb and tb
        '''
        table_list = self.genTableList(count=count, table_count=table_count)
        create_tables_start_time = time.time()
199
        self.createStb()
200 201 202 203 204 205 206 207 208 209 210 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
        table_list_generator = self.genTbListGenerator(table_list, sub_list_len)
        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

    def schemalessPerfTest(self, count, table_count=10000, sub_list_len=1000, thread_count=10):
        '''
            get performance
        '''
        insert_rows_generator = self.createTables(count=count, table_count=table_count, sub_list_len=sub_list_len, thread_count=thread_count)[0]
        return self.insertRows(count=count, rows_generator=insert_rows_generator, rows_count=1000, thread_count=10)

    def getPerfResults(self, test_times=3, table_count=10000, sub_list_len=1000, thread_count=10):
        col4_time = 0
        col1000_time = 0
        col4000_time = 0

        # for i in range(test_times):
        #     time_used = self.schemalessPerfTest(count=4, table_count=table_count, sub_list_len=sub_list_len, thread_count=thread_count)[0]
        #     col4_time += time_used
        # col4_time /= test_times
        # print(col4_time)

237
        tdCom.cleanTb()
238
        for i in range(test_times):
S
shenglian zhou 已提交
239
            time_used = self.schemalessPerfTest(count=1000, table_count=table_count, sub_list_len=sub_list_len, thread_count=thread_count)[0]
240 241 242
            col1000_time += time_used
        col1000_time /= test_times    
        print(col1000_time)
243
        
244 245
        tdCom.cleanTb()
        for i in range(test_times):
S
shenglian zhou 已提交
246
            time_used = self.schemalessPerfTest(count=4000, table_count=table_count, sub_list_len=sub_list_len, thread_count=thread_count)[0]
247 248 249
            col4000_time += time_used
        col4000_time /= test_times
        print(col4000_time)
250

251
        return col4_time, col1000_time, col4000_time
252 253 254 255

    def run(self):
        print("running {}".format(__file__))
        tdSql.prepare()
256
        result = self.getPerfResults(test_times=1, table_count=1000, sub_list_len=100, thread_count=10)
257
        print(result)
258 259 260 261 262 263 264

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

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