Benchmark-Serialize

 view release on metacpan or  search on metacpan

examples/serialize.pl  view on Meta::CPAN

use warnings;

use Getopt::Long;
use Benchmark::Serialize qw( cmpthese );

use Benchmark::Serialize::Library::ProtocolBuffers;
use Benchmark::Serialize::Library::ProtocolBuffers::XS;
use Benchmark::Serialize::Library::Data::Serializer;

my @benchmark          = ();      # package names of benchmarks to run
my $iterations         = -1;      # integer
my $structure          = {
    array  => [ 'a' .. 'j' ],
    hash   => { 'a' .. 'z' },
    string => 'x' x 200
};

my $protocolbuffers; # Can't process inline as we might need the structure

Getopt::Long::Configure( 'bundling' );
Getopt::Long::GetOptions(
    'b|benchmark=s@' => \@benchmark,
    'deflate!'       => \$Benchmark::Serialize::benchmark_deflate,
    'inflate!'       => \$Benchmark::Serialize::benchmark_inflate,
    'roundtrip!'     => \$Benchmark::Serialize::benchmark_roundtrip,
    'i|iterations=i' => \$iterations,
    'o|output=s'     => \$Benchmark::Serialize::output,
    'v|verbose!'     => \$Benchmark::Serialize::verbose,
    's|structure=s'  => sub {
        die "Structure option requires YAML.\n"
        unless YAML->require;

        $structure = YAML::LoadFile( $_[1] );
    },
    'ds|data-serializer=s'  => sub { Benchmark::Serialize::Library::Data::Serializer->register($_[1]) }, 
    'pbx=s'                 => sub { Benchmark::Serialize::Library::ProtocolBuffers::XS->register($_[1]) }, 

examples/serialize.pl  view on Meta::CPAN

        die unless defined $structure;
    }
) or exit 1;

if (defined $protocolbuffers) {
    Benchmark::Serialize::Library::ProtocolBuffers->register( ProtocolBuffers => ($protocolbuffers ? $protocolbuffers : $structure) );
}

@benchmark = ("all") unless @benchmark;

cmpthese($iterations, $structure, @benchmark);

lib/Benchmark/Serialize.pm  view on Meta::CPAN

our %EXPORT_TAGS = ( all => \@EXPORT_OK );

our $benchmark_deflate  = 1;       # boolean
our $benchmark_inflate  = 1;       # boolean
our $benchmark_roundtrip= 1;       # boolean
our $benchmark_size     = 1;       # boolean
our $verbose            = 0;       # boolean
our $output             = 'chart'; # chart or list

sub cmpthese {
    my $iterations = shift;
    my $structure  = shift;
    my @benchmarks = Benchmark::Serialize::Library->load( @_ );

    my $width   = width(map { $_->name } @benchmarks);
    my $results = { };

    print "\nModules\n";

    BENCHMARK:

lib/Benchmark/Serialize.pm  view on Meta::CPAN

        } or do {
            warn "Benchmark $name died with:\n    $@\n";
            next BENCHMARK;
        };
    
        my ($likeliness, $diag) = likeliness( $inflated, $structure );
        printf( "%-${width}s : %8s %s\n", $benchmark->name, $benchmark->version, $likeliness);

        print Test::Deep::deep_diag($diag), "\n" if defined($diag) and $Benchmark::Serialize::verbose;

        $results->{deflate}->{$name} = timeit_deflate( $iterations, $structure, $benchmark )
            if $benchmark_deflate;

        $results->{inflate}->{$name} = timeit_inflate( $iterations, $structure, $benchmark )
            if $benchmark_inflate;

        $results->{roundtrip}->{$name} = timeit_roundtrip( $iterations, $structure, $benchmark )
            if $benchmark_roundtrip;

        $results->{size}->{$name}    = length( $deflated );
    }

    output( 'Sizes', "size", $output, $results->{size}, $width )
        if $benchmark_size;

    output( 'Deflate (perl -> serialized)', "time", $output, $results->{deflate}, $width )
        if $benchmark_deflate;

lib/Benchmark/Serialize.pm  view on Meta::CPAN


sub size_list {
    my $results = shift;
    my $width   = shift;
    foreach my $title ( sort keys %{ $results } ) {
        printf( "%-${width}s : %d bytes\n", $title, $results->{ $title } );
    }
}

sub timeit_deflate {
    my ( $iterations, $structure, $benchmark ) = @_;
    return Benchmark::timethis( $iterations, sub { $benchmark->deflate($structure) }, '', 'none' );
}

sub timeit_inflate {
    my ( $iterations, $structure, $benchmark ) = @_;
    my $deflated = $benchmark->deflate($structure);
    return Benchmark::timethis( $iterations, sub { $benchmark->inflate($deflated) }, '', 'none' );
}

sub timeit_roundtrip {
    my ( $iterations, $structure, $benchmark ) = @_;
    return Benchmark::timethis( $iterations, sub { $benchmark->inflate( $benchmark->deflate( $structure )) }, '', 'none' );
}

sub width {
    return length( ( sort { length $a <=> length $b } @_ )[-1] );
}

sub likeliness {
    my ($got, $expected) = @_;
    my ($ok, $diag);



( run in 0.526 second using v1.01-cache-2.11-cpan-96521ef73a4 )