|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# Description: |
| 4 | +# A simple python script that generates lots of queries to stress the MySQL server. |
| 5 | +# |
| 6 | +# Notes: |
| 7 | +# pip install mysql-connector-python |
| 8 | +# |
| 9 | +# Examples: |
| 10 | +# ./stress_test.py mysql://root:[email protected]:33066/test --times=100 |
| 11 | + |
| 12 | +import time |
| 13 | +import sys |
| 14 | +import argparse |
| 15 | +from urllib.parse import urlparse |
| 16 | + |
| 17 | +import mysql.connector |
| 18 | + |
| 19 | + |
| 20 | +class Base: |
| 21 | + def __init__(self, db, args): |
| 22 | + self.db = db |
| 23 | + self.args = args |
| 24 | + |
| 25 | + def __call__(self): |
| 26 | + self.init_sql() |
| 27 | + |
| 28 | + rows = self.args.rows |
| 29 | + total_time = time.time() |
| 30 | + for i in range(int(self.args.times)): |
| 31 | + start_time = time.time() |
| 32 | + self.test(i) |
| 33 | + print ('{}: round: {} rows: {} to {} time: {}'.format(self.__class__.__name__, |
| 34 | + i, i*rows, (i+1)*rows, time.time()-start_time)) |
| 35 | + |
| 36 | + def init_sql(self): |
| 37 | + pass |
| 38 | + |
| 39 | + def test(self, epoch): |
| 40 | + pass |
| 41 | + |
| 42 | + |
| 43 | +class InsertStress(Base): |
| 44 | + def __init__(self, *args, **kwargs): |
| 45 | + super().__init__(*args, **kwargs) |
| 46 | + self.table_name = 'test' |
| 47 | + |
| 48 | + def init_sql(self): |
| 49 | + cursor = self.db.cursor() |
| 50 | + cursor.execute('CREATE TABLE IF NOT EXISTS {} ' |
| 51 | + '(f1 VARCHAR(30), f2 VARCHAR(30), f3 VARCHAR(30))'.format(self.table_name)) |
| 52 | + self.db.commit() |
| 53 | + |
| 54 | + def test(self, epoch): |
| 55 | + sql = "INSERT INTO {} (f1, f2, f3) VALUES (%s, %s, %s)".format(self.table_name) |
| 56 | + val = [] |
| 57 | + for i in range(self.args.rows): |
| 58 | + val.append(("l_"+str(i), "f_"+str(i), "a_"+str(i))) |
| 59 | + |
| 60 | + |
| 61 | + mycursor = self.db.cursor() |
| 62 | + mycursor.executemany(sql, val) |
| 63 | + self.db.commit() |
| 64 | + |
| 65 | + |
| 66 | +def mysql_connection(dsn): |
| 67 | + dsn = urlparse(dsn) |
| 68 | + db_name = dsn.path.strip('/') |
| 69 | + if len(db_name)==0: |
| 70 | + raise RuntimeError('Database name not corectly specifyied: {}'.format(db_name)) |
| 71 | + |
| 72 | + try: |
| 73 | + db = mysql.connector.connect( |
| 74 | + host=dsn.hostname, |
| 75 | + port=dsn.port, |
| 76 | + user=dsn.username, |
| 77 | + passwd=dsn.password, |
| 78 | + database=db_name, |
| 79 | + ) |
| 80 | + except mysql.connector.errors.ProgrammingError as e: |
| 81 | + if 'Unknown database' in e.msg: |
| 82 | + db = mysql.connector.connect( |
| 83 | + host=dsn.hostname, |
| 84 | + port=dsn.port, |
| 85 | + user=dsn.username, |
| 86 | + passwd=dsn.password, |
| 87 | + ) |
| 88 | + cursor = db.cursor() |
| 89 | + cursor.execute('CREATE DATABASE IF NOT EXISTS {}'.format(db_name)) |
| 90 | + cursor.execute('USE {}'.format(db_name)) |
| 91 | + db.commit() |
| 92 | + else: |
| 93 | + raise |
| 94 | + |
| 95 | + return db |
| 96 | + |
| 97 | + |
| 98 | +def parse_args(): |
| 99 | + parser = argparse.ArgumentParser(description='Stress test for mysql.') |
| 100 | + parser.add_argument('DSN', type=str, |
| 101 | + help='Data source name that contains user and password.') |
| 102 | + parser.add_argument('--rows', type=int, default=100000, help='The number of rows to execute') |
| 103 | + parser.add_argument('--times', type=int, default=100, |
| 104 | + help='The number of times to execute the queries') |
| 105 | + |
| 106 | + return parser.parse_args() |
| 107 | + |
| 108 | + |
| 109 | +def main(): |
| 110 | + args = parse_args() |
| 111 | + db = mysql_connection(args.DSN) |
| 112 | + |
| 113 | + # run insert stress tests |
| 114 | + InsertStress(db, args)() |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments