腳本分享:Linux下磁盤(pán)io測(cè)試
這兩天在測(cè)試Dell的PS6000存儲(chǔ),在linux下也懶得去找測(cè)試工具了,就直接使用dd命令進(jìn)行I/O測(cè)試,但因?yàn)樾枰獪y(cè)試的數(shù)據(jù)很多,從512b到4M的塊,每一檔都要進(jìn)行讀寫(xiě)的測(cè)試,這要是人肉來(lái)做,那就太累了,于是就產(chǎn)生了下面這個(gè)腳本。
本腳本使用perl編寫(xiě),根據(jù)測(cè)試用例,完成每次讀或?qū)憸y(cè)試。
本腳本共有3個(gè)參數(shù),兩個(gè)是必選參數(shù),一個(gè)是可選參數(shù)
-r 測(cè)試讀性能
-w 測(cè)試寫(xiě)性能
-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";
}
# 檢查測(cè)試文件是否存在
sub check_file {
if ( !-e "./iotest" ) {
print "The test file dose not exist,please run write test first\n";
exit;
}
}
# 打開(kāi)測(cè)試用例
open FH, "<$cwd/iotest.json";
# Main #
while () {
chomp;
$json = new JSON;
# 將測(cè)試用例的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";
}
}
}
下面來(lái)看下測(cè)試用例的編寫(xiě)格式。這里蚊子采用了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"}
該文件主要就分兩個(gè)字段,冒號(hào)前的是塊大小,冒號(hào)后的count數(shù),通過(guò)塊大小和count數(shù)就能創(chuàng)建文件,蚊子這里統(tǒng)一生成的是2G大小的文件。該文件保存文件名為iotest.json,將測(cè)試用例和測(cè)試腳本放到同一個(gè)目錄下即可。使用方面很簡(jiǎn)單,進(jìn)入到要測(cè)試的磁盤(pán)或目錄下,執(zhí)行
#perl /dir/to/iotest.pl –w
即可完成開(kāi)啟文件系統(tǒng)換的磁盤(pán)寫(xiě)測(cè)試。程序執(zhí)行完畢后會(huì)在當(dāng)前目錄下創(chuàng)建.log文件用于記錄測(cè)試結(jié)果,下圖是蚊子測(cè)試的結(jié)果,我做成了表格。

原文:http://www.wenzizone.cn/?p=405
【編輯推薦】
- Linux性能監(jiān)測(cè):磁盤(pán)IO篇
- 詳解iostat -dx 1命令監(jiān)控IO性能
- 性能優(yōu)化的技巧
- iostat來(lái)對(duì)linux硬盤(pán)IO性能進(jìn)行檢測(cè)























