taosadapter_perftest.py 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
from fabric import Connection
from loguru import logger
import shutil
import os
import time

class TaosadapterPerftest():
    def __init__(self):
        self.ip     = "192.168.1.85" 
        self.port   = "22"     
        self.user   = "root"      
        self.passwd = "tbase125!"
        self.telnetCreateStbJmxFile = "opentsdb_telnet_createStb.jmx"
        self.telnetCreateTbJmxFile = "opentsdb_telnet_createTb.jmx"
        self.telnetInsertRowsFile = "opentsdb_telnet_insertRows.jmx"
16 17
        # self.telnetMixJmxFile = "opentsdb_telnet_MixTbRows.jmx"
        self.telnetMixJmxFile = "opentsdb_telnet_jmeter_csv_import.jmx"
18

19 20 21
        self.jsonCreateStbJmxFile = "opentsdb_json_createStb.jmx"
        self.jsonCreateTbJmxFile = "opentsdb_json_createTb.jmx"
        self.jsonInsertRowsFile = "opentsdb_json_insertRows.jmx"
22 23
        # self.jsonMixJmxFile = "opentsdb_json_MixTbRows.jmx"
        self.jsonMixJmxFile = "opentsdb_json_jmeter_csv_import.jmx"
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        self.logfile = "taosadapter_perftest.log"
        self.createStbThreads = 100
        self.createTbThreads = 100
        self.insertRowsThreads = 24

        logger.add(self.logfile)
    
    def exec_remote_cmd(self, cmd):
        """
            remote exec shell cmd
        """
        try:
            c = Connection(self.ip, user=self.user, port=self.port, connect_timeout=120, connect_kwargs={"password": self.passwd})
            result = c.run(cmd, pty=False, warn=True, hide=True).stdout
            c.close()
            return result
        except Exception as e:
            logger.error(f"exec cmd {cmd} failed:{e}");

    def exec_local_cmd(self, shell_cmd):
        '''
            exec local shell cmd
        '''
        result = os.popen(shell_cmd).read().strip()
        return result

51
    def modifyJxmLooptimes(self, filename, looptimes, row_count=None, import_file_name=None):
52 53 54 55 56 57 58 59 60
        '''
            modify looptimes
        '''
        with open(filename, "r", encoding="utf-8") as f:
            lines = f.readlines()
        with open(filename, "w", encoding="utf-8") as f_w:
            for line in lines:
                if "looptimes" in line:
                    line = line.replace("looptimes", looptimes)
61 62 63
                if row_count is not None:
                    if "row_count" in line:
                        line = line.replace("row_count", row_count)
64 65 66
                if import_file_name is not None:
                    if "import_file_name" in line:
                        line = line.replace("import_file_name", import_file_name)
67 68 69 70 71 72
                f_w.write(line)

    def cleanAndRestartTaosd(self):
        '''
            restart taosd and clean env
        '''
73
        logger.info("---- restarting taosd and taosadapter ----")
74 75 76
        self.exec_remote_cmd("systemctl stop taosd")
        self.exec_remote_cmd("rm -rf /var/lib/taos/* /var/log/taos/*")
        self.exec_remote_cmd("systemctl start taosd")
77
        logger.info("---- finish restart ----")
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        time.sleep(60)
        
    def recreateReportDir(self, path):
        '''
            recreate jmeter report path
        '''
        if os.path.exists(path):
            self.exec_local_cmd(f'rm -rf {path}/*')
        else:
            os.makedirs(path)

    def cleanLog(self):
        '''
            clean log
        '''
        with open(self.logfile, 'w') as f:
            f.seek(0)
            f.truncate()

97 98 99 100 101 102 103 104 105 106
    def genMixTbRows(self, filename, table_count, row_count):
        logger.info('generating import data file')
        ts_start = 1614530008000
        with open(filename, "w", encoding="utf-8") as f_w:
            for i in range(table_count):
                for j in range(row_count):
                    input_line = str(ts_start) + "," + str(i) + '\n'
                    ts_start += 1
                    f_w.write(input_line)

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    def outputParams(self, protocol, create_type):
        '''
            procotol is "telnet" or "json"
            create_type is "stb" or "tb" or "rows"
        '''
        if protocol == "telnet":
            if create_type == "stb":
                return self.telnetCreateStbJmxFile, self.createStbThreads
            elif create_type == "tb":
                return self.telnetCreateTbJmxFile, self.createTbThreads
            elif create_type == "rows":
                return self.telnetInsertRowsFile, self.insertRowsThreads
            else:
                logger.error("create type error!")
        else:
            if create_type == "stb":
                return self.jsonCreateStbJmxFile, self.createStbThreads
            elif create_type == "tb":
                return self.jsonCreateTbJmxFile, self.createTbThreads
            elif create_type == "rows":
                return self.jsonInsertRowsFile, self.insertRowsThreads
            else:
                logger.error("create type error!")

    def insertTDengine(self, procotol, create_type, count):
        '''
            create stb/tb or insert rows
        '''
135
        self.cleanAndRestartTaosd()
136 137 138 139 140 141 142
        jmxfile, threads = self.outputParams(procotol, create_type)
        handle_file = str(count) + jmxfile
        report_dir = f'testreport/{handle_file}'
        self.recreateReportDir(report_dir)
        shutil.copyfile(jmxfile, handle_file)
        replace_count = int(count/threads)
        self.modifyJxmLooptimes(handle_file, str(replace_count))
