|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import time |
| 4 | +import pymysql |
| 5 | + |
| 6 | +def timer(func): |
| 7 | + def wrapper(*args, **kwargs): |
| 8 | + start_time = time.time() |
| 9 | + result = func(*args, **kwargs) |
| 10 | + end_time = time.time() |
| 11 | + elapsed_time = end_time - start_time |
| 12 | + return result, elapsed_time |
| 13 | + return wrapper |
| 14 | + |
| 15 | +@timer |
| 16 | +def test_ping(unix_socket, user, password, database, num_iterations): |
| 17 | + try: |
| 18 | + connection = pymysql.connect(unix_socket=unix_socket, user=user, password=password, database=database, |
| 19 | + charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) |
| 20 | + for _ in range(num_iterations): |
| 21 | + connection.ping(reconnect=False) |
| 22 | + except pymysql.MySQLError as e: |
| 23 | + print(f"Error during ping: {e}") |
| 24 | + finally: |
| 25 | + if connection: |
| 26 | + connection.close() |
| 27 | + |
| 28 | +@timer |
| 29 | +def test_select(unix_socket, user, password, database, num_iterations, sql): |
| 30 | + try: |
| 31 | + connection = pymysql.connect(unix_socket=unix_socket, user=user, password=password, database=database, |
| 32 | + charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) |
| 33 | + with connection.cursor() as cursor: |
| 34 | + for _ in range(num_iterations): |
| 35 | + cursor.execute(sql) |
| 36 | + except pymysql.MySQLError as e: |
| 37 | + print(f"Error during {sql}: {e}") |
| 38 | + finally: |
| 39 | + if connection: |
| 40 | + connection.close() |
| 41 | + |
| 42 | +unix_socket = "/data/mysql/3306/data/mysql.sock" |
| 43 | +user = "root" |
| 44 | +password = "123456" |
| 45 | +database = "information_schema" |
| 46 | +num_iterations = 100000 # 执行次数 |
| 47 | + |
| 48 | +# 测试 PING 操作 |
| 49 | +result, elapsed_time = test_ping(unix_socket, user, password, database, num_iterations) |
| 50 | +print(f"PING time for {num_iterations} iterations: {elapsed_time:.5f} seconds") |
| 51 | + |
| 52 | +# 测试 SELECT 1 |
| 53 | +result, elapsed_time = test_select(unix_socket, user, password, database, num_iterations, "SELECT 1") |
| 54 | +print(f"SELECT 1 time for {num_iterations} iterations: {elapsed_time:.5f} seconds") |
| 55 | + |
| 56 | +# 测试 SHOW FULL TABLES FROM `information_schema` LIKE 'PROBABLYNOT' |
| 57 | +result, elapsed_time = test_select(unix_socket, user, password, database, num_iterations, "SHOW FULL TABLES FROM `information_schema` LIKE 'PROBABLYNOT'") |
| 58 | +print(f"SHOW FULL TABLES time for {num_iterations} iterations: {elapsed_time:.5f} seconds") |
| 59 | + |
| 60 | +# 测试 INFORMATION_SCHEMA.TABLES |
| 61 | +new_get_tables_sql = "SELECT TABLE_SCHEMA AS TABLE_CAT, NULL AS TABLE_SCHEM, TABLE_NAME, CASE WHEN TABLE_TYPE='BASE TABLE' THEN CASE WHEN TABLE_SCHEMA = 'mysql' OR TABLE_SCHEMA = 'performance_schema' THEN 'SYSTEM TABLE' ELSE 'TABLE' END WHEN TABLE_TYPE='TEMPORARY' THEN 'LOCAL_TEMPORARY' ELSE TABLE_TYPE END AS TABLE_TYPE, TABLE_COMMENT AS REMARKS, NULL AS TYPE_CAT, NULL AS TYPE_SCHEM, NULL AS TYPE_NAME, NULL AS SELF_REFERENCING_COL_NAME, NULL AS REF_GENERATION FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'PROBABLYNOT' HAVING TABLE_TYPE IN ('TABLE',null,null,null,null) ORDER BY TABLE_TYPE, TABLE_SCHEMA, TABLE_NAME" |
| 62 | +result, elapsed_time = test_select(unix_socket, user, password, database, num_iterations, new_get_tables_sql) |
| 63 | +print(f"INFORMATION_SCHEMA.TABLES time for {num_iterations} iterations: {elapsed_time:.5f} seconds") |
0 commit comments