1#! /usr/bin/perl
2#
3use warnings;
4use strict;
5
6use IO::Socket::INET;
7
8use FindBin;
9
10@ARGV == 1 or @ARGV == 2
11    or die "Usage: $FindBin::Script HOST:PORT [COUNT]\n";
12
13# Note that it's better to run the test over the wire, because for
14# localhost the task may become CPU bound.
15my $addr = $ARGV[0];
16my $count = $ARGV[1] || 10_000;
17
18my $sock = IO::Socket::INET->new(PeerAddr => $addr,
19                                 Timeout  => 3);
20die "$!\n" unless $sock;
21
22
23# By running 'noreply' test first we also ensure there are no reply
24# packets left in the network.
25foreach my $noreply (1, 0) {
26    use Time::HiRes qw(gettimeofday tv_interval);
27
28    print "'noreply' is ", $noreply ? "enabled" : "disabled", ":\n";
29    my $param = $noreply ? 'noreply' : '';
30    my $start = [gettimeofday];
31    foreach (1 .. $count) {
32        print $sock "add foo 0 0 1 $param\r\n1\r\n";
33        scalar<$sock> unless $noreply;
34        print $sock "set foo 0 0 1 $param\r\n1\r\n";
35        scalar<$sock> unless $noreply;
36        print $sock "replace foo 0 0 1 $param\r\n1\r\n";
37        scalar<$sock> unless $noreply;
38        print $sock "append foo 0 0 1 $param\r\n1\r\n";
39        scalar<$sock> unless $noreply;
40        print $sock "prepend foo 0 0 1 $param\r\n1\r\n";
41        scalar<$sock> unless $noreply;
42        print $sock "incr foo 1 $param\r\n";
43        scalar<$sock> unless $noreply;
44        print $sock "decr foo 1 $param\r\n";
45        scalar<$sock> unless $noreply;
46        print $sock "delete foo $param\r\n";
47        scalar<$sock> unless $noreply;
48    }
49    my $end = [gettimeofday];
50    printf("update commands: %.2f secs\n\n", tv_interval($start, $end));
51}
52