| 1 | # This code runs on perl4.036 and newer perls. Usage: | ||
| 2 | # | ||
| 3 | # require 'benchlib.pl'; | ||
| 4 | # runtest(1000, <<'ENDTEST'); | ||
| 5 | # # some testing code here | ||
| 6 | # ENDTEST | ||
| 7 | |||
| 8 | package benchlib; | ||
| 9 | |||
| 10 | $cpu_factor = shift; | ||
| 11 | if (!$cpu_factor) { | ||
| 12 | die "No CPU factor specified\n"; | ||
| 13 | } | ||
| 14 | $empty_cycles_per_sec = shift; | ||
| 15 | |||
| 16 | sub main'runtest | ||
| 17 | { | ||
| 18 | local($scale, $code) = @_; | ||
| 19 | print "Date: " . &time2iso() . "\n"; | ||
| 20 | print "Test: $0\n"; | ||
| 21 | print "Perl: $^X $]\n"; | ||
| 22 | $scale = int($scale * $cpu_factor); | ||
| 23 | $scale = 1 if $scale < 1; | ||
| 24 | $code = <<'EOT1' . $code . <<EOT2 . $code . <<'EOT3'; | ||
| 25 | # warm up | ||
| 26 | for ($i = 0; $i < 2; $i++) { | ||
| 27 | package main; | ||
| 28 | #---- test code ---- | ||
| 29 | EOT1 | ||
| 30 | #------------------- | ||
| 31 | } | ||
| 32 | |||
| 33 | \$before_r = time; | ||
| 34 | (\$before_u, \$before_s, \$before_cu, \$before_cs) = times; | ||
| 35 | for (\$i = 0; \$i < $scale; \$i++) { | ||
| 36 | package main; | ||
| 37 | #---- test code ---- | ||
| 38 | EOT2 | ||
| 39 | #------------------- | ||
| 40 | } | ||
| 41 | ($after_u, $after_s, $after_cu, $after_cs) = times; | ||
| 42 | $after_r = time; | ||
| 43 | |||
| 44 | $user = ($after_u - $before_u) + ($after_cu - $before_cu); | ||
| 45 | $system = ($after_s - $before_s) + ($after_cs - $before_cs); | ||
| 46 | $real = ($after_r - $before_r); | ||
| 47 | $used = $user + $system; | ||
| 48 | |||
| 49 | print "Cycles: $scale\n"; | ||
| 50 | print "User-Time: $user\n"; | ||
| 51 | print "System-Time: $system\n"; | ||
| 52 | printf "Real-Time: %.*f\n", ($hires ? 3 : 0), $real; | ||
| 53 | printf "CPU: %.0f%%\n", 100*$used/$real if $real > 0; | ||
| 54 | if ($used > 0.1) { | ||
| 55 | my $cps = $scale / $used; | ||
| 56 | printf "Cycles-Per-Sec: %.*f\n", ($cps < 100 ? 3 : 0), $cps; | ||
| 57 | if (defined $empty_cycles_per_sec) { | ||
| 58 | printf "Empty-Cycles-Per-Sec: %.0f\n", $empty_cycles_per_sec; | ||
| 59 | $loop_overhead = $scale / $empty_cycles_per_sec; | ||
| 60 | $p = 100 * $loop_overhead / $used; | ||
| 61 | printf "Loop-Overhead: %.1f%%\n", $p; | ||
| 62 | $used -= $loop_overhead; | ||
| 63 | print "Adjusted-Used-Time: $used\n"; | ||
| 64 | } | ||
| 65 | printf "Bench-Points: %.5g\n", 1000 / $used if $used > 0; | ||
| 66 | } | ||
| 67 | EOT3 | ||
| 68 | |||
| 69 | if ($] >= 5.002) { | ||
| 70 | $code = <<'EOT' . $code; | ||
| 71 | BEGIN { | ||
| 72 | # If Time::HiRes is available, we can get better resolution | ||
| 73 | eval { | ||
| 74 | require Time::HiRes; | ||
| 75 | Time::HiRes->import('time'); | ||
| 76 | $hires++; | ||
| 77 | }; | ||
| 78 | } | ||
| 79 | |||
| 80 | EOT | ||
| 81 | } | ||
| 82 | |||
| 83 | print $code if $main'debug; | ||
| 84 | eval $code; | ||
| 85 | if ($@) { | ||
| 86 | die $@; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | sub time2iso | ||
| 91 | { | ||
| 92 | my $time = shift; | ||
| 93 | $time = time unless defined $time; | ||
| 94 | my($sec,$min,$hour,$mday,$mon,$year) = localtime($time); | ||
| 95 | return sprintf("%04d-%02d-%02d %02d:%02d:%02d", | ||
| 96 | $year+1900, $mon+1, $mday, $hour, $min, $sec); | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | 1; |