MySQL基准测试

MySQL基准测试

原因

  • 测试评估出当前系统的运行情况,建立性能基准线;
  • 模拟更高负载,找出扩展性瓶颈;
  • 规划未来的业务增长;
  • 测试不同的硬件、软件和操作系统配置;

我们只能进行大概的测试,来确定系统大致的余量。

策略

集成式

  1. 测试整体性能而不只是MySQL;
  2. 可以发现各组件之间缓存带来的影响;
  3. 更能揭示应用的真是表现;
  4. 很难建立,也很难正确设置。

组件式

  1. 需要比较不同的表结构或查询的性能;
  2. 需要针对应用中的某个具体问题进行测试;
  3. 为了避免漫长的基准测试,可以通过一个短期基准测试,快速循环,检测出调整后的效果。

指标

  1. 吞吐量:单位时间内的事务处理数,TPS和TPM。
  2. 响应时间:任务所需的整体时间,百分比响应时间和平均响应时间。
  3. 并发性:任意时间内同时发生的并发数,同时工作的线程数Threads_running或者连接数。并发性测试不是为了测试应用能达到的并发度,而是为了测试应用在不同并发下的性能。
  4. 可扩展性:系统性能/资源数的函数关系,衡量系统性能是否可以随着资源的扩展而线性扩展。

方法

错误行为

  • 使用真实数据的子集而不是全集;
  • 使用均匀的数据分布而不是真实数据分布;
  • 使用不真实的分布参数;
  • 在多用户场景中,做单用户测试;
  • 在单服务器上测试分布式应用;
  • 与真实用户行为不匹配的测试;
  • 反复执行同一个查询导致一直走缓存;
  • 忽略了系统预热的过程;
  • 测试时间太短而无法覆盖场景;
  • 在基准测试运行过程中有资源被其它任务消耗;
  • 基于MySQL的默认配置的测试没有意义,默认配置是基于消耗很少内存的极小应用的。

步骤

  1. 提出问题并明确目标;
  2. 决定采用标准基准测试还是设计专用测试;
  3. 如果要设计专用测试,获取生产数据集的快照,该快照要易于还原,以便后续的测试;选择一个时间段,记录生产系统上的查询(querylog),要记录查询所在的线程以进行回放;
  4. 需要为每一轮测试创建一个目录,保存测试结果、配置文件、测试指标、脚本和相关说明;
  5. 需要准备好系统状态收集的相关的脚本,可以自己编写也可以使用pt工具;
  6. 确定基准测试的时长,基准测试运行的时间至少要等到系统达到稳定状态之后,这时候的稳定状态才是可以相信的;
  7. 决定是否使用自动化测试,从而准备自动化基准测试脚本,或者使用相关工具;
  8. 进行基准测试,通过逐步修改基准测试参数来进行迭代找出正确的参数值;
  9. 使用工具将测试过程中的数据绘制成图形,可以使用gnuplot或者R或者python;
  10. 通过图形将数字转化为结论。

在整个测试过程中,要详细地写下测试规划,应该记录测试数据、系统配置步骤、如何测量和分析结果、预热方案等,将参数和结果文档化。

工具

sysbench
help
Usage:
  sysbench [options]... [testname] [command]

Commands implemented by most tests: prepare run cleanup help

General options:
  --threads=N                     number of threads to use [1]
  --events=N                      limit for total number of events [0]
  --time=N                        limit for total execution time in seconds [10]
  --forced-shutdown=STRING        number of seconds to wait after the --time limit before forcing shutdown, or 'off' to disable [off]
  --thread-stack-size=SIZE        size of stack per thread [64K]
  --rate=N                        average transactions rate. 0 for unlimited rate [0]
  --report-interval=N             periodically report intermediate statistics with a specified interval in seconds. 0 disables intermediate reports [0]
  --report-checkpoints=[LIST,...] dump full statistics and reset all counters at specified points in time. The argument is a list of comma-separated values representing the amount of time in seconds elapsed from start of test when report checkpoint(s) must be performed. Report checkpoints are off by default. []
  --debug[=on|off]                print more debugging info [off]
  --validate[=on|off]             perform validation checks where possible [off]
  --help[=on|off]                 print help and exit [off]
  --version[=on|off]              print version and exit [off]
  --config-file=FILENAME          File containing command line options
  --tx-rate=N                     deprecated alias for --rate [0]
  --max-requests=N                deprecated alias for --events [0]
  --max-time=N                    deprecated alias for --time [0]
  --num-threads=N                 deprecated alias for --threads [1]

Pseudo-Random Numbers Generator options:
  --rand-type=STRING random numbers distribution {uniform,gaussian,special,pareto} [special]
  --rand-spec-iter=N number of iterations used for numbers generation [12]
  --rand-spec-pct=N  percentage of values to be treated as 'special' (for special distribution) [1]
  --rand-spec-res=N  percentage of 'special' values to use (for special distribution) [75]
  --rand-seed=N      seed for random number generator. When 0, the current time is used as a RNG seed. [0]
  --rand-pareto-h=N  parameter h for pareto distribution [0.2]

