MySQL Study之--MySQL压力测试工具mysqlslap

MySQL Study之--MySQL压力测试工具mysqlslap
一、Mysqlslap介绍
     mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和查询数据,语法简单,灵活,容易使用。该工具可以模拟多个客户端同时并发的向服务器发出查询更新,给出了性能测试数据而且提供了多种引擎的性能比较。mysqlslap为mysql性能优化前后提供了直观的验证依据,笔者建议 系统运维人员应该掌握一些常见的压力测试工具,这样才能较为准确的掌握线上系统能够支撑的用户流量上限及其抗压性等问题。  www.2cto.com  
二、使用方法介绍
可以使用mysqlslap --help来显示使用方法:
1) --concurrency代表并发数量,多个可以用逗号隔开,例如:concurrency=10,50,100, 并发连接线程数分别是10、50、100个并发。
2) --engines代表要测试的引擎,可以有多个,用分隔符隔开。
3) --iterations代表要运行这些测试多少次。
4) --auto-generate-sql 代表用系统自己生成的SQL脚本来测试。
5)--auto-generate-sql-load-type 代表要测试的是读还是写还是两者混合的(read,write,update,mixed)
6) --number-of-queries 代表总共要运行多少次查询。每个客户端运行的查询数量可以用查询总数/并发数来计算。
7) --debug-info 代表要额外输出CPU以及内存的相关信息。
8) --number-int-cols :创建测试表的 int 型字段数量
9) --auto-generate-sql-add-autoincrement : 代表对生成的表自动添加auto_increment列,从5.1.18版本开始
10) --number-char-cols 创建测试表的 char 型字段数量。
11) --create-schema 测试的schema,MySQL中schema也就是database。
12) --query  使用自定义脚本执行测试,例如可以调用自定义的一个存储过程或者sql语句来执行测试。
13) --only-print 如果只想打印看看SQL语句是什么,可以用这个选项。
二、案例分析
[root@mysrv ~]# mysqlslap -V
mysqlslap  Ver 1.0 Distrib 5.1.66, for redhat-linux-gnu (x86_64)
[root@mysrv ~]# mysqlslap --help
mysqlslap  Ver 1.0 Distrib 5.1.66, for redhat-linux-gnu (x86_64)
Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Run a query multiple times against the server.
Usage: mysqlslap [OPTIONS]
Default options are read from the following files in the given order:
/etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf 
The following groups are read: mysqlslap client
The following options may be given as the first argument:
--print-defaults        Print the program argument list and exit.
--no-defaults           Don't read default options from any option file.
--defaults-file=#       Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
  -?, --help          Display this help and exit.
  -a, --auto-generate-sql 
                      Generate SQL where not supplied by file or command line.
  --auto-generate-sql-add-autoincrement 
                      Add an AUTO_INCREMENT column to auto-generated tables.
  --auto-generate-sql-execute-number=# 
                      Set this number to generate a set number of queries to
                      run.
  --auto-generate-sql-guid-primary 
                      Add GUID based primary keys to auto-generated tables.
  --auto-generate-sql-load-type=name 
                      Specify test load type: mixed, update, write, key, or
                      read; default is mixed.
  --auto-generate-sql-secondary-indexes=# 
                      Number of secondary indexes to add to auto-generated
                      tables.
  --auto-generate-sql-unique-query-number=# 
                      Number of unique queries to generate for automatic tests.
  --auto-generate-sql-unique-write-number=# 
                      Number of unique queries to generate for
                      auto-generate-sql-write-number.
  --auto-generate-sql-write-number=# 
                      Number of row inserts to perform for each thread (default
                      is 100).
  --commit=#          Commit records every X number of statements.
  -C, --compress      Use compression in server/client protocol.
  -c, --concurrency=name 
                      Number of clients to simulate for query to run.
  --create=name       File or string to use create tables.
  --create-schema=name 
                      Schema to run tests in.
  --csv[=name]        Generate CSV output to named file or to stdout if no file
                      is named.
  -#, --debug[=#]     This is a non-debug version. Catch this and exit.
  --debug-check       Check memory and open file usage at exit.
  -T, --debug-info    Print some debug info at exit.
  -F, --delimiter=name 
                      Delimiter to use in SQL statements supplied in file or
                      command line.
  --detach=#          Detach (close and reopen) connections after X number of
                      requests.
  -e, --engine=name   Storage engine to use for creating the table.
  -h, --host=name     Connect to host.
  -i, --iterations=#  Number of times to run the tests.
  --no-drop           Do not drop the schema after the test.
  -x, --number-char-cols=name 
                      Number of VARCHAR columns to create in table if
                      specifying --auto-generate-sql.
  -y, --number-int-cols=name 
                      Number of INT columns to create in table if specifying
                      --auto-generate-sql.
  --number-of-queries=# 
                      Limit each client to this number of queries (this is not
                      exact).
  --only-print        Do not connect to the databases, but instead print out
                      what would have been done.
  -p, --password[=name] 
                      Password to use when connecting to server. If password is
                      not given it's asked from the tty.
  -P, --port=#        Port number to use for connection.
  --post-query=name   Query to run or file containing query to execute after
                      tests have completed.
  --post-system=name  system() string to execute after tests have completed.
  --pre-query=name    Query to run or file containing query to execute before
                      running tests.
  --pre-system=name   system() string to execute before running tests.
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
                      memory).
  -q, --query=name    Query to run or file containing query to run.
  -s, --silent        Run program in silent mode - no output.
  -S, --socket=name   The socket file to use for connection.
  --ssl               Enable SSL for connection (automatically enabled with
                      other flags).Disable with --skip-ssl.
  --ssl-ca=name       CA file in PEM format (check OpenSSL docs, implies
                      --ssl).
  --ssl-capath=name   CA directory (check OpenSSL docs, implies --ssl).
  --ssl-cert=name     X509 cert in PEM format (implies --ssl).
  --ssl-cipher=name   SSL cipher to use (implies --ssl).
  --ssl-key=name      X509 key in PEM format (implies --ssl).
  --ssl-verify-server-cert 
                      Verify server's "Common Name" in its cert against
                      hostname used when connecting. This option is disabled by
                      default.
  -u, --user=name     User for login if not current user.
  -v, --verbose       More verbose output; you can use this multiple times to
                      get even more verbose output.
  -V, --version       Output version information and exit.
