File: ds_vs_eval.pl

package info (click to toggle)
libdata-streamdeserializer-perl 0.06-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 728 kB
  • sloc: perl: 660; makefile: 3
file content (168 lines) | stat: -rw-r--r-- 4,185 bytes parent folder | download | duplicates (4)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl

use warnings;
use strict;

use utf8;
use open qw(:std :utf8);

use lib qw(blib/lib blib/arch);
use Getopt::Std qw(getopts);
use Time::HiRes qw(time);
use Data::StreamDeserializer;

sub compare_object($$);
sub usage();
sub min(@);
sub max(@);
sub sum(@);
sub avg(@);

$| = 1;
getopts 'hn:b:' => \my %opts or usage;
usage if $opts{h};
my $file = $ARGV[0] or usage;
die "File not found: $file\n" unless -f $file;
my $data = `cat $file`;
my $iterations = $opts{n} || 1000;
my $block_size = $opts{b} || 512;

printf "%s bytes were read\n", length $data;

print "First deserializing by eval...";
my $object = eval $data;
die "Can't eval input data: $@" if $@;
print " done\n";

print "First deserializing by Data::DeSerializer...";
my $dsf = new Data::StreamDeserializer
    data => $data, block_size => $block_size;
1 until $dsf->next;
die "Can't deserialize input data: " . $dsf->error if $dsf->is_error;
print " done\n";

print "Check if deserialized objects are same...";
die "deserialized object aren't the same\n"
    unless compare_object $dsf->result, $object;
print " done\n";

my (@delay, @delay_dsr);
my $time = time;
printf "\nStarting %d iterations for eval...", $iterations;
for (1 .. $iterations) {
    my $start = time;
    my $res = eval $data;
    push @delay, time - $start;
}
my $eval_time = time - $time;
printf " done (%3.3f seconds)\n", $eval_time;

printf "Starting %d iterations for Data::StreamDeserializer...", $iterations;

$time = time;
my $partcounter = 0;
for (1 .. $iterations) {
    my $start = time;
    my $dsr = new Data::StreamDeserializer
        data => $data, block_size => $block_size;

    $partcounter++ until $dsr->next;
    $partcounter++;

    push @delay_dsr, time - $start;
}

my $ds_time = time - $time;
printf " done (%3.3f seconds)\n", $ds_time;

print  "\nEval statistic:\n";
printf "\t%d iterations were done\n", $iterations;
printf "\tmaximum deserialization time: %2.4f seconds\n", max(@delay);
printf "\tminimum deserialization time: %2.4f seconds\n", min(@delay);
printf "\taverage deserialization time: %2.4f seconds\n", avg(@delay);

print  "\nStreamDeserializer statistic:\n";
printf "\t%d iterations were done\n", $iterations;
printf "\t%d SUBiterations were done\n", $partcounter;
printf "\t%d bytes in one block in one iteration\n", $block_size;
printf "\tmaximum deserialization time: %2.4f seconds\n", max(@delay_dsr);
printf "\tminimum deserialization time: %2.4f seconds\n", min(@delay_dsr);
printf "\taverage deserialization time: %2.4f seconds\n", avg(@delay_dsr);
printf "\taverage subiteration time:    %2.5f seconds\n",
    sum(@delay_dsr) / $partcounter;

sub compare_object($$)
{
    my ($o1, $o2) = @_;
    return 0 unless ref($o1) eq ref $o2;
    return $o1 eq $o2 unless ref $o1;                        # SCALAR
    return $o1 eq $o2 if 'Regexp' eq ref $o1;                # Regexp
    return compare_object $$o1, $$o2 if 'SCALAR' eq ref $o1; # SCALARREF
    return compare_object $$o1, $$o2 if 'REF' eq ref $o1;    # REF

    if ('ARRAY' eq ref $o1) {
        return 0 unless @$o1 == @$o2;
        for (0 .. $#$o1) {
            return 0 unless compare_object $o1->[$_], $o2->[$_];
        }
        return 1;
    }

    if ('HASH' eq ref $o1) {
        return 0 unless keys(%$o1) == keys %$o2;

        for (keys %$o1) {
            return 0 unless exists $o2->{$_};
            return 0 unless compare_object $o1->{$_}, $o2->{$_};
        }
        return 1;
    }


    die ref $o1;
}

sub usage()
{
    print <<eof;

    usage: perl $0 [OPTIONS] test_file

    OPTIONS:

        -h              - this helpscreen
        -n count        - iterations (default 1000)
        -b count        - bytes in one subiteration (default 512),
                            see perldoc Data::StreamDeserializer
                                hint: block_size
eof
    exit 0;
}

sub min(@) {
    my $min = shift;
    for (@_) {
        $min = $_ if $min > $_;
    }
    return $min;
}

sub max(@) {
    my $max = shift;
    for (@_) {
        $max = $_ if $max < $_;
    }
    return $max;
}

sub sum(@)
{
    my $sum = 0;
    $sum += $_ for @_;
    return $sum;
}

sub avg(@)
{
    return sum(@_) / @_;
}