使用mysqlslap对mysql进行压测,观察Azure虚拟机cpu使用率

一直想做这个测试,原因很简单,很多人一直比较怀疑Azure的虚拟机性能,说相同的配置凭啥比阿里的虚拟机贵那么多,其实,我自己以前也怀疑过,但是接触Azure的几个月,确实发现Azure的虚拟机性能真的很强悍,在这里笔者举个例子说明,有个客户(这里就不透露名字了)在本地数据中心cpu使用率一直在60%以上,因为我们上云都是先平移,如果不够用我们再auto sacle,但是客户却很不放心,非要高配的机器,在我们的百般劝说之下好不容易才答应先平移(客户表示很委屈,为什么我要高配的机器你不给,哈哈,开个玩笑),最后测试下来的结果发现cpu平时使用率在百分之二十多,周末最繁忙的时刻也没超过百分之五十,客户当场就说没想到Azure虚拟机性能这么优越,同时也很感谢我们没有坑他,在本次场景中,其实性能的瓶颈并不在vm,而是在于数据库,因为mysql是轻量级数据库不同于Oracle!!!

在本次测试环境中,我们在中国东部建立一台vm,考虑到数据库对虚拟机性能要求较高,这里我们以A6为例(要记得先建立高配的机型,然后再降为A6虚拟机!!!),并挂载1T的硬盘。

第一步建立虚拟机,过程略,结果如下

 在这里,笔者想说一句,由于本次poc环境中,我们需要直观地看到cpu使用率,考虑到linux的ps命令的局限性,而且ARM里面很难看到cpu使用率,所以笔者选用ASM环境来建立虚拟机。

接下来我们先记录一下cpu与内存的使用情况

接下来第一步,添加附加磁盘

第二步,挂载附加磁盘

ssh远程登陆到该虚拟机

1.切换到root用户

[yangyang@mysqlvm1 ~]$ sudo su - root
[root@mysqlvm1 ~]# 

切换成功

2.查看

[root@mysqlvm1 ~]# fdisk -cul /dev/sdc

Disk /dev/sdc: 1098.4 GB, 1098437885952 bytes
255 heads, 63 sectors/track, 133544 cylinders, total 2145386496 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

3.分区

[root@mysqlvm1 ~]# fdisk -cu /dev/sdc
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x9694814d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): p

Disk /dev/sdc: 1098.4 GB, 1098437885952 bytes
255 heads, 63 sectors/track, 133544 cylinders, total 2145386496 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9694814d

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-2145386495, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2145386495, default 2145386495): 
Using default value 2145386495

Command (m for help): p

Disk /dev/sdc: 1098.4 GB, 1098437885952 bytes
255 heads, 63 sectors/track, 133544 cylinders, total 2145386496 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9694814d

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048  2145386495  1072692224   83  Linux

Command (m for help): w

4.格式化

查看帮助文件

[root@mysqlvm1 ~]# man mkfs.ext4

找到lazy

  lazy_itable_init[= <0 to disable, 1 to enable>]
                          If enabled and the uninit_bg feature is enabled, the
                          inode table will not be fully initialized by mke2fs.
                          This speeds up filesystem initialization noticeably,
                          but it requires the kernel  to  finish  initializing
                          the filesystem in the background when the filesystem
                          is first mounted.  If the option value  is  omitted,
                          it defaults to 1 to enable lazy inode table initial-
                          ization.

快速格式化

[root@mysqlvm1 ~]# mkfs.ext4 -E lazy_itable_init /dev/sdc1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
67043328 inodes, 268173056 blocks
13408652 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
8184 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000, 214990848

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

5.创建挂载点并挂载

[root@mysqlvm1 ~]#mkdir /1T
[root@mysqlvm1 ~]#mount /dev/sdc1 /1T

第三步,安装mysql,这里选择yum安装

[root@mysqlvm1 dev]# cd /1T/
[root@mysqlvm1 1T]# yum -y install mysql-server

看到如下结果

Installed:
  mysql-server.x86_64 0:5.1.73-7.el6                                            

Dependency Installed:
  mysql.x86_64 0:5.1.73-7.el6        perl-DBD-MySQL.x86_64 0:4.013-3.el6       

Dependency Updated:
  mysql-libs.x86_64 0:5.1.73-7.el6                                              

