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

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

 这两天在测试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文件用于记录测试结果,下图是蚊子测试的结果,我做成了表格。

 

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

【编辑推荐】

  1. Linux性能监测:磁盘IO篇
  2. 详解iostat -dx 1命令监控IO性能
  3. 性能优化的技巧
  4. iostat来对linux硬盘IO性能进行检测

 

责任编辑:黄丹 来源: 蚊子空间
相关推荐

2019-03-28 08:00:00

Linux磁盘IO监控存储设备

2010-12-21 09:58:37

Linux脚本自动关机任务管理

2019-08-23 06:22:47

LinuxShell监控脚本

2010-10-09 09:18:59

Shell脚本

2011-11-08 21:51:43

Linux 监控 IO

2010-12-22 13:17:47

Linux性能监测磁盘IO

2021-06-21 11:11:29

LinuxIO磁盘IO

2019-07-25 07:14:03

LinuxSync操作系统

2013-08-15 14:10:24

云主机磁盘IO

2010-05-27 17:51:55

Linux查看磁盘空间

2021-02-21 11:48:30

内存磁盘IO

2022-03-01 20:26:12

PythonCSV脚本

2021-09-30 07:26:15

磁盘IO网络

2010-09-06 14:14:32

ppp-on

2023-12-20 14:38:50

Linux磁盘IO

2019-08-12 07:45:44

Linux脚本shell

2017-06-16 15:18:15

虚拟化WindowsIO

2009-09-07 09:41:16

2010-12-17 09:52:32

bash命令

2010-11-15 11:21:31

Oracle启动脚本
点赞
收藏

51CTO技术栈公众号