From 0c72134b24f1ebc7d0c07644124ba05d577da3cf Mon Sep 17 00:00:00 2001 From: hjp Date: Tue, 5 Jul 2016 19:28:35 +0000 Subject: [PATCH] benchmark opening of a random file --- openrandomfile/openrandomfile | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 openrandomfile/openrandomfile diff --git a/openrandomfile/openrandomfile b/openrandomfile/openrandomfile new file mode 100755 index 0000000..01380eb --- /dev/null +++ b/openrandomfile/openrandomfile @@ -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; +}