Home Database Mysql Tutorial sysbench自定义lua脚本实现实际的业务逻辑压力测试

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

Jun 07, 2016 pm 04:41 PM
lua business accomplish actual Script customize

使用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就行)
Copy after login

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

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
Copy after login

插入模拟的际业务数据:

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
Copy after login

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
Copy after login

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

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

Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to quickly set up a custom avatar in Netflix How to quickly set up a custom avatar in Netflix Feb 19, 2024 pm 06:33 PM

An avatar on Netflix is ​​a visual representation of your streaming identity. Users can go beyond the default avatar to express their personality. Continue reading this article to learn how to set a custom profile picture in the Netflix app. How to quickly set a custom avatar in Netflix In Netflix, there is no built-in feature to set a profile picture. However, you can do this by installing the Netflix extension on your browser. First, install a custom profile picture for the Netflix extension on your browser. You can buy it in the Chrome store. After installing the extension, open Netflix on your browser and log into your account. Navigate to your profile in the upper right corner and click

How to create a script for editing? Tutorial on how to create a script through editing How to create a script for editing? Tutorial on how to create a script through editing Mar 13, 2024 pm 12:46 PM

Cutting is a video editing tool with comprehensive editing functions, support for variable speed, various filters and beauty effects, and rich music library resources. In this software, you can edit videos directly or create editing scripts, but how to do it? In this tutorial, the editor will introduce the method of editing and making scripts. Production method: 1. Click to open the editing software on your computer, then find the "Creation Script" option and click to open. 2. In the creation script page, enter the "script title", and then enter a brief introduction to the shooting content in the outline. 3. How can I see the "Storyboard Description" option in the outline?

How to execute .sh file in Linux system? How to execute .sh file in Linux system? Mar 14, 2024 pm 06:42 PM

How to execute .sh file in Linux system? In Linux systems, a .sh file is a file called a Shell script, which is used to execute a series of commands. Executing .sh files is a very common operation. This article will introduce how to execute .sh files in Linux systems and provide specific code examples. Method 1: Use an absolute path to execute a .sh file. To execute a .sh file in a Linux system, you can use an absolute path to specify the location of the file. The following are the specific steps: Open the terminal

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Windows PowerShell Scripting Tutorial for Beginners Windows PowerShell Scripting Tutorial for Beginners Mar 13, 2024 pm 10:55 PM

We've designed this Windows PowerShell scripting tutorial for beginners, whether you're a tech enthusiast or a professional looking to improve your scripting skills. If you have no prior knowledge of PowerShell scripting, this article will start with the basics and be tailored for you. We'll help you master the installation steps for a PowerShell environment and walk you through the main concepts and features of PowerShell scripts. If you're ready to learn more about PowerShell scripting, let's embark on this exciting learning journey together! What is WindowsPowerShell? PowerShell is a hybrid command system developed by Microsoft

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

The operation process of edius custom screen layout The operation process of edius custom screen layout Mar 27, 2024 pm 06:50 PM

1. The picture below is the default screen layout of edius. The default EDIUS window layout is a horizontal layout. Therefore, in a single-monitor environment, many windows overlap and the preview window is in single-window mode. 2. You can enable [Dual Window Mode] through the [View] menu bar to make the preview window display the playback window and recording window at the same time. 3. You can restore the default screen layout through [View menu bar>Window Layout>General]. In addition, you can also customize the layout that suits you and save it as a commonly used screen layout: drag the window to a layout that suits you, then click [View > Window Layout > Save Current Layout > New], and in the pop-up [Save Current Layout] Layout] enter the layout name in the small window and click OK

See all articles