Log options:
  --verbosity=N verbosity level {5 - debug, 0 - only critical messages} [3]

  --percentile=N       percentile to calculate in latency statistics (1-100). Use the special value of 0 to disable percentile calculations [95]
  --histogram[=on|off] print latency histogram in report [off]

General database options:

  --db-driver=STRING  specifies database driver to use ('help' to get list of available drivers) [mysql]
  --db-ps-mode=STRING prepared statements usage mode {auto, disable} [auto]
  --db-debug[=on|off] print database-specific debug information [off]


Compiled-in database drivers:
  mysql - MySQL driver
  pgsql - PostgreSQL driver

mysql options:
  --mysql-host=[LIST,...]          MySQL server host [localhost]
  --mysql-port=[LIST,...]          MySQL server port [3306]
  --mysql-socket=[LIST,...]        MySQL socket
  --mysql-user=STRING              MySQL user [sbtest]
  --mysql-password=STRING          MySQL password []
  --mysql-db=STRING                MySQL database name [sbtest]
  --mysql-ssl[=on|off]             use SSL connections, if available in the client library [off]
  --mysql-ssl-cipher=STRING        use specific cipher for SSL connections []
  --mysql-compression[=on|off]     use compression, if available in the client library [off]
  --mysql-debug[=on|off]           trace all client library calls [off]
  --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
  --mysql-dry-run[=on|off]         Dry run, pretend that all MySQL client API calls are successful without executing them [off]

pgsql options:
  --pgsql-host=STRING     PostgreSQL server host [localhost]
  --pgsql-port=N          PostgreSQL server port [5432]
  --pgsql-user=STRING     PostgreSQL user [sbtest]
  --pgsql-password=STRING PostgreSQL password []
  --pgsql-db=STRING       PostgreSQL database name [sbtest]

Compiled-in tests:
  fileio - File I/O test
  cpu - CPU performance test
  memory - Memory functions speed test
  threads - Threads subsystem performance test
  mutex - Mutex performance test

See 'sysbench <testname> help' for a list of options for each test.

常用通用参数:

  • --threads=N:需要使用的线程总数(默认值为1)。
  • --events=N:请求总数的上限值(默认值为10000)。
  • --thread-stack-size=SIZE:每个线程的栈空间的大小(默认值为32KB)。
  • --tx-rate=N:目标事务速率(TPS)(默认值为0)。
  • --report-interval=N:指定一个间隔时间,sysbench便会定期地报告测试期间的中间统计结果,以秒为单位。若取值为0,则表示禁用中间报告功能(默认值为0)。
  • --report-checkpoints=[LIST,...]:在指定的时间点,转储完整的统计数据,并且复位所有的计数器。这个选项的参数是一个由逗号分隔的值,表示从测试开始到必须执行报告检查点时需要花费的时间,以秒为单位。默认禁用报告检查点。
  • --test=STRING:需要运行的测试项。
  • --percentile=N:计算查询响应时间的百分比等级(默认值为95,表示sysbench会丢弃5%的最长的请求,然后再从剩余的请求中选择一个最长的请求)。
  • --histogram=[on|off]:打印响应时长直方图。
fileio测试
fileio options:
  --file-num=N                  number of files to create [128]
  --file-block-size=N           block size to use in all IO operations [16384]
  --file-total-size=SIZE        total size of files to create [2G]
  --file-test-mode=STRING       test mode {seqwr, seqrewr, seqrd, rndrd, rndwr, rndrw}
  --file-io-mode=STRING         file operations mode {sync,async,mmap} [sync]
  --file-async-backlog=N        number of asynchronous operatons to queue per thread [128]
  --file-extra-flags=[LIST,...] list of additional flags to use to open files {sync,dsync,direct} []
  --file-fsync-freq=N           do fsync() after this number of requests (0 - don't use fsync()) [100]
  --file-fsync-all[=on|off]     do fsync() after each write operation [off]
  --file-fsync-end[=on|off]     do fsync() at the end of test [on]
  --file-fsync-mode=STRING      which method to use for synchronization {fsync, fdatasync} [fsync]
  --file-merged-requests=N      merge at most this number of IO requests if possible (0 - don't merge) [0]
  --file-rw-ratio=N             reads/writes ratio for combined test [1.5]

在这个测试中,你需要创建一个大于最大内存的文件,然后选择一种读写方式(seqwr\seqrewr\seqrd\rndrd\rndwr\rndrw)测试。

  • --file-num=N:需要创建的文件数量(默认为128个文件)。
  • --file-block-size=N:在所有的IO操作中使用的数据块的大小(默认值为16384)。
  • --file-total-size=SIZE:需要创建的文件的总大小(默认值为2G)。
  • --file-test-mode=STRING:测试模式,可选的值有seqwr(顺序写)、seqrewr(顺序重写)、seqrd(顺序读)、rndrd(随机读)、rndwr(随机写)、rndrw(随机读写)。
  • --file-io-mode=STRING:文件操作模式,可选的值有sync、async、mmap(默认值为sync)。
  • --file-extra-flags=STRING:在已打开文件上使用的额外标志,可选的值有sync、dsync、direct。
  • --file-fsync-freq=N:在发出这个参数指定数量的请求之后,执行fsync(),将内存中已修改的文件数据同步至存储设备中。若取值为0,则表示不使用fsync()(默认值为100)。
  • --file-fsync-all=[on|off]:每次写操作之后,执行fsync()(默认值为off)。
  • --file-fsync-end=[on|off]:在测试结束时,执行fsync()(默认值为on)。
  • --file-fsync-mode=STRING:同步数据时,使用哪种方法,可选的值有fsync、fdatasync(默认值为fsync)。
  • --file-merged-requests=N:如果可能,最多合并由这个参数指定数量的IO请求。若取值为0,则表示不会合并请求(默认值为0)。
  • --file-rw-ratio=N:进行读写综合测试时,读写操作的比率(默认值为1.5)。
CPU测试
cpu options:
  --cpu-max-prime=N upper limit for primes generator [10000]

这个测试将会执行cpu_max_prime的标准整除验证算法(从2到cpu_max_prime平方根,除去10的整数倍)

  • --cpu-max-prime=N:素数生成器的上限值(默认值为10000)。
内存测试
memory options:
  --memory-block-size=SIZE    size of memory block for test [1K]
  --memory-total-size=SIZE    total size of data to transfer [100G]
  --memory-scope=STRING       memory access scope {global,local} [global]
  --memory-hugetlb[=on|off]   allocate memory from HugeTLB pool [off]
  --memory-oper=STRING        type of memory operations {read, write, none} [write]
  --memory-access-mode=STRING memory access mode {seq,rnd} [seq]

这个测试将会分配一个memory-block-size大小的内存缓冲区,对其进行读写,直到达到memory-total-size数据量。

  • --memory-block-size=SIZE:测试使用的内存块的大小(默认值为1K)。
  • --memory-total-size=SIZE:需要传输的数据总大小(默认值为100G)。
  • --memory-scope=STRING:内存访问范围,可选的值有global、local(默认值为global)。
  • --memory-hugetlb=[on|off]:从HugeTLB池中分配内存(默认值为off)。
  • --memory-oper=STRING:内存操作的类型,可选的值有read、write、none(默认值为write)。
  • --memory-access-mode=STRING:内存访问模式,可选的值有seq(顺序)、rnd(随机)(默认值为seq)。
线程测试
threads options:
  --thread-yields=N number of yields to do per request [1000]
  --thread-locks=N  number of locks per thread [8]

这个测试将会执行thread-yields次"加thread-locks个锁-让出调度器-解锁"的循环。

  • --thread-yields=N:线程让出次数(默认值为1000)。线程将会执行加锁-让出-解锁的循环,循环次数为thread-yields次。
  • --thread-locks=N:每个线程使用的锁的数量(默认值为8)。
互斥锁测试
mutex options:
  --mutex-num=N   total size of mutex array [4096]
  --mutex-locks=N number of mutex locks to do per thread [50000]
  --mutex-loops=N number of empty loops to do outside mutex lock [10000]

这个测试将会从mutex-num大小的互斥锁池中取出互斥锁,每次取出后,执行mutex-loops全局变量递增操作,mutex-locks次。

  • --mutex-num=N:互斥锁数组的总大小(默认值为4096)。
  • --mutex-locks=N:执行每个线程时使用的互斥锁的数量(默认值为50000)。
  • --mutex-loops=N:在互斥锁内部执行的空循环的数量(默认值为10000)。
OLTP测试

内置支持的测试类型:bulk_insert,empty_test,oltp_common,oltp_delete,oltp_insert,oltp_point_select,oltp_read_only,oltp_read_write,oltp_update_index,oltp_update_non_index,oltp_write_only,prime-test,select_random_points,select_random_ranges

mysql options:
  --mysql-host=[LIST,...]          MySQL server host [localhost]
  --mysql-port=[LIST,...]          MySQL server port [3306]
  --mysql-socket=[LIST,...]        MySQL socket
  --mysql-user=STRING              MySQL user [sbtest]
  --mysql-password=STRING          MySQL password []
  --mysql-db=STRING                MySQL database name [sbtest]
  --mysql-ssl[=on|off]             use SSL connections, if available in the client library [off]
  --mysql-ssl-cipher=STRING        use specific cipher for SSL connections []
  --mysql-compression[=on|off]     use compression, if available in the client library [off]
  --mysql-debug[=on|off]           trace all client library calls [off]
  --mysql-ignore-errors=[LIST,...] list of errors to ignore, or "all" [1213,1020,1205]
  --mysql-dry-run[=on|off]         Dry run, pretend that all MySQL client API calls are successful without executing them [off]

可以通过(sysbench oltp_read_write help)命令获取对应test的详细信息。

参考资料
案例
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容