143
        logger.info(f'jmeter running ----- jmeter -n -t {handle_file} -l {report_dir}/{handle_file}.txt -e -o {report_dir}')
144 145
        result = self.exec_local_cmd(f"jmeter -n -t {handle_file} -l {report_dir}/{handle_file}.txt -e -o {report_dir}")
        logger.info(result)
146 147 148
        logger.info("----- sleep 120s and please record data -----")
        time.sleep(120)
    
149
    def insertMixTbRows(self, procotol, table_count, row_count):
150
        self.cleanAndRestartTaosd()
151 152 153 154 155
        local_path = os.getcwd()
        jmxfile = f"opentsdb_{procotol}_{table_count}Tb{row_count}Rows.jmx"
        import_file_name = f"import_opentsdb_{procotol}_{table_count}Tb{row_count}Rows.txt"
        import_file_path = local_path + '/' + import_file_name
        self.genMixTbRows(import_file_name, table_count, row_count)
156 157 158 159 160 161
        report_dir = f'testreport/{jmxfile}'
        self.recreateReportDir(report_dir)
        if procotol == "telnet":
            shutil.copyfile(self.telnetMixJmxFile, jmxfile)
        else:
            shutil.copyfile(self.jsonMixJmxFile, jmxfile)
162 163
        self.modifyJxmLooptimes(jmxfile, str(int(table_count*row_count/100)), import_file_name=import_file_path)
        logger.info(f'jmeter running ----- jmeter -n -t {jmxfile} -l {report_dir}/{jmxfile}.txt -e -o {report_dir}')
164 165 166 167
        result = self.exec_local_cmd(f"jmeter -n -t {jmxfile} -l {report_dir}/{jmxfile}.txt -e -o {report_dir}")
        logger.info(result)
        logger.info("----- sleep 120s and please record data -----")
        time.sleep(120)
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    
    # def insertMixTbRows(self, procotol, looptimes, row_count):
    #     self.cleanAndRestartTaosd()
    #     jmxfile = f"opentsdb_{procotol}_{looptimes}Tb100Rows.jmx"
    #     report_dir = f'testreport/{jmxfile}'
    #     self.recreateReportDir(report_dir)
    #     if procotol == "telnet":
    #         shutil.copyfile(self.telnetMixJmxFile, jmxfile)
    #     else:
    #         shutil.copyfile(self.jsonMixJmxFile, jmxfile)

    #     self.modifyJxmLooptimes(jmxfile, str(looptimes), str(row_count))
    #     result = self.exec_local_cmd(f"jmeter -n -t {jmxfile} -l {report_dir}/{jmxfile}.txt -e -o {report_dir}")
    #     logger.info(result)
    #     logger.info("----- sleep 120s and please record data -----")
    #     time.sleep(120)
184 185
        

186 187 188 189 190 191 192

if __name__ == '__main__':
    taosadapterPerftest = TaosadapterPerftest()
    taosadapterPerftest.cleanLog()
    
    logger.info('------------ Start testing the scenarios in the report chapter 3.4.1 ------------')
    for procotol in ["telnet", "json"]:
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        logger.info(f'----- {procotol} protocol ------- Creating 30W stable ------------')
        taosadapterPerftest.insertTDengine(procotol, "stb", 300000)
        logger.info(f'----- {procotol} protocol ------- Creating 100W table with stb "cpu.usage_user" ------------')
        taosadapterPerftest.insertTDengine(procotol, "tb", 1000000)
        logger.info(f'----- {procotol} protocol ------- inserting 100W rows ------------')
        taosadapterPerftest.insertTDengine(procotol, "rows", 1000000)

        logger.info(f'----- {procotol} protocol ------- Creating 50W stable ------------')
        taosadapterPerftest.insertTDengine(procotol, "stb", 500000)
        logger.info(f'----- {procotol} protocol ------- Creating 500W table with stb "cpu.usage_user" ------------')
        taosadapterPerftest.insertTDengine(procotol, "tb", 5000000)
        logger.info(f'----- {procotol} protocol ------- inserting 500W rows ------------')
        taosadapterPerftest.insertTDengine(procotol, "rows", 5000000)

        logger.info(f'----- {procotol} protocol ------- Creating 100W stable ------------')
        taosadapterPerftest.insertTDengine(procotol, "stb", 1000000)
        logger.info(f'----- {procotol} protocol ------- Creating 1000W table with stb "cpu.usage_user" ------------')
        taosadapterPerftest.insertTDengine(procotol, "tb", 10000000)
        logger.info(f'----- {procotol} protocol ------- inserting 1000W rows ------------')
        taosadapterPerftest.insertTDengine(procotol, "rows", 10000000)

        logger.info(f'----- {procotol} protocol ------- Creating 10W stable 1000Rows ------------')
        taosadapterPerftest.insertMixTbRows(procotol, 100000, 1000)
216 217

        logger.info(f'----- {procotol} protocol ------- Creating 100W stable 100Rows ------------')
218
        taosadapterPerftest.insertMixTbRows(procotol, 1000000, 100)
219

220
        logger.info(f'----- {procotol} protocol ------- Creating 500W stable 20Rows ------------')
221
        taosadapterPerftest.insertMixTbRows(procotol, 5000000, 20)
222

223
        logger.info(f'----- {procotol} protocol ------- Creating 1000W stable 10Rows ------------')
224
        taosadapterPerftest.insertMixTbRows(procotol, 10000000, 10)