/
test.pl
executable file
·300 lines (256 loc) · 6.35 KB
/
test.pl
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/perl
use strict;
use warnings;
use blib;
$ValueObject::ObjectCount = 0;
sub ValueObject::new {
my $cls = shift;
my $v = rand();
$ValueObject::ObjectCount++;
my $self = \$v;
bless $self, $cls;
}
sub ValueObject::DESTROY {
$ValueObject::ObjectCount--;
}
$KeyObject::ObjectCount = 0;
sub KeyObject::new {
my $cls = shift;
my $v = rand();
$KeyObject::ObjectCount++;
my $self = \$v;
bless $self, $cls;
}
sub KeyObject::DESTROY { $KeyObject::ObjectCount--; }
package main;
use strict;
use warnings;
use Data::Dumper;
use Getopt::Long;
use Ref::Store::Common;
use Carp::Heavy;
use Log::Fu { level=> "debug", target => \*STDOUT };
use lib "/home/mordy/src/Ref-Store/lib";
use Benchmark qw(:all);
use Module::Stubber 'Memory::Usage' => [],
will_use => { state => sub { [] } };
my $Htype = 'Ref::Store::PP';
GetOptions('x|xs' => \my $use_xs,
'p|pp' => \my $use_pp,
'sweeping' => \my $use_sweep,
'c|count=i' => \my $count,
'm|mode=s' => \my $Mode,
'd|dump' => \my $Dump,
'prealloc=i' => \my $Prealloc,
'cycles=i' => \my $Cycles
);
$Cycles ||= 1;
$Prealloc ||= 0;
$count ||= 50;
$Mode ||= 'all';
my $cur_cycle = 0;
my $_mu;
if($^O !~ /linux/i) {
log_warn("Can't run Memory::Usage on non-linux systems (procfs needed)");
} else {
$_mu = Memory::Usage->new();
}
sub memusage_log {
return unless defined $_mu;
my $label = shift;
$_mu->record($label);
}
sub memusage_dump {
return unless defined $_mu;
#Dump memory usage information:
log_info("Dumping memory usage statistics");
my $mpriv = 0;
printf STDERR ("%-40s %6s\t %6s\n", "State", "RSS", "DIFF");
foreach my $st (@{$_mu->state()}) {
my ($msg,$rss) = @{$st}[1,3];
$rss /= 1024;
my $diff = $rss - $mpriv;
printf STDERR ("%-40s %6dMB\t %6d\n", $msg, $rss, $diff);
$mpriv = $rss;
}
}
my $i_BEGIN = 1;
my $i_END = $count;
memusage_log("Begin");
sub single_pass {
my $Hash = $Htype->new();
my ($impl_s) = (split(/::/, $Htype))[-1];
my $mu_prefix = "$cur_cycle: [$impl_s]";
my @olist;
#Create object list..
timethis(1, sub {
@olist = map { ValueObject->new() } ($i_BEGIN..$i_END);
}, "Object Creation");
memusage_log("$mu_prefix Objects Created");
if($Mode =~ /key|all/i ) {
timethis(1, sub {
foreach my $i ($i_BEGIN..$i_END) {
my $obj = $olist[$i-1];
$Hash->store($i, $obj);
$Hash->store(-$i, $obj);
push @olist, $obj;
}
}, "String Key (STORE)");
log_infof("Created %d objects\n", $ValueObject::ObjectCount);
log_infof("Have %d objects now", $ValueObject::ObjectCount);
memusage_log("$mu_prefix Key storage");
timethis(1, sub {
foreach my $i($i_BEGIN..$i_END) {
eval {
my $obj1 = $Hash->fetch($i) or die "POSITIVE KEY FAIL!";
my $obj2 = $Hash->fetch(-$i) or die "GAH!";
$obj1->isa('ValueObject') &&
$obj2->isa('ValueObject') &&
$obj1 == $obj2
or die
"Soemthing happen!";
#log_info($obj1);
}; if($@) {
#print Dumper($Hash);
die $@;
}
}
}, "String Key (FETCH)");
}
my (@klist,@klist2);
if($Mode =~ /objk|all/i) {
@klist = map { KeyObject->new() } (0..$i_END-1);
@klist2 = map { KeyObject->new() } (0..$i_END-1);
memusage_log("$mu_prefix ObjK created");
timethis(1, sub {
foreach my $i (0..$i_END-1) {
$Hash->store($klist[$i], $olist[$i]);
$Hash->store($klist2[$i], $olist[$i]);
}
}, "Object Key (STORE)");
memusage_log("$mu_prefix ObjK Store");
timethis(1, sub {
foreach my $i (0..$i_END-1) {
my $res1 = $Hash->fetch($klist[$i]);
my $res2 = $Hash->fetch($klist2[$i]);
if(!$res1 || !$res2 || $res1 != $res2 || $res1 != $olist[$i]) {
die("Object key mismatch!");
}
}
}, "Object Key (FETCH)");
}
if($Mode =~ m/attr|all/i) {
my $ATTRTYPE = 42;
my $ATTRTYPE_ALT = "ALLYOURBASE";
$Hash->register_kt($ATTRTYPE, "TESTATTR");
$Hash->register_kt($ATTRTYPE_ALT, "ALTATTR");
my @attrpairs = (
[43, $ATTRTYPE],
[666, $ATTRTYPE],
[770, $ATTRTYPE],
[1, $ATTRTYPE_ALT]
);
timethis(1, sub {
foreach my $o (@olist) {
$Hash->store_a(@$_, $o) foreach @attrpairs;
}
}, "Attribute (STORE)");
memusage_log("$mu_prefix Attribtue Storage");
my $result_count = 0;
timethis(1, sub {
foreach my $apair (@attrpairs) {
my @tmp = $Hash->fetch_a(@$apair);
$result_count += scalar @tmp;
}
}, "Attribute (FETCH)");
log_info("Got total $result_count entries");
}
if($Dump) {
$Hash->dump();
}
log_infof("FORWARD=%d, REVERSE=%d, KEYS=%d, ATTRS=%d",
scalar values %{$Hash->forward},
scalar values %{$Hash->reverse},
scalar values %{$Hash->scalar_lookup},
scalar values %{$Hash->attr_lookup});
#print Dumper($Hash);
timethis(1, sub {
@olist = ();
@klist = ();
@klist2 = ();
}, "Delete");
if($Hash->isa('Ref::Store::Sweeping')) {
log_warn("Sweeping..");
$Hash->sweep();
}
log_debug("Everything should be cleared");
log_infof("Have %d objects now", $ValueObject::ObjectCount);
log_infof("FORWARD: %d, REVERSE=%d",
scalar values %{$Hash->forward},
scalar values %{$Hash->reverse}
);
if(!$Hash->is_empty) {
print Dumper($Hash);
}
#$Hash->dump();
#print Dumper($Hash);
}
my $use_all = !($use_xs||$use_pp||$use_sweep);
my @impl_map = (
$use_xs, 'XS',
$use_pp, 'PP',
$use_sweep, 'Sweeping'
);
my @EnabledImplementations;
while (@impl_map) {
my ($enabled,$backend) = splice(@impl_map, 0, 2);
if(!$enabled && !$use_all) {
next;
}
push @EnabledImplementations, 'Ref::Store::'.$backend;
}
foreach (1..$Cycles) {
$cur_cycle = $_;
log_info("Cycle: $cur_cycle");
foreach my $impl (@EnabledImplementations) {
$Htype = $impl;
eval "require $Htype";
log_warn("Using $Htype");
single_pass();
}
}
memusage_dump();
use Scalar::Util qw(weaken);
sub compare_simple {
my %simplehash;
my @vals;
foreach ($i_BEGIN..$i_END) {
push @vals, ValueObject->new();
}
timethis(1, sub {
foreach my $i ($i_BEGIN..$i_END) {
$simplehash{$i} = $vals[$i];
$simplehash{-$i} = $vals[$i];
}
}, "Normal hash: STORE");
timethis(1, sub {
foreach my $i ($i_BEGIN..$i_END) {
my $copy;
$copy = $simplehash{$i};
$copy = $simplehash{-$i};
}
}, "Normal hash: FETCH");
timethis(1, sub {
%simplehash = ();
}, "Normal hash: DELETE");
%simplehash = ();
timethis(1, sub {
foreach my $i (0..$i_END-1) {
foreach my $a (qw(attr1 att2 att3 attr4)) {
weaken($simplehash{$a}->{$vals[$i]+0} = $vals[$i]);
}
}
}, "Normal Hash, ATTR STORE");
}
compare_simple();
log_info("Exiting..");