search
HomeDatabaseMysql Tutorialsysbench自定义lua脚本实现实际的业务逻辑压力测试

使用sysbench进行mysql的oltp测试,一般的测试在sysbench中在tests/db中提供了一个oltp.lua脚本可以进行oltp的压力测试。 但不能完全模拟自己实际业务的压力测试,不同的业务,数据结构,数据量都是不一样的,为了更接近实际业务的读写压力测试,就得自己写l

使用sysbench进行mysql的oltp测试,一般的测试在sysbench中在tests/db中提供了一个oltp.lua脚本可以进行oltp的压力测试。
但不能完全模拟自己实际业务的压力测试,不同的业务,数据结构,数据量都是不一样的,为了更接近实际业务的读写压力测试,就得自己写lua脚本,然后通过sysbench进行压力测试。
写这个lua脚本很简单,只需要会写lua脚本就可以了。

1、首先收集实际业务的访问数据库的sql;
2、准备一台恢复好的备份库(从线上导一个)
3、将收集的sql写在lua脚本里(具体如何写,后面举个例子)
4、通过sysbench的--test参数和--mysql-db参数进行测试(这里就不需要prepare了,直接run就行)

举个小例子
模拟实际业务环境:

CREATE TABLE `t1` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10101 DEFAULT CHARSET=utf8

插入模拟的际业务数据:

delimiter $$
create procedure add_data(in maxnum int) 
begin 
	declare i int default 0;
	declare s varchar(500); 
	while(i<maxnum do select concat into s insert t1 set i="i+1;" end while delimiter call add_data>
<p>准备lua脚本:[root@sever3 db]# cat test.lua</p>
<pre class="brush:php;toolbar:false">function thread_init(thread_id)
   -- set_vars()
   db_connect()
end
function event(thread_id)
   local table_name
   local rs
   table_name = "t1"
        db_query("begin")
  for i=1, 10000 do
      rs = db_query("SELECT name FROM ".. table_name .." WHERE id=" .. i)
  end
end

set_vars()? 如果需要使用更多的参数,需要执行这个,前面需要引用下common.lua

db_connect()?? 是连接数据库的,这个是sysbench里的函数,不管那么多,直接用就行。

thread_init()? 第一个调用的lua函数

event(thread_id) ? 可以把sql逻辑写到这里, ? –num-threads多少个,就会同时调用多少个

然后进行压测就ok

[root@sever3 sysbench]# ./sysbench --mysql-socket=/data/mysql_3309/mysql.sock --test=tests/db/test.lua --mysql-user=root --num-threads=12 --report-interval=10 --rand-type=uniform --max-time=30 --max-requests=0 --percentile=99 --mysql-db=test run
sysbench 0.5:  multi-threaded system evaluation benchmark
Running the test with following options:
Number of threads: 12
Report intermediate results every 10 second(s)
Random number generator seed is 0 and will be ignored
Threads started!
[  10s] threads: 12, tps: 0.00, reads/s: 64131.41, writes/s: 0.00, response time: 3291.51ms (99%)
[  20s] threads: 12, tps: 0.00, reads/s: 79980.83, writes/s: 0.00, response time: 1947.61ms (99%)
[  30s] threads: 12, tps: 0.00, reads/s: 78354.15, writes/s: 0.00, response time: 2418.21ms (99%)
OLTP test statistics:
    queries performed:
        read:                            2280000
        write:                           0
        other:                           228
        total:                           2280228
    transactions:                        0      (0.00 per sec.)
    deadlocks:                           0      (0.00 per sec.)
    read/write requests:                 2280000 (72705.35 per sec.)
    other operations:                    228    (7.27 per sec.)
General statistics:
    total time:                          31.3595s
    total number of events:              228
    total time taken by event execution: 368.0393s
    response time:
         min:                                985.61ms
         avg:                               1614.21ms
         max:                               3756.13ms
         approx.  99 percentile:            3289.54ms
Threads fairness:
    events (avg/stddev):           19.0000/3.83
    execution time (avg/stddev):   30.6699/0.42

sysbench具体的使用、结果解读可以参数下

http://imysql.com/2014/10/17/sysbench-full-user-manual.shtml

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.