Alex Savatieiev | ec70369 | 2019-06-07 15:33:31 -0500 | [diff] [blame] | 1 | #!/usr/bin/perl |
| 2 | |
| 3 | eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' |
| 4 | if 0; # not running under some shell |
| 5 | # $Id: entropy-random-bench,v 1.7 2014/11/27 21:58:22 dpchrist Exp $ |
| 6 | ####################################################################### |
| 7 | # Argument defaults -- edit to suit: |
| 8 | |
| 9 | my $entropy = '/proc/sys/kernel/random/entropy_avail'; |
| 10 | my $random = '/dev/urandom'; |
| 11 | my $duration = 60.0; # seconds |
| 12 | my $entropy_upper = 4095; # bits |
| 13 | my $entropy_lower = 0; # bits |
| 14 | my $nap_upper = 10.0; # seconds |
| 15 | my $nap_lower = 1.0E-06; # seconds |
| 16 | my $gain = 10.0; # seconds / bit |
| 17 | |
| 18 | ####################################################################### |
| 19 | # The rest of the script should not be edited: |
| 20 | |
| 21 | use strict; |
| 22 | use warnings; |
| 23 | |
| 24 | use Getopt::Long qw( |
| 25 | :config |
| 26 | auto_help |
| 27 | auto_version |
| 28 | ); |
| 29 | use Pod::Usage; |
| 30 | use Time::HiRes qw( sleep time ); |
| 31 | |
| 32 | $| = 1; |
| 33 | |
| 34 | our $VERSION = sprintf("%d.%03d", q$Revision: 1.7 $ =~ /(\d+)/g); |
| 35 | my $man; |
| 36 | |
| 37 | GetOptions( |
| 38 | "entropy=s" => \$entropy, |
| 39 | "random=s" => \$random, |
| 40 | "duration=f" => \$duration, |
| 41 | "entropy-upper=f" => \$entropy_upper, |
| 42 | "entropy-lower=f" => \$entropy_lower, |
| 43 | "nap-upper=f" => \$nap_upper, |
| 44 | "nap-lower=f" => \$nap_lower, |
| 45 | "gain=f" => \$gain, |
| 46 | "man" => \$man, |
| 47 | ) or pod2usage(2); |
| 48 | pod2usage(-exitstatus => 0, -verbose => 2) if $man; |
| 49 | |
| 50 | my $entropy_span = $entropy_upper - $entropy_lower; |
| 51 | my $entropy_setpoint = $entropy_upper / 2; |
| 52 | my $nap_span = $nap_upper - $nap_lower; |
| 53 | my $nap_offset = $nap_upper / 2; |
| 54 | |
| 55 | my $err; |
| 56 | my $buf; |
| 57 | my $e1; |
| 58 | my $e2; |
| 59 | my $t1; |
| 60 | my $t2; |
| 61 | my $rate; |
| 62 | my $dt; |
| 63 | my $signal; |
| 64 | my $nap = $nap_lower; |
| 65 | my $lastprint; |
| 66 | |
| 67 | open(my $random_fh, $random) or die "error opening $random: $!"; |
| 68 | $err = sysread($random_fh, $buf, 1); |
| 69 | die "error reading $random: $!" unless defined $err && $err; |
| 70 | |
| 71 | ### /proc/sys/kernel/random/entropy_avail is not world-readable, but |
| 72 | ### 'cat' can read it (?) |
| 73 | $e1 = `cat $entropy`; |
| 74 | chomp $e1; |
| 75 | |
| 76 | print "time (seconds) entropy (bits) random (bytes/second)\n", |
| 77 | "============== ============== ======================\n"; |
| 78 | my $begin = $lastprint = $t1 = time(); |
| 79 | my $end = $begin + $duration; |
| 80 | do { |
| 81 | sleep($nap); |
| 82 | |
| 83 | $err = sysread($random_fh, $buf, 1); |
| 84 | die "error reading $random: $!" unless defined $err; |
| 85 | |
| 86 | $e2 = `cat $entropy`; |
| 87 | chomp $e2; |
| 88 | |
| 89 | $t2 = time(); |
| 90 | $dt = $t2 - $t1; |
| 91 | $rate = 1.0 / $dt; |
| 92 | if ($dt && ($lastprint + 1 < $t2)) { |
| 93 | $lastprint = $t2; |
| 94 | printf "%14.06f %14i %22.6f\n", |
| 95 | $t2 - $begin, |
| 96 | $e2, |
| 97 | $rate; |
| 98 | } |
| 99 | $signal = ($e2 - $entropy_setpoint) / $entropy_span; |
| 100 | $nap = -1.0 * $gain * $signal * $nap_span + $nap_offset; |
| 101 | $nap = $nap_lower if $nap < $nap_lower; |
| 102 | $nap = $nap_upper if $nap_upper < $nap; |
| 103 | $e1 = $e2; |
| 104 | $t1 = $t2; |
| 105 | } while ($t2 < $end); |
| 106 | DONE: |
| 107 | |
| 108 | __END__ |
| 109 | |
| 110 | =head1 NAME |
| 111 | |
| 112 | entropy-random-bench - Linux entropy pool / random number benchmark |
| 113 | |
| 114 | =head1 SYNOPSIS |
| 115 | |
| 116 | entropy-random-bench.pl [options] |
| 117 | |
| 118 | Options: |
| 119 | --entropy path to entropy availble file |
| 120 | --random path to random number file |
| 121 | --duration duration of benchmark |
| 122 | --entropy-upper upper range value of entropy available |
| 123 | --entropy-lower lower range value of entropy available |
| 124 | --nap-upper upper range value for sleep() calls |
| 125 | --nap-lower upper range value for sleep() calls |
| 126 | --gain timing loop proportional gain |
| 127 | --man print manual page and exit |
| 128 | --help, -? print usage message and exit |
| 129 | |
| 130 | =head1 DESCRIPTION |
| 131 | |
| 132 | Interactive benchmark for Linux entropy pool |
| 133 | and random number generator. |
| 134 | |
| 135 | $Revision: 1.7 $ |
| 136 | |
| 137 | =head1 SEE ALSO |
| 138 | |
| 139 | =head1 AUTHOR |
| 140 | |
| 141 | David Paul Christensen, E<lt>dpchrist@holgerdanske.comE<gt> |
| 142 | |
| 143 | =head1 COPYRIGHT AND LICENSE |
| 144 | |
| 145 | Copyright (C) 2014 by David Paul Christensen |
| 146 | |
| 147 | This library is free software; you can redistribute it and/or modify |
| 148 | it under the same terms as Perl itself, either Perl version 5.14.2 or, |
| 149 | at your option, any later version of Perl 5 you may have available. |
| 150 | |
| 151 | =cut |
| 152 | |