Complete!

设置开机启动

[root@mysqlvm1 ~]# chkconfig mysqld on

启动mysql服务

[root@mysqlvm1 ~]# service mysqld start

设置mysql root用户以及密码

[root@mysqlvm1 ~]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, 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.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | mysqlvm1  |          |
| root | 127.0.0.1 |          |
|      | localhost |          |
|      | mysqlvm1  |          |
+------+-----------+----------+
5 rows in set (0.00 sec)

mysql> set password for root@localhost=password('root');
Query OK, 0 rows affected (0.00 sec)

mysql> exit

用户root,密码root

用root来登陆

[root@mysqlvm1 ~]# mysql -u root -p

以下是mysql的一些常见命令

show databases; //查看系统已存在的数据库
use databasesname;   //选择需要使用的数据库
drop database databasename; //删除选定的数据库
exit    //退出数据库的连接
create database test01;    //建立名为test的数据库
show tables;        // 列出当前数据库下的表
其他基本的增删改查使用标准SQL即可

第四步,进行压测

mysqlslap是mysql自带的基准测试工具,可以模拟多个并发连接数来向服务器发起多次查询请求,这样可以观察CPU的情况

单线程测试

[root@mysqlvm1 ~]# mysqlslap -a -uroot -proot
Benchmark
        Average number of seconds to run all queries: 0.001 seconds
        Minimum number of seconds to run all queries: 0.001 seconds
        Maximum number of seconds to run all queries: 0.001 seconds
        Number of clients running queries: 1
        Average number of queries per client: 0

多线程测试(100)

[root@mysqlvm1 ~]# mysqlslap -a -c 100 -uroot -proot
Benchmark
        Average number of seconds to run all queries: 0.193 seconds
        Minimum number of seconds to run all queries: 0.193 seconds
        Maximum number of seconds to run all queries: 0.193 seconds
        Number of clients running queries: 100
        Average number of queries per client: 0

迭代测试

[root@mysqlvm1 ~]# mysqlslap -a -i 10 -uroot -proot
Benchmark
        Average number of seconds to run all queries: 0.001 seconds
        Minimum number of seconds to run all queries: 0.001 seconds
        Maximum number of seconds to run all queries: 0.001 seconds
        Number of clients running queries: 1
        Average number of queries per client: 0

每个客户端的并发连接数*每个客户端查询的次数=总的查询次数

一下是mysqlslap的常见语法

# mysqlslap [options]

常用参数 [options] 详细说明:

--auto-generate-sql, -a 自动生成测试表和数据,表示用mysqlslap工具自己生成的SQL脚本来测试并发压力。
--auto-generate-sql-load-type=type 测试语句的类型。代表要测试的环境是读操作还是写操作还是两者混合的。取值包括:read,key,write,update和mixed(默认)。
--auto-generate-sql-add-auto-increment 代表对生成的表自动添加auto_increment列,从5.1.18版本开始支持。
--number-char-cols=N, -x N 自动生成的测试表中包含多少个字符类型的列,默认1
--number-int-cols=N, -y N 自动生成的测试表中包含多少个数字类型的列,默认1
--number-of-queries=N 总的测试查询次数(并发客户数×每客户查询次数)
--query=name,-q 使用自定义脚本执行测试,例如可以调用自定义的一个存储过程或者sql语句来执行测试。
--create-schema 代表自定义的测试库名称,测试的schema,MySQL中schema也就是database。
--commint=N 多少条DML后提交一次。
--compress, -C 如果服务器和客户端支持都压缩,则压缩信息传递。
--concurrency=N, -c N 表示并发量,也就是模拟多少个客户端同时执行select。可指定多个值,以逗号或者--delimiter参数指定的值做为分隔符。例如:--concurrency=100,200,500。
--engine=engine_name, -e engine_name 代表要测试的引擎,可以有多个,用分隔符隔开。例如:--engines=myisam,innodb。
--iterations=N, -i N 测试执行的迭代次数,代表要在不同并发环境下,各自运行测试多少次。
--only-print 只打印测试语句而不实际执行。
--detach=N 执行N条语句后断开重连。
--debug-info, -T 打印内存和CPU的相关信息。

接下来我们来执行一条更复杂的测试语句

