blob: a8f6bd077a8fd4ecf5aa95c26725fd27c7a8c932 [file] [log] [blame]
Alex Savatieievec703692019-06-07 15:33:31 -05001#!/usr/bin/perl
2
3eval '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
9my $entropy = '/proc/sys/kernel/random/entropy_avail';
10my $random = '/dev/urandom';
11my $duration = 60.0; # seconds
12my $entropy_upper = 4095; # bits
13my $entropy_lower = 0; # bits
14my $nap_upper = 10.0; # seconds
15my $nap_lower = 1.0E-06; # seconds
16my $gain = 10.0; # seconds / bit
17
18#######################################################################
19# The rest of the script should not be edited:
20
21use strict;
22use warnings;
23
24use Getopt::Long qw(
25 :config
26 auto_help
27 auto_version
28 );
29use Pod::Usage;
30use Time::HiRes qw( sleep time );
31
32$| = 1;
33
34our $VERSION = sprintf("%d.%03d", q$Revision: 1.7 $ =~ /(\d+)/g);
35my $man;
36
37GetOptions(
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);
48pod2usage(-exitstatus => 0, -verbose => 2) if $man;
49
50my $entropy_span = $entropy_upper - $entropy_lower;
51my $entropy_setpoint = $entropy_upper / 2;
52my $nap_span = $nap_upper - $nap_lower;
53my $nap_offset = $nap_upper / 2;
54
55my $err;
56my $buf;
57my $e1;
58my $e2;
59my $t1;
60my $t2;
61my $rate;
62my $dt;
63my $signal;
64my $nap = $nap_lower;
65my $lastprint;
66
67open(my $random_fh, $random) or die "error opening $random: $!";
68$err = sysread($random_fh, $buf, 1);
69die "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`;
74chomp $e1;
75
76print "time (seconds) entropy (bits) random (bytes/second)\n",
77 "============== ============== ======================\n";
78my $begin = $lastprint = $t1 = time();
79my $end = $begin + $duration;
80do {
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);
106DONE:
107
108__END__
109
110=head1 NAME
111
112entropy-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
132Interactive benchmark for Linux entropy pool
133and random number generator.
134
135$Revision: 1.7 $
136
137=head1 SEE ALSO
138
139=head1 AUTHOR
140
141David Paul Christensen, E<lt>dpchrist@holgerdanske.comE<gt>
142
143=head1 COPYRIGHT AND LICENSE
144
145Copyright (C) 2014 by David Paul Christensen
146
147This library is free software; you can redistribute it and/or modify
148it under the same terms as Perl itself, either Perl version 5.14.2 or,
149at your option, any later version of Perl 5 you may have available.
150
151=cut
152