一、性能学习Ubuntu 安装mysql 我们在Ubuntu环境下装一个测试用的笔记MySQL来学习。 1.更新包 2.安装MySQL的性能学习服务端和客户端 在安装的过程中,需要输入数据库密码。笔记 3.启动Mysql 4.登录mysql 二、性能学习sysbench基准测试 sysbench是笔记一个开源的、模块化的性能学习、跨平台的笔记多线程性能测试工具,可以用来进行CPU、性能学习内存、笔记磁盘I/O、性能学习线程、笔记数据库的性能学习性能测试。目前支持的笔记数据库有MySQL、Oracle和PostgreSQL。性能学习当前功能允许测试的系统参数有: file I/O performance (文件I / O性能) scheduler performance (调度性能) memory allocation and transfer speed (内存分配和传输速度) POSIX threads implementation performance (POSIX线程执行绩效) database server performance (OLTP benchmark) (数据库服务器性能) 1.安装 Ubuntu系统可以直接apt,如: 在安装的时候出现了这样的错误:Could not get lock /var/lib/dpkg/lock 出现这个问题可能是有另外一个程序正在运行,导致资源被锁不可用。而导致资源被锁的原因可能是上次运行安装或更新时没有正常完成,香港云服务器进而出现此状况,解决的办法其实很简单: 在终端中敲入以下两句 再试着安装,问题解决。 2.通用选项说明 Usage: #使用方法 General options: #通用选项 --num-threads=N number of threads to use [1] #创建测试线程的数目。默认为1. --max-requests=N limit for total number of requests [10000] #请求的最大数目。默认为10000,0代表不限制。 --max-time=N limit for total execution time in seconds [0] #最大执行时间,单位是s。默认是0,不限制。 --forced-shutdown=STRING amount of time to wait after --max-time before forcing shutdown [off] #超过max-time强制中断。默认是off。 --thread-stack-size=SIZE size of stack per thread [32K] #每个线程的堆栈大小。默认是32K。 --init-rng=[on|off] initialize random number generator [off] #在测试开始时是否初始化随机数发生器。默认是off。 --test=STRING test to run #指定测试项目名称。 --debug=[on|off] print more debugging info [off] #是否显示更多的调试信息。默认是off。 --validate=[on|off] perform validation checks where possible [off] #在可能情况下执行验证检查。云服务器默认是off。 --help=[on|off] print help and exit #帮助信息。 --version=[on|off] print version and exit #版本信息。 Compiled-in tests: #测试项目 fileio - File I/O test #IO cpu - CPU performance test #CPU memory - Memory functions speed test #内存 threads - Threads subsystem performance test #线程 mutex - Mutex performance test #互斥性能测试 oltp - OLTP test # 数据库,事务处理 Commands: prepare:测试前准备工作; run:正式测试 cleanup:测试后删掉测试数据 help version See sysbench --test=<name> help for a list of options for each test. #查看每个测试项目的更多选项列表 更多选项: 1):sysbench --test=fileio help fileio options: --file-num=N 创建测试文件的数量。默认是128 --file-block-size=N 测试时文件块的大小。默认是16384(16K) --file-total-size=SIZE 测试文件的总大小。默认是2G --file-test-mode=STRING 文件测试模式{ seqwr(顺序写), seqrewr(顺序读写), seqrd(顺序读), rndrd(随机读), rndwr(随机写), rndrw(随机读写)} --file-io-mode=STRING 文件操作模式{ sync(同步),async(异步),fastmmap(快速map映射),slowmmap(慢map映射)}。默认是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 2):sysbench --test=cpu help --cpu-max-prime=N 最大质数发生器数量。默认是10000 3):sysbench --test=memory help memory options: --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 4):sysbench --test=threads help sysbench 0.4.12: multi-threaded system evaluation benchmark threads options: --thread-yields=N 每个请求产生多少个线程。默认是1000 --thread-locks=N 每个线程的锁的数量。默认是8 5):sysbench --test=mutex help mutex options: --mutex-num=N 数组互斥的总大小。默认是4096 --mutex-locks=N 每个线程互斥锁的数量。默认是50000 --mutex-loops=N 内部互斥锁的空循环数量。默认是10000 6): sysbench --test=oltp help oltp options: --oltp-test-mode=STRING 执行模式{ simple,complex(advanced transactional),nontrx(non-transactional),sp}。默认是complex --oltp-reconnect-mode=STRING 重新连接模式{ session(不使用重新连接。每个线程断开只在测试结束),transaction(在每次事务结束后重新连接),query(在每个SQL语句执行完重新连接),random(对于每个事务随机选择以上重新连接模式)}。默认是session --oltp-sp-name=STRING 存储过程的名称。默认为空 --oltp-read-only=[on|off] 只读模式。Update,delete,insert语句不可执行。默认是off --oltp-skip-trx=[on|off] 省略begin/commit语句。默认是off --oltp-range-size=N 查询范围。默认是100 --oltp-point-selects=N number of point selects [10] --oltp-simple-ranges=N number of simple ranges [1] --oltp-sum-ranges=N number of sum ranges [1] --oltp-order-ranges=N number of ordered ranges [1] --oltp-distinct-ranges=N number of distinct ranges [1] --oltp-index-updates=N number of index update [1] --oltp-non-index-updates=N number of non-index updates [1] --oltp-nontrx-mode=STRING 查询类型对于非事务执行模式{ select, update_key, update_nokey, insert, delete} [select] --oltp-auto-inc=[on|off] AUTO_INCREMENT是否开启。默认是on --oltp-connect-delay=N 在多少微秒后连接数据库。默认是10000 --oltp-user-delay-min=N 每个请求最短等待时间。单位是ms。默认是0 --oltp-user-delay-max=N 每个请求最长等待时间。单位是ms。默认是0 --oltp-table-name=STRING 测试时使用到的表名。默认是sbtest --oltp-table-size=N 测试表的记录数。默认是10000 --oltp-dist-type=STRING 分布的随机数{ uniform(均匀分布),Gaussian(高斯分布),special(空间分布)}。默认是special --oltp-dist-iter=N 产生数的迭代次数。默认是12 --oltp-dist-pct=N 值的百分比被视为special (for special distribution)。默认是1 --oltp-dist-res=N ‘special’的百分比值。默认是75 General database options: --db-driver=STRING 指定数据库驱动程序(help to get list of available drivers) --db-ps-mode=STRING编制报表使用模式{ auto, disable} [auto] Compiled-in database drivers: mysql options: --mysql-host=[LIST,...] MySQL server host [localhost] --mysql-port=N MySQL server port [3306] --mysql-socket=STRING MySQL socket --mysql-user=STRING MySQL user [sbtest] --mysql-password=STRING MySQL password [] --mysql-db=STRING MySQL database name [sbtest] --mysql-table-engine=STRING storage engine to use for the test table { myisam,innodb,bdb,heap,ndbcluster,federated} [innodb] --mysql-engine-trx=STRING whether storage engine used is transactional or not { yes,no,auto} [auto] --mysql-ssl=[on|off] use SSL connections, if available in the client library [off] --myisam-max-rows=N max-rows parameter for MyISAM tables [1000000] --mysql-create-options=STRING additional options passed to CREATE TABLE [] 3.测试线程 4.测试IO --num-threads 开启的线程 --file-total-size 总的文件大小 1,prepare阶段,生成需要的测试文件,完成后会在当前目录下生成很多小文件。 2,run阶段 下面的命令运行文件 I/O 混合随机读/写基准测试: 执行结果: 输出结果分析: 每秒请求数是:233.33 Requests/sec 吞吐量是:3.6458Mb/sec 清除运行文件: 5.清理测试时生成的文件 6.测试内存 7.测试mutex 8.测试OLTP 1,prepare阶段,生成需要的测试表 2,run阶段 3,清理测试时生成的测试表 三、服务器性能剖析 我们实际开发中,最常碰到三个性能相关的服务器请求: 1、如何确认服务器是否达到了性能最佳的状态; 2、找出某条语句为什么执行不够快 3、诊断被用户描述成“停顿”、“堆积”或“卡死”的某些间歇性疑难故障 找到慢的原因,我们才能够对症下药。 1、何为性能? 我们将性能定义为完成某件任务所需要的时间度量,换句话说,性能即响应时间。 完成一项任务所需要的时间可以分成两部分:执行时间和等待时间。 2、测量PHP应用程序 这里我们用一个PHP性能剖析工具,名叫ifP(instrumentation-for-php),代码托管在Google Code上(https://code.google.com/archi...),该工具可以更好的关注数据库的调用。所以当无法在数据库层面进行测试的时候,Ifp可以很好的帮助应用剖析数据库的利用率。Ifp是一个提供了计数器和计时器的单例类,很容易部署到生产环境中,因为不需要访问PHP的配置权限(因为对于很多开发人员来说,都没有访问PHP的配置的权限。)