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

发表于:2011-9-07 10:24

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:深夜的蚊子    来源:51Testing软件测试网采编

  这两天在测试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";
}
}
}

  下面来看下测试用例的编写格式。这里蚊子采用了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文件用于记录测试结果,下图是蚊子测试的结果,我做成了表格。

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号