sysbench 数据库性能测试工具的使用

ぃ、小莉子 提交于 2019-11-29 01:56:18

sysbench 数据库性能测试

Mac上安装sysbench测试工具

brew install sysbench

测试sysbench 是否安装成功

//执行这条指令
sysbench cpu --cpu-max-prime=10000 run

sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Prime numbers limit: 10000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:   859.16

General statistics:
    total time:                          10.0008s
    total number of events:              8594

Latency (ms):
         min:                                    1.10
         avg:                                    1.16
         max:                                   24.47
         95th percentile:                        1.32
         sum:                                 9990.99

Threads fairness:
    events (avg/stddev):           8594.0000/0.00
    execution time (avg/stddev):   9.9910/0.00

sysbench能支持哪些性能指标测试

sysbench支持以下几种测试模式:

1、CPU运算性能
2、磁盘IO性能
3、调度程序性能
4、内存分配及传输速度
5、POSIX线程性能
6、数据库性能(OLTP基准测试)
目前sysbench主要支持 mysql,drizzle,pgsql,oracle 等几种数据库。

cpu 运算性能测试

sysbench cpu --cpu-max-prime=10000 run

sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Prime numbers limit: 10000

Initializing worker threads...

Threads started!

CPU speed:
    events per second:   859.16

General statistics:
    total time:                          10.0008s
    total number of events:              8594

Latency (ms):
         min:                                    1.10
         avg:                                    1.16
         max:                                   24.47
         95th percentile:                        1.32
         sum:                                 9990.99

Threads fairness:
    events (avg/stddev):           8594.0000/0.00
    execution time (avg/stddev):   9.9910/0.00

磁盘Io测试

第一步准备阶段,生成测试用到的数据文件,生成的数据文件至少要比内存大

//创建测试文件,后续run操作会读写这个文件 这里文件大小为10G
sysbench --test=fileio --file-total-size=10G prepare

ARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

128 files, 81920Kb each, 10240Mb total
Creating files for the test...
Extra file open flags: (none)
Creating file test_file.0
Creating file test_file.1
Creating file test_file.2
Creating file test_file.3
Creating file test_file.4
Creating file test_file.5
Creating file test_file.6
Creating file test_file.7
Creating file test_file.8
Creating file test_file.9
Creating file test_file.10
Creating file test_file.11
Creating file test_file.12
Creating file test_file.13
Creating file test_file.14
Creating file test_file.15
Creating file test_file.16
Creating file test_file.17
Creating file test_file.18
Creating file test_file.19
Creating file test_file.20
Creating file test_file.21
Creating file test_file.22
Creating file test_file.23
Creating file test_file.24
Creating file test_file.25
Creating file test_file.26
Creating file test_file.27
Creating file test_file.28
Creating file test_file.29
Creating file test_file.30
Creating file test_file.31
Creating file test_file.32
Creating file test_file.33
Creating file test_file.34
Creating file test_file.35
Creating file test_file.36
Creating file test_file.37
Creating file test_file.38
Creating file test_file.39
Creating file test_file.40
Creating file test_file.41
Creating file test_file.42
Creating file test_file.43
Creating file test_file.44
Creating file test_file.45
Creating file test_file.46
Creating file test_file.47
Creating file test_file.48
Creating file test_file.49
Creating file test_file.50
Creating file test_file.51
Creating file test_file.52
Creating file test_file.53
Creating file test_file.54
Creating file test_file.55
Creating file test_file.56
Creating file test_file.57
Creating file test_file.58
Creating file test_file.59
Creating file test_file.60
Creating file test_file.61
Creating file test_file.62
Creating file test_file.63
Creating file test_file.64
Creating file test_file.65
Creating file test_file.66
Creating file test_file.67
Creating file test_file.68
Creating file test_file.69
Creating file test_file.70
Creating file test_file.71
Creating file test_file.72
Creating file test_file.73
Creating file test_file.74
Creating file test_file.75
Creating file test_file.76
Creating file test_file.77
Creating file test_file.78
Creating file test_file.79
Creating file test_file.80
Creating file test_file.81
Creating file test_file.82
Creating file test_file.83
Creating file test_file.84
Creating file test_file.85
Creating file test_file.86
Creating file test_file.87
Creating file test_file.88
Creating file test_file.89
Creating file test_file.90
Creating file test_file.91
Creating file test_file.92
Creating file test_file.93
Creating file test_file.94
Creating file test_file.95
Creating file test_file.96
Creating file test_file.97
Creating file test_file.98
Creating file test_file.99
Creating file test_file.100
Creating file test_file.101
Creating file test_file.102
Creating file test_file.103
Creating file test_file.104
Creating file test_file.105
Creating file test_file.106
Creating file test_file.107
Creating file test_file.108
Creating file test_file.109
Creating file test_file.110
Creating file test_file.111
Creating file test_file.112
Creating file test_file.113
Creating file test_file.114
Creating file test_file.115
Creating file test_file.116
Creating file test_file.117
Creating file test_file.118
Creating file test_file.119
Creating file test_file.120
Creating file test_file.121
Creating file test_file.122
Creating file test_file.123
Creating file test_file.124
Creating file test_file.125
Creating file test_file.126
Creating file test_file.127
10737418240 bytes written in 31.64 seconds (323.62 MiB/sec).

