腳本分享:Linux下磁盤io測試
這兩天在測試Dell的PS6000存儲,在linux下也懶得去找測試工具了,就直接使用dd命令進(jìn)行I/O測試,但因?yàn)樾枰獪y試的數(shù)據(jù)很多,從512b到4M的塊,每一檔都要進(jìn)行讀寫的測試,這要是人肉來做,那就太累了,于是就產(chǎn)生了下面這個腳本。
本腳本使用perl編寫,根據(jù)測試用例,完成每次讀或?qū)憸y試。
本腳本共有3個參數(shù),兩個是必選參數(shù),一個是可選參數(shù)
-r 測試讀性能
-w 測試寫性能
-c 可選參數(shù),用于關(guān)閉文件系統(tǒng)的緩存
#!/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"); } # 獲得參數(shù) 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格式轉(zhuǎn)成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讀取,文件內(nèi)容如下
{"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數(shù),通過塊大小和count數(shù)就能創(chuàng)建文件,蚊子這里統(tǒng)一生成的是2G大小的文件。該文件保存文件名為iotest.json,將測試用例和測試腳本放到同一個目錄下即可。使用方面很簡單,進(jìn)入到要測試的磁盤或目錄下,執(zhí)行
#perl /dir/to/iotest.pl –w
即可完成開啟文件系統(tǒng)換的磁盤寫測試。程序執(zhí)行完畢后會在當(dāng)前目錄下創(chuàng)建.log文件用于記錄測試結(jié)果,下圖是蚊子測試的結(jié)果,我做成了表格。

原文:http://www.wenzizone.cn/?p=405
【編輯推薦】