forked from memcached/memcached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstress-memcached.pl
executable file
·101 lines (87 loc) · 2.18 KB
/
stress-memcached.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
#!/usr/bin/env perl
#
use strict;
use lib '../../api/perl/lib';
use Cache::Memcached;
use Time::HiRes qw(time);
unless (@ARGV == 2) {
die "Usage: stress-memcached.pl ip:port threads\n";
}
my $host = shift;
my $threads = shift;
my $memc = new Cache::Memcached;
$memc->set_servers([$host]);
unless ($memc->set("foo", "bar") &&
$memc->get("foo") eq "bar") {
die "memcached not running at $host ?\n";
}
$memc->disconnect_all();
my $running = 0;
while (1) {
if ($running < $threads) {
my $cpid = fork();
if ($cpid) {
$running++;
#print "Launched $cpid. Running $running threads.\n";
} else {
stress();
exit 0;
}
} else {
wait();
$running--;
}
}
sub stress {
undef $memc;
$memc = new Cache::Memcached;
$memc->set_servers([$host]);
my ($t1, $t2);
my $start = sub { $t1 = time(); };
my $stop = sub {
my $op = shift;
$t2 = time();
my $td = sprintf("%0.3f", $t2 - $t1);
if ($td > 0.25) { print "Took $td seconds for: $op\n"; }
};
my $max = rand(50);
my $sets = 0;
for (my $i = 0; $i < $max; $i++) {
my $key = key($i);
my $set = $memc->set($key, $key);
$sets++ if $set;
}
for (1..int(rand(500))) {
my $rand = int(rand($max));
my $key = key($rand);
my $meth = int(rand(3));
my $exp = int(rand(3));
undef $exp unless $exp;
$start->();
if ($meth == 0) {
$memc->add($key, $key, $exp);
$stop->("add");
} elsif ($meth == 1) {
$memc->delete($key);
$stop->("delete");
} else {
$memc->set($key, $key, $exp);
$stop->("set");
}
$rand = int(rand($max));
$key = key($rand);
$start->();
my $v = $memc->get($key);
$stop->("get");
if ($v && $v ne $key) { die "Bogus: $v for key $rand\n"; }
}
$start->();
my $multi = $memc->get_multi(map { key(int(rand($max))) } (1..$max));
$stop->("get_multi");
}
sub key {
my $n = shift;
$_ = sprintf("%04d", $n);
if ($n % 2) { $_ .= "a"x20; }
$_;
}