文件的读写模式如下

  • seqwr 顺序写入
  • seqrewr 顺序重写
  • seqrd 顺序读取
  • rndrd 随机读取
  • rndwr 随机写入
  • rndrw 混合随机读/写

第二步 测试写入数据

//执行混合随机测试//随机写入时间为300秒
sysbench --test=fileio --file-total-size=10G --file-test-mode=rndrw --time=300 --max-requests=0 run

WARNING: the --test option is deprecated. You can pass a script name or path on the command line without any options.
sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 1
Initializing random number generator from current time


Extra file open flags: (none)
128 files, 80MiB each
10GiB total file size
Block size 16KiB
Number of IO requests: 0
Read/Write ratio for combined random IO test: 1.50
Periodic FSYNC enabled, calling fsync() each 100 requests.
Calling fsync() at the end of test, Enabled.
Using synchronous I/O mode
Doing random r/w test
Initializing worker threads...

Threads started!


File operations:
    reads/s:                      3688.61
    writes/s:                     2459.07
    fsyncs/s:                     7869.33

Throughput:
    read, MiB/s:                  57.63
    written, MiB/s:               38.42

General statistics:
    total time:                          300.0014s
    total number of events:              4205035

Latency (ms):
         min:                                    0.00
         avg:                                    0.07
         max:                                  153.05
         95th percentile:                        0.20
         sum:                               295336.94

Threads fairness:
    events (avg/stddev):           4205035.0000/0.00
    execution time (avg/stddev):   295.3369/0.00

第三步,清除测试文件

sysbench --test=fileio --file-total-size=10G cleanup

数据库性能(OLTP基准测试)

查看一下:

/usr/local/Cellar/sysbench/1.0.17/share/sysbench目录下的lua文件

-rwxr-xr-x  1 xxydliuyss  staff   1.4K  3 15 20:16 bulk_insert.lua
-rw-r--r--  1 xxydliuyss  staff    14K  3 15 20:16 oltp_common.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.3K  3 15 20:16 oltp_delete.lua
-rwxr-xr-x  1 xxydliuyss  staff   2.4K  3 15 20:16 oltp_insert.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.2K  3 15 20:16 oltp_point_select.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.6K  3 15 20:16 oltp_read_only.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.8K  3 15 20:16 oltp_read_write.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.1K  3 15 20:16 oltp_update_index.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.1K  3 15 20:16 oltp_update_non_index.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.4K  3 15 20:16 oltp_write_only.lua
-rwxr-xr-x  1 xxydliuyss  staff   1.9K  3 15 20:16 select_random_points.lua
-rwxr-xr-x  1 xxydliuyss  staff   2.1K  3 15 20:16 select_random_ranges.lua
drwxr-xr-x  5 xxydliuyss  staff   160B  8 28 18:53 tests

0.5版本之后就取消了—test=oltp的命令,改用直接调用脚本的方式

如何查看脚本的参数和语法

sysbench --test=/usr/local/Cellar/sysbench/1.0.17/share/sysbench/oltp_common.lua help

