这两天在测试Dell的PS6000存储,在linux下也懒得去找测试工具了,就直接使用dd命令进行I/O测试,但因为需要测试的数据很多,从512b到4M的块,每一档都要进行读写的测试,这要是人肉来做,那就太累了,于是就产生了下面这个脚本。
本脚本使用perl编写,根据测试用例,完成每次读或写测试。
本脚本共有3个参数,两个是必选参数,一个是可选参数
-r 测试读性能
-w 测试写性能
-c 可选参数,用于关闭文件系统的缓存
#!/bin/evn perluse JSON;use Data::Dumper;use Cwd;use File::Basename;our $cache_flag = '';# 获取脚本所在目录my $cwd;if ( $0 =~ m{^/} ) {$cwd = dirname($0);}else {my $dir = getcwd();$cwd = dirname("$dir/$0");}# 获得参数if ( @ARGV < 1 ) {&usage;exit;}elsif ( @ARGV == 1 ) {if ( ( $ARGV[0] ne "-r" ) && ( $ARGV[0] ne "-w" ) ) {print "Unrecognized Optionn";&usage;exit;}}elsif ( @ARGV == 2 ) {if ( $ARGV[0] eq "-r" ) {$cache_flag = "iflag=direct,nonblock";}elsif ( $ARGV[0] eq "-w" ) {$cache_flag = "oflag=direct,nonblock";}elsif ( ( $ARGV[0] ne "-r" ) || ( $ARGV[0] ne "-w" ) ) {print "Unrecognized Optionn";&usage;exit;}}my $opt = $ARGV[0];# 使用帮助sub usage {print "Usage: iotest.pl [OPTION] [OPTION]n";print "n";print "-rtexecute read testn";print "-wtexecute write testn";print "-ctiotest with local filesystem cachenn";}# 检查测试文件是否存在sub check_file {if ( !-e "./iotest" ) {print "The test file dose not exist,please run write test firstn";exit;}}# 打开测试用例open FH, "<$cwd/iotest.json";# Main #while () {chomp;$json = new JSON;# 将测试用例的json格式转成hashmy %strings = %{ $json->decode($_) };if ( $opt eq "-r" ) {&check_file;while ( ( $key, $value ) = each %strings ) {open rfh, '>>iotestr.log';my $cmd ="/bin/dd if=./iotest of=/dev/null bs=$key count=$value $cache_flag >> iotestr.log 2>&1";print rfh "IOTest Block: $keyn";`$cmd`;print rfh "n";}}elsif ( $opt eq "-w" ) {while ( ( $key, $value ) = each %strings ) {open wfh, '>>iotestw.log';my $cmd ="/bin/dd if=/dev/zero of=./iotest bs=$key count=$value $cache_flag >> iotestw.log 2>&1";print wfh "IOTest Block: $keyn";`$cmd`;print wfh "n";}}}
下面来看下测试用例的编写格式。这里蚊子采用了json串的方式,这样便于perl读取,文件内容如下
{"512":"4096000","1K":"2048000","2K":"1024000","4K":"512000","8K":"256000","16K":"128000","32K":"64000","64k":"32000","128k":"16000","256k":"8000","512k":"4000","1M":"2000","2M":"1000","4M":"500"}
该文件主要就分两个字段,冒号前的是块大小,冒号后的count数,通过块大小和count数就能创建文件,蚊子这里统一生成的是2G大小的文件。该文件保存文件名为iotest.json,将测试用例和测试脚本放到同一个目录下即可。使用方面很简单,进入到要测试的磁盘或目录下,执行
#perl /dir/to/iotest.pl –w
即可完成开启文件系统换的磁盘写测试。程序执行完毕后会在当前目录下创建.log文件用于记录测试结果,下图是蚊子测试的结果,我做成了表格。

原文:http://www.wenzizone.cn/?p=405
【编辑推荐】
- Linux性能监测:磁盘IO篇
- 详解iostat -dx 1命令监控IO性能
- 性能优化的技巧
- iostat来对linux硬盘IO性能进行检测
推荐阅读
业务和管理需求使得数据中心管理和灾难恢复的缺陷更加明显。二十年前,用货车运输磁带进行存储就能满足需求了。十年前,两个数据中心的距离只要能进行I/O,就能满足需求。现在,随着电子商务成为首要的负载,恢复计划>>>详细阅读
本文标题:脚本分享:Linux下磁盘io测试
地址:http://www.17bianji.com/yunwei/733.html
1/2 1