-
Notifications
You must be signed in to change notification settings - Fork 1
/
redis.t
82 lines (74 loc) · 1.71 KB
/
redis.t
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
#!perl
use strict;
use XSFun;
use Protocol::Redis::XS;
use RedisDB::Parse::Redis;
use RedisDB::Parse::Redis_XS;
use RedisDB::Parse::Redis_PP;
use Data::Dumper;
my $redisdb = RedisDB::Parse::Redis_PP->new();
my $redisdb_xs = RedisDB::Parse::Redis_XS->new();
$redisdb->set_default_callback(sub {});
$redisdb_xs->set_default_callback(sub {});
my $redis = Protocol::Redis::XS->new(api => 1);
#my $reply = "+OK\r\n";
my $reply = join "\r\n", qw(
*3
:123
$-1
$9
hhhhhhhhh
), "";
#$reply .= "\$100000\r\n" . ("x" x 100000) . "\r\n";
sub parse_elem {
if ($_[0] =~ /\G\$(-?[0-9]+)\r\n/sgc) {
return undef if $1 == -1;
my $str = substr($_[0], pos($_[0]), $1);
pos($_[0]) += $1+2;
return $str;
}
elsif ($_[0] =~ /\G:(-?[0-9]+)\r\n/sgc) {
return $1;
}
else {
die;
}
}
sub rparse {
pos($_[0]) = 0;
if ($_[0] =~ /\G\*(-?[0-9]+)\r\n/sgc) {
my $nargs = $1;
return undef
if $nargs == -1;
my @rv;
foreach (1..$nargs) {
push @rv, parse_elem($_[0]);
}
return \@rv;
}
elsif ($_[0] =~ /\G[:\$]/sgc) {
pos($_[0])--;
return [parse_elem($_[0])];
}
elsif ($_[0] =~ /\G([+-])/sgc) {
my $rv = substr($_[0], ($1 eq '+' ? pos($_[0]) : pos($_[0])-1));
$rv =~ s/\r\n$//;
return $rv;
}
else { die }
}
#use Data::Dumper;
#warn Dumper(rparse($reply));
#warn Dumper(XSFun::redis_parse($reply));
#warn Dumper(do{$redis->parse($reply);$redis->get_message});
use Benchmark qw(timethese);
timethese(-2, {
xs => sub {my $x = XSFun::redis_parse($reply)},
perl => sub {my $x = rparse($reply)},
prxs => sub {
$redis->parse($reply);
my $x = $redis->get_message;
},
redisdb => sub {$redisdb->add($reply)},
redisdb_xs => sub {$redisdb_xs->add($reply)},
});