并发分别为50,100并发,1000次查询,并进行迭代测试

[yangyang@mysqlvm1 ~]$ mysqlslap -a --concurrency=50,100 --number-of-queries 100
0 --iterations=5 --debug-info -uroot -proot
Benchmark
        Average number of seconds to run all queries: 0.147 seconds
        Minimum number of seconds to run all queries: 0.132 seconds
        Maximum number of seconds to run all queries: 0.163 seconds
        Number of clients running queries: 50
        Average number of queries per client: 20

Benchmark
        Average number of seconds to run all queries: 0.196 seconds
        Minimum number of seconds to run all queries: 0.154 seconds
        Maximum number of seconds to run all queries: 0.244 seconds
        Number of clients running queries: 100
        Average number of queries per client: 10


User time 0.62, System time 1.02
Maximum resident set size 30164, Integral resident set size 0
Non-physical pagefaults 10837, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 29785, Involuntary context switches 1271

接下来我们来测试一个更大的查询次数,极端情况下,30000次查询,并减小并发量为20,50

[yangyang@mysqlvm1 ~]$ mysqlslap -a --concurrency=20,50 --number-of-queries 30000 --iterations=5 --debug-info -uroot -proot
Benchmark
        Average number of seconds to run all queries: 60.339 seconds
        Minimum number of seconds to run all queries: 55.709 seconds
        Maximum number of seconds to run all queries: 63.855 seconds
        Number of clients running queries: 20
        Average number of queries per client: 1500

Benchmark
        Average number of seconds to run all queries: 47.620 seconds
        Minimum number of seconds to run all queries: 46.899 seconds
        Maximum number of seconds to run all queries: 48.904 seconds
        Number of clients running queries: 50
        Average number of queries per client: 600


User time 406.27, System time 781.33
Maximum resident set size 126940, Integral resident set size 0
Non-physical pagefaults 38944979, Physical pagefaults 0, Swaps 0
Blocks in 0 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 5415184, Involuntary context switches 653795

我们到portal中去观察结果

我们看到cpu的峰值使用情况达到了95.97%,这个时候我们再升到D12V2,执行相同的操作,看看结果如何

[yangyang@mysqlvm1 ~]$  mysqlslap -a --concurrency=20,50 --number-of-queries 30000 --iterations=5 --debug-info -uroot -proot
Benchmark
        Average number of seconds to run all queries: 18.069 seconds
        Minimum number of seconds to run all queries: 17.928 seconds
        Maximum number of seconds to run all queries: 18.223 seconds
        Number of clients running queries: 20
        Average number of queries per client: 1500

Benchmark
        Average number of seconds to run all queries: 16.130 seconds
        Minimum number of seconds to run all queries: 15.414 seconds
        Maximum number of seconds to run all queries: 18.134 seconds
        Number of clients running queries: 50
        Average number of queries per client: 600


User time 174.35, System time 100.30
Maximum resident set size 94236, Integral resident set size 0
Non-physical pagefaults 30137497, Physical pagefaults 6, Swaps 0
Blocks in 1832 out 0, Messages in 0 out 0, Signals 0
Voluntary context switches 14059308, Involuntary context switches 10329

我们回到portal里面来观察结果,我们去新portal去看,这里为了结果更准确,我执行了两次,所以第二幅图有两个峰值,这样结果更直观。

 从上图可以看出,cpu最高也就在百分之五十左右,对于一般系统而言,前端不可能对后端有如此大的压力,即使在这种极端情况下,我们的虚拟机依然能承受,即使A6虚拟机也能承受,但是在这种极端情况我们不建议使用A系列虚拟机,D系列虚拟机性能比A系列高了百分之三十以上,再次证明,Azure的虚拟机性能是不容置疑的,本来笔者打算在linux系统中使用ps和top命令来查看cpu进程的,但是发现效果不是很良好,所以还是portal观察,但是portal的数据是有滞后性的,所以大家在观察的时候需要等待一会儿,一般十五分钟左右,本来我是想再测试一下DS系列虚拟机,但是由于普通存储盘的虚拟机无法升级到ssd的,所以就没有再去测试,如果有读者有兴趣可以去测试一下!!!

 

  

posted @ 2016-12-27 17:39  yangyangchenjie  阅读(466)  评论(0编辑  收藏  举报