Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vsespb
Last active December 24, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsespb/6776512 to your computer and use it in GitHub Desktop.
Save vsespb/6776512 to your computer and use it in GitHub Desktop.
GPG --multifile benchmark
#!/usr/bin/perl
use strict;
use warnings;
use Carp;
use IO::Handle;
use File::Copy;
use Time::HiRes qw/gettimeofday tv_interval/;
our $PASSWORD="MyPassword";
our $COUNT = 10;
our $COPIES = 2;
sub filename
{
"OUT/".shift().".tmp";
}
sub run_pgp
{
my ($cb, @command) = @_;
local (*READHANDLE,*WRITEHANDLE);
local $^F = 100;
pipe READHANDLE,WRITEHANDLE;
binmode READHANDLE;
binmode WRITEHANDLE;
WRITEHANDLE->autoflush;
READHANDLE->autoflush;
my $pid = fork();
unless ($pid){
close WRITEHANDLE;
my $n = fileno READHANDLE;
exec q{gpg}, q{--passphrase-fd}, $n, @command;
die;
}
close READHANDLE;
$cb->(*WRITEHANDLE);
waitpid $pid, 0;
confess if $?;
}
sub run_pgp2
{
my ($cb, @command) = @_;
my ($R, $W);
local $^F = 100;
pipe $R, $W;
binmode $W;
binmode $W;
$W->autoflush;
$R->autoflush;
my $pid = open(my $C, "|-");
unless ($pid){
close $W;
my $n = fileno $R;
umask ~0700;
exec q{gpg}, q{--passphrase-fd}, $n, @command;
die;
}
close $R;
$cb->($W, $C);
}
sub encrypt_file
{
my ($filename) = @_;
run_pgp(sub {
my ($f) = @_;
print $f $PASSWORD, "\n";
close $f;
}, qw/--yes --batch --symmetric --cipher-algo AES256/, $filename);
}
my @filenames;
for (1..$COUNT) {
my $filename = filename "src$_";
open my $f, ">", $filename;
print $f "SomeLongTestSequence SomeLongTestSequence SomeLongTestSequence SomeLongTestSequence SomeLongTestSequence SomeLongTestSequence SomeLongTestSequence test $_";
close $f;
encrypt_file($filename);
my $encfilename = "$filename.gpg";
push @filenames, $encfilename;
for (1..$COPIES) {
my $newencfilename = "${filename}_copy${_}.gpg";
unlink $newencfilename if -e $newencfilename;
copy($encfilename, $newencfilename);
push @filenames, $newencfilename;
}
unlink $filename or confess;
};
my $t0 = [gettimeofday];
run_pgp2(sub {
my ($P, $C) = @_;
print STDERR fileno $P, fileno $C, "\n";
print $P $PASSWORD, "\n";
close $P;
for (@filenames) {
print $C $_, "\n";
}
close $C;
}, qw/-d --multifile/);
my $t1 = [gettimeofday];
print "copies:", scalar @filenames, "\n";
print tv_interval ( $t0, $t1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment