sysbench mysql 单表 insert 压测 , 自定义lua测试脚本
提示:环境搭建
文章目录
- 前言
- 一、压测工具 sysbench
- 二、使用步骤
- 1. 自定义测试脚本 , 单表 ,单行插入
- 2.测试
前言
提示:版本
sysbench 1.0.20 (using system LuaJIT 2.1.0-beta3)
mysql 8.0 (2c4g)
提示:以下是本篇文章正文内容,下面案例可供参考
一、压测工具 sysbench
安装
yum install sysbench
二、使用步骤
1. 自定义测试脚本 , 单表 ,单行插入
数据表
CREATE TABLE `im_private_message` (`id` bigint NOT NULL AUTO_INCREMENT COMMENT 'id',`tmp_id` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '临时id,由前端生成',`send_id` bigint NOT NULL COMMENT '发送用户id',`recv_id` bigint NOT NULL COMMENT '接收用户id',`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci COMMENT '发送内容',`type` tinyint(1) NOT NULL COMMENT '消息类型 0:文字 1:图片 2:文件 3:语音 4:视频 21:提示',`status` tinyint(1) NOT NULL COMMENT '状态 0:等待推送(未送达) 1:已送达(未读) 2:撤回 3:已读',`send_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '发送时间',PRIMARY KEY (`id`) USING BTREE,KEY `idx_send_id` (`send_id`) USING BTREE,KEY `idx_recv_id` (`recv_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1307544 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci ROW_FORMAT=DYNAMIC COMMENT='私聊消息';
sysbench_insert_tps_test.lua
-- Sysbench script for testing INSERT TPS on im_private_message table-- Default options
-- Note: In sysbench, options are accessed via sysbench.cmdline.options after definition
function thread_init()drv = sysbench.sql.driver()con = drv:connect()
end-- Cleanup function
function thread_done()con:disconnect()
end-- Prepare function - create test data if needed
function prepare()-- For our test, we don't need to prepare any specific data-- The im_private_message table should already existprint("Preparing sysbench INSERT TPS test for im_private_message table")
end-- Cleanup function
function cleanup()-- No cleanup needed for our testprint("Cleanup completed")
end-- Function to generate random content with emojis
function generate_random_content(length)local text_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "local content = ""-- Generate base textfor i = 1, length dolocal rand_index = sysbench.rand.uniform(1, #text_chars)content = content .. string.sub(text_chars, rand_index, rand_index)end-- Add random emojis (simplified approach for sysbench)local emojis = {"😊", "😂", "🥰", "😍", "🤔", "😎", "🎉", "💕", "🌟", "🔥"}local emoji_count = sysbench.rand.uniform(1, 3)for i = 1, emoji_count dolocal rand_emoji_index = sysbench.rand.uniform(1, #emojis)content = content .. emojis[rand_emoji_index]endreturn content
end-- Function to generate tmp_id
function generate_tmp_id()return "sysbench_" .. os.time() .. "_" .. sysbench.rand.uniform(1000, 9999)
end-- Main test function - this is where we do our INSERT operations
function event()-- Get options or use defaultslocal start_user_id = sysbench.opt.start_user_id or 1000000local max_user_id = sysbench.opt.max_user_id or 1001000-- Generate random sender and receiver IDslocal sender_id = sysbench.rand.uniform(start_user_id, max_user_id)local receiver_id = sysbench.rand.uniform(start_user_id, max_user_id)-- Ensure sender != receiverwhile receiver_id == sender_id doreceiver_id = sysbench.rand.uniform(start_user_id, max_user_id)end-- Generate message contentlocal tmp_id = generate_tmp_id()local content = generate_random_content(20)local send_time = os.date("%Y-%m-%d %H:%M:%S")-- Prepare and execute the INSERT statementlocal query = string.format([[INSERT INTO im_private_message (tmp_id, send_id, recv_id, content, type, status, send_time) VALUES ('%s', %d, %d, '%s', %d, %d, '%s')]], tmp_id, sender_id, receiver_id, content, 0, 0, send_time)con:query(query)-- Update counterif sysbench.counter_inc thensysbench.counter_inc("inserts")end
end-- Define command line options
sysbench.cmdline.options = {start_user_id = {"Start user ID for random selection", 1000000},max_user_id = {"Maximum user ID for random selection", 1001000}
}
2.测试
代码如下
sysbench sysbench_insert_tps_test.lua --db-driver=mysql --mysql-host=192.168.3.61 --mysql-port=3306 --mysql-user=root --mysql-password=123456 --mysql-db=testdb --time=120 --threads=10 --start-user-id=1000000 --max-user-id=1001000 run
测试结果(2c4g): tps 1300 左右
sysbench 1.0.20 (using system LuaJIT 2.1.0-beta3)Running the test with following options:
Number of threads: 10
Initializing random number generator from current timeInitializing worker threads...Threads started!SQL statistics:queries performed:read: 0write: 27434other: 0total: 27434transactions: 27434 (1371.31 per sec.)queries: 27434 (1371.31 per sec.)ignored errors: 0 (0.00 per sec.)reconnects: 0 (0.00 per sec.)General statistics:total time: 20.0040stotal number of events: 27434Latency (ms):min: 5.44avg: 7.29max: 32.7995th percentile: 8.58sum: 199993.92Threads fairness:events (avg/stddev): 2743.4000/88.83execution time (avg/stddev): 19.9994/0.00