mdisk: Avoid delay when mounting disks with a `/.dontsleep/` directory
[sunny256-utils.git] / Div / compr-test
blobbd9f0b8a85ca12b6a0aee02ed4b961bf4475e4ba
1 #!/usr/bin/env perl
3 #=======================================================================
4 # compr-test
5 # File ID: 8cb08b6c-fa50-11dd-aa63-000475e441b9
6 # Display a graph which compares the compress ratio from several
7 # programs. Quick & Dirty™.
9 # Character set: UTF-8
10 # ©opyleft 2006– Øyvind A. Holm <sunny@sunbase.org>
11 # License: GNU General Public License version 2 or later, see end of
12 # file for legal stuff.
13 #=======================================================================
15 use strict;
16 use warnings;
17 use Getopt::Long;
18 use Time::HiRes qw{ gettimeofday };
20 $| = 1;
22 our $Debug = 0;
24 our %Opt = (
26 'debug' => 0,
27 'full-scale' => 0,
28 'help' => 0,
29 'time' => 0,
30 'verbose' => 0,
31 'version' => 0,
35 our $progname = $0;
36 $progname =~ s/^.*\/(.*?)$/$1/;
37 our $VERSION = "0.00";
39 Getopt::Long::Configure("bundling");
40 GetOptions(
42 "debug" => \$Opt{'debug'},
43 "full-scale|f" => \$Opt{'full-scale'},
44 "help|h" => \$Opt{'help'},
45 "time|t" => \$Opt{'time'},
46 "verbose|v+" => \$Opt{'verbose'},
47 "version" => \$Opt{'version'},
49 ) || die("$progname: Option error. Use -h for help.\n");
51 $Opt{'debug'} && ($Debug = 1);
52 $Opt{'help'} && usage(0);
53 if ($Opt{'version'}) {
54 print_version();
55 exit(0);
58 LOOP:
59 for my $File (@ARGV) {
60 my $orig_size = file_size($File);
61 my $yrange = $Opt{'full-scale'} ? "[0:100]" : "[:]";
63 unless (length($orig_size)) {
64 warn("$progname: $File: Unable to stat() file\n");
65 next LOOP;
67 if ($orig_size == 0) {
68 warn("$progname: $File: File cannot be empty\n");
69 next LOOP;
71 unless (open(DatFP, ">", "$File.compr")) {
72 warn("$progname: $File.compr: Cannot open file for write: $!\n");
73 next LOOP;
76 if ($Opt{'time'}) {
77 system("cat $File >/dev/null");
79 for my $Comp (1..9) {
80 my $tmp_prefix = "$File.$Comp.tmp";
82 $Opt{'verbose'} && print("gzip -$Comp\n");
83 my $gzip_start = gettimeofday;
84 system("gzip -$Comp <$File >$tmp_prefix.gz");
85 my $gzip_end = gettimeofday;
86 my $gzip_time = $gzip_end-$gzip_start;
87 my $compr_gzip = file_size("$tmp_prefix.gz");
88 unlink("$tmp_prefix.gz");
90 $Opt{'verbose'} && print("bzip2 -$Comp\n");
91 my $bzip_start = gettimeofday;
92 system("bzip2 -$Comp <$File >$tmp_prefix.bz2");
93 my $bzip_end = gettimeofday;
94 my $bzip_time = $bzip_end-$bzip_start;
95 my $compr_bzip = file_size("$tmp_prefix.bz2");
96 unlink("$tmp_prefix.bz2");
98 $Opt{'verbose'} && print("zip -$Comp\n");
99 my $zip_start = gettimeofday;
100 system("zip -$Comp - - 2>/dev/null <$File >$tmp_prefix.zip");
101 my $zip_end = gettimeofday;
102 my $zip_time = $zip_end-$zip_start;
103 my $compr_zip = file_size("$tmp_prefix.zip");
104 unlink("$tmp_prefix.zip");
106 $Opt{'verbose'} && print("lzip -$Comp\n");
107 my $lzip_start = gettimeofday;
108 system("lzip -$Comp <$File >$tmp_prefix.lz");
109 my $lzip_end = gettimeofday;
110 my $lzip_time = $lzip_end-$lzip_start;
111 my $compr_lzip = file_size("$tmp_prefix.lz");
112 unlink("$tmp_prefix.lz");
114 $Opt{'verbose'} && print("lrzip -L $Comp\n");
115 my $lrzip_start = gettimeofday;
116 system("lrzip -L $Comp <$File >$tmp_prefix.lrz");
117 my $lrzip_end = gettimeofday;
118 my $lrzip_time = $lrzip_end-$lrzip_start;
119 my $compr_lrzip = file_size("$tmp_prefix.lrz");
120 unlink("$tmp_prefix.lrz");
122 $Opt{'verbose'} && print("lrzip -z -L $Comp\n");
123 my $lrzip_z_start = gettimeofday;
124 system("lrzip -z -L $Comp <$File >$tmp_prefix.lrz");
125 my $lrzip_z_end = gettimeofday;
126 my $lrzip_z_time = $lrzip_z_end-$lrzip_z_start;
127 my $compr_lrzip_z = file_size("$tmp_prefix.lrz");
128 unlink("$tmp_prefix.lrz");
130 $Opt{'verbose'} && print("xz -$Comp\n");
131 my $xz_start = gettimeofday;
132 system("xz -$Comp <$File >$tmp_prefix.xz");
133 my $xz_end = gettimeofday;
134 my $xz_time = $xz_end-$xz_start;
135 my $compr_xz = file_size("$tmp_prefix.xz");
136 unlink("$tmp_prefix.xz");
138 $Opt{'verbose'} && print("xz -e -$Comp\n");
139 my $xz_e_start = gettimeofday;
140 system("xz -e -$Comp <$File >$tmp_prefix.xz");
141 my $xz_e_end = gettimeofday;
142 my $xz_e_time = $xz_e_end-$xz_e_start;
143 my $compr_xz_e = file_size("$tmp_prefix.xz");
144 unlink("$tmp_prefix.xz");
146 my ($ratio_gzip, $ratio_bzip, $ratio_zip, $ratio_lzip, $ratio_lrzip, $ratio_lrzip_z, $ratio_xz, $ratio_xz_e);
148 if ($Opt{'time'}) {
149 # $ratio_gzip = $gzip_time / ($compr_gzip/$orig_size);
150 # $ratio_bzip = $bzip_time / ($compr_bzip/$orig_size);
151 # $ratio_zip = $zip_time / ($compr_zip/$orig_size);
152 $ratio_gzip = $gzip_time;
153 $ratio_bzip = $bzip_time;
154 $ratio_zip = $zip_time;
155 $ratio_lzip = $lzip_time;
156 $ratio_lrzip = $lrzip_time;
157 $ratio_lrzip_z = $lrzip_z_time;
158 $ratio_xz = $xz_time;
159 $ratio_xz_e = $xz_e_time;
160 } else {
161 $ratio_gzip = ($compr_gzip/$orig_size) * 100;
162 $ratio_bzip = ($compr_bzip/$orig_size) * 100;
163 $ratio_zip = ($compr_zip/$orig_size) * 100;
164 $ratio_lzip = ($compr_lzip/$orig_size) * 100;
165 $ratio_lrzip = ($compr_lrzip/$orig_size) * 100;
166 $ratio_lrzip_z = ($compr_lrzip_z/$orig_size) * 100;
167 $ratio_xz = ($compr_xz/$orig_size) * 100;
168 $ratio_xz_e = ($compr_xz_e/$orig_size) * 100;
171 print(DatFP "$Comp\t$ratio_gzip\t$ratio_bzip\t$ratio_zip\t$ratio_lzip\t$ratio_lrzip\t$ratio_lrzip_z\t$ratio_xz\t$ratio_xz_e\n");
173 close(DatFP);
174 my $gnuplot_file = "$File.gnuplot";
175 if (open(GnuplFP, ">", "$gnuplot_file")) {
176 print(GnuplFP <<END);
177 #!/usr/bin/gnuplot -persist
179 set yrange $yrange
180 plot "$File.compr" using 1:2 title "gzip" w lp pt 3, \\
181 "$File.compr" using 1:3 title "bzip2" w lp pt 3, \\
182 "$File.compr" using 1:4 title "zip" w lp pt 3, \\
183 "$File.compr" using 1:5 title "lzip" w lp pt 3, \\
184 "$File.compr" using 1:6 title "lrzip" w lp pt 3, \\
185 "$File.compr" using 1:7 title "lrzip -z" w lp pt 3, \\
186 "$File.compr" using 1:8 title "xz" w lp pt 3, \\
187 "$File.compr" using 1:9 title "xz -e" w lp pt 3
189 close(GnuplFP);
191 system("gnuplot -persist $gnuplot_file");
194 sub file_size {
195 my $File = shift;
196 my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime,
197 $mtime, $ctime, $blksize, $blocks) = stat($File);
198 return(defined($size) ? $size : "");
201 sub print_version {
202 # Print program version {{{
203 print("$progname v$VERSION\n");
204 # }}}
205 } # print_version()
207 sub usage {
208 # Send the help message to stdout {{{
209 my $Retval = shift;
211 if ($Opt{'verbose'}) {
212 print("\n");
213 print_version();
215 print(<<END);
217 Usage: $progname [options] [file [files [...]]]
219 Display a graph which compares the compress ratio from several programs.
220 Quick & Dirty™. Needs gnuplot(1).
222 Options:
224 -f, --full-scale
225 Use Y range 0–100.
226 -h, --help
227 Show this help.
228 -t, --time
229 Show number of microseconds used instead of size.
230 -v, --verbose
231 Increase level of verbosity. Can be repeated.
232 --version
233 Print version information.
234 --debug
235 Print debugging messages.
238 exit($Retval);
239 # }}}
240 } # usage()
242 sub msg {
243 # Print a status message to stderr based on verbosity level {{{
244 my ($verbose_level, $Txt) = @_;
246 if ($Opt{'verbose'} >= $verbose_level) {
247 print(STDERR "$progname: $Txt\n");
249 # }}}
250 } # msg()
252 sub D {
253 # Print a debugging message {{{
254 $Debug || return;
255 my @call_info = caller;
256 chomp(my $Txt = shift);
257 my $File = $call_info[1];
258 $File =~ s#\\#/#g;
259 $File =~ s#^.*/(.*?)$#$1#;
260 print(STDERR "$File:$call_info[2] $$ $Txt\n");
261 return("");
262 # }}}
263 } # D()
265 __END__
267 # Plain Old Documentation (POD) {{{
269 =pod
271 =head1 NAME
275 =head1 SYNOPSIS
277 [options] [file [files [...]]]
279 =head1 DESCRIPTION
283 =head1 OPTIONS
285 =over 4
287 =item B<-h>, B<--help>
289 Print a brief help summary.
291 =item B<-v>, B<--verbose>
293 Increase level of verbosity. Can be repeated.
295 =item B<--version>
297 Print version information.
299 =item B<--debug>
301 Print debugging messages.
303 =back
305 =head1 BUGS
309 =head1 AUTHOR
311 Made by Øyvind A. Holm S<E<lt>sunny@sunbase.orgE<gt>>.
313 =head1 COPYRIGHT
315 Copyleft © Øyvind A. Holm E<lt>sunny@sunbase.orgE<gt>
316 This is free software; see the file F<COPYING> for legalese stuff.
318 =head1 LICENCE
320 This program is free software: you can redistribute it and/or modify it
321 under the terms of the GNU General Public License as published by the
322 Free Software Foundation, either version 2 of the License, or (at your
323 option) any later version.
325 This program is distributed in the hope that it will be useful, but
326 WITHOUT ANY WARRANTY; without even the implied warranty of
327 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
328 See the GNU General Public License for more details.
330 You should have received a copy of the GNU General Public License along
331 with this program.
332 If not, see L<http://www.gnu.org/licenses/>.
334 =head1 SEE ALSO
336 =cut
338 # }}}
340 # vim: set fenc=UTF-8 ft=perl fdm=marker ts=4 sw=4 sts=4 et fo+=w :