Skip to content

Instantly share code, notes, and snippets.

@kazeburo
Created Mar 15, 2018
Embed
What would you like to do?
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
sub cap_cmd {
my ($cmdref) = @_;
pipe my $logrh, my $logwh
or die "Died: failed to create pipe:$!\n";
my $pid = fork;
if ( ! defined $pid ) {
die "Died: fork failed: $!\n";
}
elsif ( $pid == 0 ) {
#child
close $logrh;
open STDOUT, '>&', $logwh
or die "Died: failed to redirect STDOUT\n";
close $logwh;
exec @$cmdref;
die "Died: exec failed: $!\n";
}
close $logwh;
my $result;
while(<$logrh>){
$result .= $_;
}
close $logrh;
while (wait == -1) {}
my $exit_code = $?;
$exit_code = $exit_code >> 8;
return ($result, $exit_code);
}
sub byte_human_read {
my $byte = shift;
if ($byte > 1024 * 1024 * 1024 * 1024 * 100) {
return sprintf("%.1fTB",$byte/(1024 * 1024 * 1024 * 1024));
} elsif ($byte > 1024 * 1024 * 1024 * 100) {
return sprintf("%.1fGB",$byte/(1024 * 1024 * 1024));
} elsif ($byte > 1024 * 1024 * 100) {
return sprintf("%.1fMB",$byte/(1024 * 1024));
} elsif ($byte > 1024 * 100) {
return sprintf("%.1fKB",$byte/1024);
}
return sprintf("%.1fB",$byte);
}
my @fio_cmd = @ARGV;
my $fio;
for ( split /:/, $ENV{PATH} ) {
if ( -x "$_/fio" ) {
$fio = "$_/fio";
last;
}
}
die "Died: couldnot find fio" unless $fio;
unshift @fio_cmd, $fio;
push @fio_cmd, '--output-format=json';
(my $res,my $exit_code) = cap_cmd(\@fio_cmd);
if ( $exit_code != 0 ) {
exit $exit_code;
}
my @data = eval {
my $data = JSON::decode_json($res);
my $mode = $data->{"global options"}->{"rw"};
$mode =~ s/rand//;
# read : io=33047MB, bw=3304.2MB/s, iops=211457, runt= 10002msec
printf "FIO : rw=%s, bs=%sB\n", $data->{"global options"}->{"rw"}, $data->{"global options"}->{"bs"};
printf " read : io=%s, bw=%s/s, iops=%d runt=%sms\n",
byte_human_read($data->{"jobs"}[0]{"read"}{"io_bytes"}),
byte_human_read($data->{"jobs"}[0]{"read"}{"bw"}*1024),
$data->{"jobs"}[0]{"read"}{"iops"},
$data->{"jobs"}[0]{"read"}{"runtime"};
printf "write : io=%s, bw=%s/s, iops=%d runt=%sms\n",
byte_human_read($data->{"jobs"}[0]{"write"}{"io_bytes"}),
byte_human_read($data->{"jobs"}[0]{"write"}{"bw"}*1024),
$data->{"jobs"}[0]{"write"}{"iops"},
$data->{"jobs"}[0]{"write"}{"runtime"};
};
die $@ if $@;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment