脚本分享:Linux下磁盘io测试

这两天在测试Dell的PS6000存储,在linux下也懒得去找测试工具了,就直接使用dd命令进行I/O测试,但因为需要测试的数据很多,从512b到4M的块,每一档都要进行读写的测试,这要是人肉来做,那就太累了,于是就产生了下面这个脚本。本脚本使用perl编写,根据测试用例,完成每次读或写测试。
首页 新闻资讯 行业资讯 脚本分享:Linux下磁盘io测试

 这两天在测试Dell的PS6000存储,在linux下也懒得去找测试工具了,就直接使用dd命令进行I/O测试,但因为需要测试的数据很多,从512b到4M的块,每一档都要进行读写的测试,这要是人肉来做,那就太累了,于是就产生了下面这个脚本。

本脚本使用perl编写,根据测试用例,完成每次读或写测试。

本脚本共有3个参数,两个是必选参数,一个是可选参数

-r 测试读性能

-w 测试写性能

-c 可选参数,用于关闭文件系统的缓存

复制

#!/bin/evn perl
use 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 Option\n";
&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 Option\n";
&usage;
exit;
}
}
my $opt = $ARGV[0];
# 使用帮助
sub usage {
print "Usage: iotest.pl [OPTION] [OPTION]\n";
print "\n";
print "-r\texecute read test\n";
print "-w\texecute write test\n";
print "-c\tiotest with local filesystem cache\n\n";
}
# 检查测试文件是否存在
sub check_file {
if ( !-e "./iotest" ) {
print "The test file dose not exist,please run write test first\n";
exit;
}
}
# 打开测试用例
open FH, "<$cwd/iotest.json";
# Main #
while () {
chomp;
$json = new JSON;
# 将测试用例的json格式转成hash
my %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: $key\n";
`$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: $key\n";
`$cmd`;
print wfh "\n";
}
}
}
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

下面来看下测试用例的编写格式。这里蚊子采用了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"}
  • 1.

该文件主要就分两个字段,冒号前的是块大小,冒号后的count数,通过块大小和count数就能创建文件,蚊子这里统一生成的是2G大小的文件。该文件保存文件名为iotest.json,将测试用例和测试脚本放到同一个目录下即可。使用方面很简单,进入到要测试的磁盘或目录下,执行

复制

#perl /dir/to/iotest.pl –w
  • 1.

即可完成开启文件系统换的磁盘写测试。程序执行完毕后会在当前目录下创建.log文件用于记录测试结果,下图是蚊子测试的结果,我做成了表格。

 



原文:http://www.wenzizone.cn/?p=405

【编辑推荐】

  1. Linux性能监测:磁盘IO篇

  2. 详解iostat -dx 1命令监控IO性能

  3. 性能优化的技巧

  4. iostat来对linux硬盘IO性能进行检测

 

24    2011-09-02 10:29:30    脚本 Linux perl