1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
################################################################################
#
# Copyright (c) 2002-2024 Marcus Holland-Moritz. All rights reserved.
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
################################################################################
use Test;
use Convert::Binary::C @ARGV;
use Convert::Binary::C::Cached;
$^W = 1;
BEGIN {
plan tests => 11;
}
my $CCCFG = require './tests/include/config.pl';
push @{$CCCFG->{Define}}, 'CACHE_TEST=1';
push @{$CCCFG->{Include}}, 'tests/include';
eval { require Data::Dumper }; $Data_Dumper = $@;
eval { require IO::File }; $IO_File = $@;
if( $Data_Dumper or $IO_File ) {
my $req;
$req = 'IO::File' if $IO_File;
$req = 'Data::Dumper' if $Data_Dumper;
$req = 'Data::Dumper and IO::File' if $Data_Dumper && $IO_File;
skip( "caching requires $req", 0 ) for 1 .. 11;
exit;
}
else { ok(1) }
eval { my @dummy = times };
if( $@ ) {
print "# no times() funtion, trying Time::HiRes...\n";
eval {
require Time::HiRes;
*main::mytime = \&Time::HiRes::time;
$required_time = 5;
$time_per_test = 1;
};
if( $@ ) {
print "# can't load Time::HiRes, using time()...\n";
*main::mytime = sub { time };
$required_time = 20;
$time_per_test = 4;
}
}
else {
print "# using times() for timing...\n";
*main::mytime = sub { my @t = times; $t[0]+$t[1] };
$required_time = 5;
$time_per_test = 1;
}
$cache = 'tests/cache.cbc';
-e $cache and unlink $cache;
# check "normal" C::B::C object
$tests = 5;
$next_test_time = 0;
$iterations = 0;
$start_time = mytime();
$elapsed_time = 0;
$fail = 0;
while ($elapsed_time < $required_time) {
eval {
$c = Convert::Binary::C->new( %$CCCFG );
$c->parse_file( 'tests/include/include.c' );
};
$@ and $fail = 1 and last;
$iterations++;
$elapsed_time = mytime() - $start_time;
# this is just to prevent the user from stopping the test
if( $elapsed_time >= $next_test_time and $tests > 0 ) {
$tests--;
$next_test_time += $time_per_test;
ok(1);
}
}
ok(1) while $tests-- > 0;
ok( $fail, 0, "failed to perform reference speed test ($@)" );
print "# uncached: $iterations iterations in $elapsed_time seconds\n";
# create cache file
eval {
$c = Convert::Binary::C::Cached->new( Cache => $cache, %$CCCFG );
$c->parse_file( 'tests/include/include.c' );
};
ok($@,'',"failed to create cache file for speed test");
# not ok if cache file doesn't exist now
ok( -e $cache );
# check cached object (this should be a lot faster)
$start_time = mytime();
eval {
for( 1 .. $iterations ) {
$c = Convert::Binary::C::Cached->new( Cache => $cache, %$CCCFG );
$c->parse_file( 'tests/include/include.c' );
}
};
ok( $@, '', "failed to perform cached speed test ($@)" );
$cached_time = mytime() - $start_time;
$speedup = $cached_time < 0.001 ? 1000 : $elapsed_time / $cached_time;
print "# cached: $iterations iterations in $cached_time seconds\n";
print "# speedup is $speedup\n";
# a speedup of 2 is acceptable
ok( $speedup > 2 );
-e $cache and unlink $cache;
|