案例一:
[root@mysrv ~]#  mysqlslap -uroot -poracle --concurrency=100 --iterations=1 --auto-generate-sql --auto-generate-sql-load-type=mixed --auto-generate-sql-add-autoincrement --engine=myisam --number-of-queries=10 --debug-info</span></strong>
测试信息:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 0.473 seconds
        Minimum number of seconds to run all queries: 0.473 seconds
        Maximum number of seconds to run all queries: 0.473 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0
User time 0.04, System time 0.14
Maximum resident set size 6732, Integral resident set size 0
Non-physical pagefaults 1333, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 2389, Involuntary context switches 48
备注: 本次测试以100个并发线程、测试1次,自动生成SQL测试脚本、读、写、更新混合测试、自增长字段、测试引擎为myisam、共运行10次查询,输出cpu资源信息
案例二:
[root@mysrv ~]# mysqlslap -h127.0.0.1 -P3306 -u root -poracle --concurrency=100 --iterations=1 --create-schema='prod' --query='select * from t1;' --number-of-queries=10 -e innodb --debug-info 
测试信息:
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 1.455 seconds
        Minimum number of seconds to run all queries: 1.455 seconds
        Maximum number of seconds to run all queries: 1.455 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0
User time 0.03, System time 0.12
Maximum resident set size 5420, Integral resident set size 0
Non-physical pagefaults 1273, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 536, Involuntary context switches 151

备注:使用mysqlslap指定sql语句进行测试
案例三:
[root@mysrv ~]# mysqlslap --concurrency=50,100,200 --iterations=20 --number-int-cols=4 --number-char-cols=35 --auto-generate-sql --auto-generate-sql-add-autoincrement --auto-generate-sql-load-type=read --engine=myisam,innodb --number-of-queries=200 --verbose --socket=/tmp/mysql.sock -uroot -poracle
测试信息:
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 1.521 seconds
        Minimum number of seconds to run all queries: 1.397 seconds
        Maximum number of seconds to run all queries: 2.092 seconds
        Number of clients running queries: 50
        Average number of queries per client: 4
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 1.465 seconds
        Minimum number of seconds to run all queries: 1.279 seconds
        Maximum number of seconds to run all queries: 1.996 seconds
        Number of clients running queries: 100
        Average number of queries per client: 2
Benchmark
        Running for engine myisam
        Average number of seconds to run all queries: 1.367 seconds
        Minimum number of seconds to run all queries: 1.260 seconds
        Maximum number of seconds to run all queries: 1.576 seconds
        Number of clients running queries: 200
        Average number of queries per client: 1
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 1.198 seconds
        Minimum number of seconds to run all queries: 1.084 seconds
        Maximum number of seconds to run all queries: 1.301 seconds
        Number of clients running queries: 50
        Average number of queries per client: 4
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 1.423 seconds
        Minimum number of seconds to run all queries: 1.238 seconds
        Maximum number of seconds to run all queries: 1.761 seconds
        Number of clients running queries: 100
        Average number of queries per client: 2
Benchmark
        Running for engine innodb
        Average number of seconds to run all queries: 1.389 seconds
        Minimum number of seconds to run all queries: 1.268 seconds
        Maximum number of seconds to run all queries: 1.653 seconds
        Number of clients running queries: 200
        Average number of queries per client: 1
备注:
    #系统脚本测试,增加int型 4列char 型35列,测试2种引擎myisam,innodb读的性能,分别用50,100,200个客户端对服务器进行测试总共200个查询语句 执行20次查询

你可能感兴趣的:(mysql)