oltp_common.lua options:
  --auto_inc[=on|off]           Use AUTO_INCREMENT column as Primary Key (for MySQL), or its alternatives in other DBMS. When disabled, use client-generated IDs [on]
  --create_secondary[=on|off]   Create a secondary index in addition to the PRIMARY KEY [on]
  --delete_inserts=N            Number of DELETE/INSERT combinations per transaction [1]
  --distinct_ranges=N           Number of SELECT DISTINCT queries per transaction [1]
  --index_updates=N             Number of UPDATE index queries per transaction [1]
  --mysql_storage_engine=STRING Storage engine, if MySQL is used [innodb]
  --non_index_updates=N         Number of UPDATE non-index queries per transaction [1]
  --order_ranges=N              Number of SELECT ORDER BY queries per transaction [1]
  --pgsql_variant=STRING        Use this PostgreSQL variant when running with the PostgreSQL driver. The only currently supported variant is 'redshift'. When enabled, create_secondary is automatically disabled, and delete_inserts is set to 0
  --point_selects=N             Number of point SELECT queries per transaction [10]
  --range_selects[=on|off]      Enable/disable all range SELECT queries [on]
  --range_size=N                Range size for range SELECT queries [100]
  --secondary[=on|off]          Use a secondary index in place of the PRIMARY KEY [off]
  --simple_ranges=N             Number of simple range SELECT queries per transaction [1]
  --skip_trx[=on|off]           Don't start explicit transactions and execute all queries in the AUTOCOMMIT mode [off]
  --sum_ranges=N                Number of SELECT SUM() queries per transaction [1]
  --table_size=N                Number of rows per table [10000]
  --tables=N                    Number of tables [1]

第一步准备数据库

mysqladmin create test

第二步 准备测试数据

//准备十万条测试数据的量
sysbench --test=/usr/local/Cellar/sysbench/1.0.17/share/sysbench/oltp_common.lua --table-size=100000 --mysql-host=k8s-n1 --mysql-port=3307 --mysql-user=root --mysql-password=123456 --mysql-db=test prepare

第三步测试

进行插入性能测试

//插入10万条数据,8个线程执行,每10秒中报告一次 
sysbench --test=/usr/local/Cellar/sysbench/1.0.17/share/sysbench/oltp_insert.lua --table-size=100000 --mysql-host=k8s-n1 --mysql-port=3307 --mysql-user=root --mysql-password=123456 --mysql-db=test --threads=8 --max-requests=0 --report-interval=10  --time=300 run

结果

sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

Running the test with following options:
Number of threads: 8
Report intermediate results every 10 second(s)
Initializing random number generator from current time


Initializing worker threads...

Threads started!

[ 10s ] thds: 8 tps: 1239.65 qps: 1239.65 (r/w/o: 0.00/1239.65/0.00) lat (ms,95%): 9.39 err/s: 0.00 reconn/s: 0.00
[ 20s ] thds: 8 tps: 1073.90 qps: 1073.90 (r/w/o: 0.00/1073.90/0.00) lat (ms,95%): 13.70 err/s: 0.00 reconn/s: 0.00
[ 30s ] thds: 8 tps: 1008.58 qps: 1008.58 (r/w/o: 0.00/1008.58/0.00) lat (ms,95%): 13.46 err/s: 0.00 reconn/s: 0.00
[ 40s ] thds: 8 tps: 1039.27 qps: 1039.27 (r/w/o: 0.00/1039.27/0.00) lat (ms,95%): 12.75 err/s: 0.00 reconn/s: 0.00
[ 50s ] thds: 8 tps: 958.69 qps: 958.69 (r/w/o: 0.00/958.69/0.00) lat (ms,95%): 15.55 err/s: 0.00 reconn/s: 0.00
[ 60s ] thds: 8 tps: 954.88 qps: 954.88 (r/w/o: 0.00/954.88/0.00) lat (ms,95%): 14.46 err/s: 0.00 reconn/s: 0.00
[ 70s ] thds: 8 tps: 954.40 qps: 954.40 (r/w/o: 0.00/954.40/0.00) lat (ms,95%): 14.46 err/s: 0.00 reconn/s: 0.00
[ 80s ] thds: 8 tps: 897.28 qps: 897.28 (r/w/o: 0.00/897.28/0.00) lat (ms,95%): 16.41 err/s: 0.00 reconn/s: 0.00
[ 90s ] thds: 8 tps: 840.91 qps: 840.91 (r/w/o: 0.00/840.91/0.00) lat (ms,95%): 16.71 err/s: 0.00 reconn/s: 0.00
[ 100s ] thds: 8 tps: 728.62 qps: 728.62 (r/w/o: 0.00/728.62/0.00) lat (ms,95%): 22.28 err/s: 0.00 reconn/s: 0.00
[ 110s ] thds: 8 tps: 733.59 qps: 733.59 (r/w/o: 0.00/733.59/0.00) lat (ms,95%): 20.00 err/s: 0.00 reconn/s: 0.00

结果解读

Running the test with following options:
Number of threads: 8
Report intermediate results every 10 second(s)
Random number generator seed is 0 and will be ignored


Threads started!
-- 每10秒钟报告一次测试结果,tps、每秒读、每秒写、99%以上的响应时长统计
[  10s] threads: 8, tps: 1111.51, reads/s: 15568.42, writes/s: 4446.13, response time: 9.95ms (99%)
[  20s] threads: 8, tps: 1121.90, reads/s: 15709.62, writes/s: 4487.80, response time: 9.78ms (99%)
[  30s] threads: 8, tps: 1120.00, reads/s: 15679.10, writes/s: 4480.20, response time: 9.84ms (99%)
[  40s] threads: 8, tps: 1114.20, reads/s: 15599.39, writes/s: 4456.30, response time: 9.90ms (99%)
[  50s] threads: 8, tps: 1114.00, reads/s: 15593.60, writes/s: 4456.70, response time: 9.84ms (99%)
[  60s] threads: 8, tps: 1119.30, reads/s: 15671.60, writes/s: 4476.50, response time: 9.99ms (99%)
OLTP test statistics:
    queries performed:
        read:                            938224    -- 读总数
        write:                           268064    -- 写总数
        other:                           134032    -- 其他操作总数(SELECT、INSERT、UPDATE、DELETE之外的操作,例如COMMIT等)
        total:                           1340320    -- 全部总数
    transactions:                        67016  (1116.83 per sec.)    -- 总事务数(每秒事务数)
    deadlocks:                           0      (0.00 per sec.)    -- 发生死锁总数
    read/write requests:                 1206288 (20103.01 per sec.)    -- 读写总数(每秒读写次数)
    other operations:                    134032 (2233.67 per sec.)    -- 其他操作总数(每秒其他操作次数)

General statistics:    -- 一些统计结果
    total time:                          60.0053s    -- 总耗时
    total number of events:              67016    -- 共发生多少事务数
    total time taken by event execution: 479.8171s    -- 所有事务耗时相加(不考虑并行因素)
    response time:    -- 响应时长统计
         min:                                  4.27ms    -- 最小耗时
         avg:                                  7.16ms    -- 平均耗时
         max:                                 13.80ms    -- 最长耗时
         approx.  99 percentile:               9.88ms    -- 超过99%平均耗时

Threads fairness:
    events (avg/stddev):           8377.0000/44.33
    execution time (avg/stddev):   59.9771/0.00

其他信息可以参考这个连接:
http://www.linuxidc.com/Linux/2017-04/142856.htm

其他使用参数可以用help查看。

如果需要更加详细关于sysbench 可以查看http://blog.itpub.net/29773961/viewspace-1849676

详细介绍

SysBench 使用指南

更新时间:2018-09-29 16:44:06

本页目录

DRDS 性能测试使用 SysBench 作为压测工具,本文介绍 SysBench 的使用方法。

安装

测试使用的是 SysBench 0.5 版本,安装方法如下:

git clone https://github.com/akopytov/sysbench.gitgit checkout 0.5yum -y install make automake libtool pkgconfig libaio-develyum -y install mariadb-devel./autogen.sh./configuremake -jmake install

注意:以上是在压测 ECS 上的安装方法,如果需要安装到其他操作系统,参考 SysBench 官方文档

安装完毕后,所有自带压测脚本都在 /usr/local/share/sysbench 目录下,DRDS 性能测试将使用该目录下的脚本。除此之外,也可以在源码目录 sysbench/sysbench/tests/db 下找到对应的压测脚本。

