File: vs_dumper.pl

package info (click to toggle)
libdata-streamserializer-perl 0.07-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 676 kB
  • sloc: perl: 486; makefile: 3
file content (179 lines) | stat: -rw-r--r-- 4,130 bytes parent folder | download | duplicates (2)
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
169
170
171
172
173
174
175
176
177
178
179
#!/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::Dumper;
use Data::StreamSerializer;

$Data::Dumper::Indent = 0;
$Data::Dumper::Terse = 1;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Deepcopy = 1;

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 serializing by eval...";
my $object = eval $data;
die "Can't eval input data: $@" if $@;
print " done\n";

print "First serializing by Data::StreamSerializer... ";
my $sr = new Data::StreamSerializer($object);
my $dsld = '';
while(defined(my $str = $sr->next)) {
    $dsld  .= $str;
}
my $dobject = eval $dsld;
die $dsld if $@;

die "Serialize error" unless compare_object $dobject, $object;
print "done\n";


my @dumper_time;
my $time = time;
printf "Starting %d iterations for Dumper... ", $iterations;

for (1 .. $iterations) {
    my $start_time = time;
    my $str = Dumper($object);
    push @dumper_time, time - $start_time;
}

my $dumper_time = time - $time;

printf "done (%2.3f seconds)\n", $dumper_time;


my @ss_time;
$time = time;
printf "Starting %d iterations for Data::StreamSerializer... ", $iterations;

my $counter = 0;

for (1 .. $iterations) {
    my $start_time = time;

    $sr = new Data::StreamSerializer($object);
    $sr->block_size($block_size);
    $counter++ while defined $sr->next;
    push @ss_time, time - $start_time;
}

my $ss_time = time - $time;

printf "done (%2.3f seconds)\n", $ss_time;


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

print  "\nData::StreamSerializer statistic:\n";
printf "\t%d iterations were done\n", $iterations;
printf "\t%d SUBiterations were done\n", $counter;
printf "\tmaximum serialization time: %2.4f seconds\n", max(@ss_time);
printf "\tminimum serialization time: %2.4f seconds\n", min(@ss_time);
printf "\taverage serialization time: %2.4f seconds\n", avg(@ss_time);
printf "\taverage subiteration  time: %2.5f seconds\n", $ss_time / $counter;

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(@_) / @_;
}