[sheepdog] [PATCH 1/6] vditest: use if/else instead of switch/case

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Mon Feb 4 07:38:00 CET 2013


switch/case syntax is not familiar to perl programmers.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 script/vditest |  162 ++++++++++++++++++++++++-------------------------------
 1 files changed, 71 insertions(+), 91 deletions(-)

diff --git a/script/vditest b/script/vditest
index ffaf522..b8178ac 100755
--- a/script/vditest
+++ b/script/vditest
@@ -11,7 +11,6 @@
 #
 use strict;
 use Getopt::Std;
-use Switch;
 use Time::HiRes qw(gettimeofday);
 use IPC::Open2;
 
@@ -58,11 +57,9 @@ sub get_aligned_blk {
 sub to_bytes {
     my ($size) = @_;
 
-    switch ($size) {
-	case /K/i { $size *= 1024; }
-	case /M/i { $size *= 1024 ** 2; }
-	case /G/i { $size *= 1024 ** 3; }
-    }
+    $size *= 1024 if $size =~ /k$/i;
+    $size *= 1024 ** 2 if $size =~ /m$/i;
+    $size *= 1024 ** 3 if $size =~ /g$/i;
 
     $_[0] = $size;
 }
@@ -193,95 +190,78 @@ sub parse_opts {
 
     foreach my $key (keys %opts) {
 	my $val = $opts{$key};
-	switch ($key) {
-	    case '?' {
-		help(0);
-	    }
-	    case 'B' {
-		($lblk, $hblk) = ($val =~ /(\d+[kmg]?):?(\d*[kmg]?)/i);
-		to_bytes($lblk);
-		to_bytes($hblk);
-		$hblk = $lblk  if $hblk == 0;
-
-		error("$lblk is not sector aligned\n")  if $lblk % 512 != 0;
-		error("$lblk is not valid\n")  if $lblk == 0;
-		error("$hblk is not sector aligned\n")  if $hblk % 512 != 0;
-		error("$hblk is too large\n")  if $lblk > (64 * 1024 ** 2);
-		error("transfer range is invalid\n")  if $lblk > $hblk;
-	    }
-	    case 'c' {
-		if ($val !~ /(none|write(back|through))/) {
-		    error("\"$val\" is not valid\n");
-		}
-		$cache = $val;
-	    }
-	    case 'C' {
-		error("\"$val\" is not valid\n")  if ($val <= 0);
-		$concurrency = $val;
-	    }
-	    case 'D' {
-		($rrate, $wrate) = ($val =~ /(\d+)\%?:?(\d*)\%?/);
-	    }
-	    case 'f' {
-		$flush_interval = $val;
-	    }
-	    case 'F' {
-		$file = 1;
-	    }
-	    case 'h' {
-		error("\"$val\" is not valid\n")  if ($val <= 0);
-		$hbeat = $val;
-	    }
-	    case 'n' {
-		$no_act = 1;
-		$verbose = 1;
-	    }
-	    case 'o' {
-		error("\"$val\" is not valid\n")  if ($val < 0);
-		$offset = $val;
-	    }
-	    case 'p' {
-		if ($val =~ /^l/) {
-		    $seek_pattern = "linear";
-		} elsif ($val =~ /^r/) {
-		    $seek_pattern = "random";
-		} else {
-		    error("\"$val\" is not valid\n");
-		}
-	    }
-	    case 'r' {
-		$read_test = 1;
-		if ($write_test) {
-		    ($rrate, $wrate) = (50, 50);
-		} else {
-		    ($rrate, $wrate) = (100, 0);
-		}
-	    }
-	    case 's' {
-		$seed = $val;
-	    }
-	    case 'S' {
-		($sblk, $eblk) = ($val =~ /(\d+[kmg]?):?(\d*[kmg]?)/i);
-		to_bytes($sblk);
-		to_bytes($eblk);
 
-		error("$sblk is not sector aligned\n")  if $sblk % 512 != 0;
-		error("$eblk is not sector aligned\n")  if $eblk % 512 != 0;
+	if ($key eq '?') {
+	    help(0);
+	} elsif ($key eq 'B') {
+	    ($lblk, $hblk) = ($val =~ /(\d+[kmg]?):?(\d*[kmg]?)/i);
+	    to_bytes($lblk);
+	    to_bytes($hblk);
+	    $hblk = $lblk  if $hblk == 0;
+
+	    error("$lblk is not sector aligned\n")  if $lblk % 512 != 0;
+	    error("$lblk is not valid\n")  if $lblk == 0;
+	    error("$hblk is not sector aligned\n")  if $hblk % 512 != 0;
+	    error("$hblk is too large\n")  if $lblk > (64 * 1024 ** 2);
+	    error("transfer range is invalid\n")  if $lblk > $hblk;
+	} elsif ($key eq 'c') {
+	    if ($val !~ /(none|write(back|through))/) {
+		error("\"$val\" is not valid\n");
 	    }
-	    case 'T' {
-		error("\"$val\" is not valid\n")  if ($val < 0);
-		$runtime = $val;
+	    $cache = $val;
+	} elsif ($key eq 'C') {
+	    error("\"$val\" is not valid\n")  if ($val <= 0);
+	    $concurrency = $val;
+	} elsif ($key eq 'D') {
+	    ($rrate, $wrate) = ($val =~ /(\d+)\%?:?(\d*)\%?/);
+	} elsif ($key eq 'f') {
+	    $flush_interval = $val;
+	} elsif ($key eq 'F') {
+	    $file = 1;
+	} elsif ($key eq 'h') {
+	    error("\"$val\" is not valid\n")  if ($val <= 0);
+	    $hbeat = $val;
+	} elsif ($key eq 'n') {
+	    $no_act = 1;
+	    $verbose = 1;
+	} elsif ($key eq 'o') {
+	    error("\"$val\" is not valid\n")  if ($val < 0);
+	    $offset = $val;
+	} elsif ($key eq 'p') {
+	    if ($val =~ /^l/) {
+		$seek_pattern = "linear";
+	    } elsif ($val =~ /^r/) {
+		$seek_pattern = "random";
+	    } else {
+		error("\"$val\" is not valid\n");
 	    }
-	    case 'v' {
-		$verbose = 1;
+	} elsif ($key eq 'r') {
+	    $read_test = 1;
+	    if ($write_test) {
+		($rrate, $wrate) = (50, 50);
+	    } else {
+		($rrate, $wrate) = (100, 0);
 	    }
-	    case 'w' {
-		$write_test = 1;
-		if ($read_test) {
-		    ($rrate, $wrate) = (50, 50);
-		} else {
-		    ($rrate, $wrate) = (0, 100);
-		}
+	} elsif ($key eq 's') {
+	    $seed = $val;
+	} elsif ($key eq 'S') {
+	    ($sblk, $eblk) = ($val =~ /(\d+[kmg]?):?(\d*[kmg]?)/i);
+	    to_bytes($sblk);
+	    to_bytes($eblk);
+
+	    error("$sblk is not sector aligned\n")  if $sblk % 512 != 0;
+	    error("$eblk is not sector aligned\n")  if $eblk % 512 != 0;
+	} elsif ($key eq 'T') {
+	    error("\"$val\" is not valid\n")  if ($val < 0);
+	    $runtime = $val;
+	} elsif ($key eq 'v') {
+	    $verbose = 1;
+	} elsif ($key eq 'w') {
+	    $write_test = 1;
+	    if ($read_test) {
+		($rrate, $wrate) = (50, 50);
+	    } else {
+		($rrate, $wrate) = (0, 100);
 	    }
 	}
     }
-- 
1.7.2.5




More information about the sheepdog mailing list