使用简介

常用测试模型

SysBench 通过脚本定义了若干常用的压测模型,以下简单介绍几个常用模型:

压测模型 描述
bulk_inert.lua 批量插入数据
insert.lua 单值插入数据
delete.lua 删除数据
oltp.lua 混合读写测试,读写比例14:4
select.lua 简单的主键查询

表结构

对 DRDS 进行测试时,对 SysBench 的测试表稍作改造,将其修改为分库分表的形式,测试表的建表语句如下:

CREATE TABLE `sbtest1` (  `id` int(10) unsigned NOT NULL,  `k` int(10) unsigned NOT NULL DEFAULT '0',  `c` char(120) NOT NULL DEFAULT '',  `pad` char(60) NOT NULL DEFAULT '',  KEY `xid` (`id`),  KEY `k_1` (`k`)) dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 4

以上建表语句中,分库分表数可自行调整,该表会在准备数据阶段自动生成,无需手动创建。

测试命令及参数

使用 SysBench 进行压测,通常分为三个步骤:

  • prepare,准备数据
  • run,运行测试模型
  • cleanup,清理测试数据

通常仅需准备一次数据,在此数据基础上测试各种模型即可。

常用参数

SysBench 中常用的参数如下:

  • --oltp-tables-count=1:表数量
  • --oltp-table-size=10000000:每个表产生的记录行数
  • --oltp-read-only=off:是否生成只读SQL,默认 off,如果设置为 on,则 oltp 模型不会生成 update, delete, insert 的 SQL 语句
  • --oltp-skip-trx=[on|off]:省略 BEGIN / COMMIT 语句。默认是 off
  • --rand-init=on:是否随机初始化数据,如果不随机化那么初始好的数据每行内容除了主键不同外其他完全相同
  • --num-threads=12: 并发线程数,可以理解为模拟的客户端并发连接数
  • --report-interval=10:表示每 10s 输出一次性能数据
  • --max-requests=0:压力测试产生请求的总数,如果以下面的 max-time 来记,这个值设为 0 即可
  • --max-time=120:测试的持续时间
  • --oltp_auto_inc=off:id 是否为自增列
  • --oltp_secondary=on:将 id 设置为非主键防止主键冲突
  • --oltp_range_size=5: 连续取值 5 个,必定走到 5 个分片
  • --mysql_table_options='dbpartition by hash(id) tbpartition by hash(id) tbpartitions 2':DRDS 支持拆分表,在建表的时候需要指定拆分方式

示例命令

以下是示例命令,供参考:

准备数据:

sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp_tables_count=1 --report-interval=10 --oltp-table-size=10000000  --mysql-user=*** --mysql-password=*** --mysql-table-engine=innodb  --rand-init=on  --mysql-host=**** --mysql-port=*** --mysql-db=*** --max-time=300 --max-requests=0   --oltp_skip_trx=on --oltp_auto_inc=off  --oltp_secondary=on --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200   prepare

执行测试

sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp_tables_count=1 --report-interval=10 --oltp-table-size=10000000  --mysql-user=*** --mysql-password=*** --mysql-table-engine=innodb  --rand-init=on  --mysql-host=**** --mysql-port=*** --mysql-db=*** --max-time=300 --max-requests=0   --oltp_skip_trx=on --oltp_auto_inc=off --oltp_secondary --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200 run

清理环境

sysbench --test='/usr/local/share/sysbench/oltp.lua' --oltp_tables_count=1 --report-interval=10 --oltp-table-size=10000000  --mysql-user=*** --mysql-password=*** --mysql-table-engine=innodb  --rand-init=on  --mysql-host=**** --mysql-port=*** --mysql-db=*** --max-time=300 --max-requests=0   --oltp_skip_trx=on --oltp_auto_inc=off --oltp_secondary --oltp_range_size=5 --mysql_table_options='dbpartition by hash(`id`) tbpartition by hash(`id`) tbpartitions 2' --num-threads=200 cleanup

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

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

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

1、首先收集实际业务的访问数据库的sql;
2、准备一台恢复好的备份库(从线上导一个)
3、将收集的sql写在lua脚本里(具体如何写,后面举个例子)4、通过sysbench的--test参数和--mysql-db参数进行测试(这里就不需要prepare了,直接run就行)
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
) 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("abcdedsfsfsdfsfsf",i) into s; 
        insert into t1(name)values(s);  
        set i=i+1;
    end while;
end $$ 
delimiter ;call add_data(10000);

call add_data(10000);

准备lua脚本:[root@sever3 db]# cat test.lua

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)
  endend
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

别人的demo

pathtest = string.match(test, "(.*/)") or ""

dofile(pathtest .. "common.lua")

function thread_init(thread_id)
   set_vars()

   if (db_driver == "mysql" and mysql_table_engine == "myisam") then
      begin_query = "LOCK TABLES sbtest WRITE"
      commit_query = "UNLOCK TABLES"
   else
      begin_query = "BEGIN"
      commit_query = "COMMIT"
   end
end

function event(thread_id)
   local vid1
   local vid2
   local vid3
   local vid4
   local vid5
   local vk
   local vxv
   vid1 = sb_rand_uniform(1,10000)
   vid2 = sb_rand_uniform(1,10000)
   vid3 = sb_rand_uniform(1,10000)
   vid4 = sb_rand_uniform(1,10000)
   vid5 = sb_rand_uniform(1,10000)
   vk = sb_rand_uniform(10,10000)
   vxv = sb_rand_str([[###########-###########-###########-###########-###########]])
   rs = db_query("SELECT pad FROM test1 WHERE id=" .. vid1)
   rs = db_query("SELECT pad FROM test2 WHERE id=" .. vid2)
   rs = db_query("SELECT pad FROM test3 WHERE id=" .. vid3)

   db_query(begin_query)
   rs = db_query("update test4 set k=k-1 where id=" .. vid4)
   rs = db_query("update test5 set k=k+1 where id=" .. vid5)
   db_query(commit_query)

   rs = db_query("insert into test6 (k,xv) values " .. string.format("(%d , '%s')",vk,vxv));

end

利用sysbench快速插入海量数据到数据库

1.准备两个表,

CREATE TABLE `employ` (
  `id` bigint(20) NOT NULL COMMENT '员工编号',
  `name` varchar(48) NOT NULL COMMENT '名字',
  `departmentId` bigint(10) NOT NULL COMMENT '部门编号',
  `datasourceId` varchar(64) NOT NULL COMMENT '数据存储位置',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `employExt` (
  `userid` bigint(20) NOT NULL COMMENT '员工编号',
  `username` varchar(48) NOT NULL COMMENT '名字',
  `address` varchar(255) NOT NULL COMMENT '地址',
  `phone` char(11) NOT NULL COMMENT '号码',
  `cardId` char(18) NOT NULL COMMENT '身份证号码',
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

第二步编写lua脚本

#!/usr/bin/env sysbench

require("oltp_common")


function thread_init()
   if (db_driver == "mysql" and mysql_table_engine == "myisam") then
      begin_query = "LOCK TABLES sbtest WRITE"
      commit_query = "UNLOCK TABLES"
   else
      begin_query = "BEGIN"
      commit_query = "COMMIT"
   end
   drv = sysbench.sql.driver()
   con = drv:connect()
   stmt = {}
   param = {}

   for t = 1, sysbench.opt.tables do
      stmt[t] = {}
      param[t] = {}
   end

   -- This function is a 'callback' defined by individual benchmark scripts
   prepare_statements()
end

function prepare_statements()
   -- We do not use prepared statements here, but oltp_common.sh expects this
   -- function to be defined
end

function event(thread_id)
   local table_name = "employ"
   local table1_name = "employExt"
   local employ_name = get_c_value()
   local departmentId = sb_rand_uniform(10,10000)
   local datasourceId = "db1"

   if (sysbench.opt.auto_inc) then
      i = 0
   else
         -- Convert a uint32_t value to SQL INT
      i = sysbench.rand.unique() - 2147483648
   end
      con:query(string.format("INSERT INTO %s (id, name, departmentId, datasourceId) VALUES " ..
                                 "(%d, %s, '%d', '%s')",
                              table_name, i, employ_name, departmentId, datasourceId))
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!