benchmark opening of a random file

This commit is contained in:
hjp 2016-07-05 19:28:35 +00:00
parent e4f841f6c3
commit 0c72134b24
1 changed files with 31 additions and 0 deletions

31
openrandomfile/openrandomfile Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/perl
use warnings;
use strict;
use Time::HiRes qw(time);
chdir "/" or die;
my $finished;
my $depth = 0;
while (!$finished) {
my $t0 = time;
opendir my $dh, ".";
my @files = grep !m/^\.\.?$/, readdir($dh);
last unless @files;
my $file = $files[rand(@files)];
if (-d $file) {
if (chdir($file)) {
# continue
} else {
$finished = 1;
}
} else {
my $buf;
if (open (my $fh, '<', $file)) {
read($fh, $buf, 1);
}
$finished = 1;
}
my $t1 = time;
printf "%2d %s: %g\n", ++$depth, $file, $t1 - $t0;
}