| 1 | #! /usr/bin/perl |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2008 Tomash Brechko. All rights reserved. |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the same terms as Perl itself, either Perl version 5.8.8 |
|---|
| 7 | # or, at your option, any later version of Perl 5 you may have |
|---|
| 8 | # available. |
|---|
| 9 | # |
|---|
| 10 | use warnings; |
|---|
| 11 | use strict; |
|---|
| 12 | |
|---|
| 13 | # NOTE: this test uses INSTANCE_COUNT x 2 file descriptors. This |
|---|
| 14 | # means that normally spawning more than ~500 instances won't work. |
|---|
| 15 | |
|---|
| 16 | use FindBin; |
|---|
| 17 | |
|---|
| 18 | @ARGV >= 3 |
|---|
| 19 | or die "Usage: $FindBin::Script MIN_PORT INSTANCE_COUNT KEY_COUNT [SEED]\n"; |
|---|
| 20 | |
|---|
| 21 | my ($min_port, $instance_count, $key_count, $seed) = @ARGV; |
|---|
| 22 | $seed = time unless defined $seed; |
|---|
| 23 | srand($seed); |
|---|
| 24 | |
|---|
| 25 | print "Instances: $instance_count, keys: $key_count, random seed: $seed\n"; |
|---|
| 26 | |
|---|
| 27 | my $host = '127.3.5.7'; |
|---|
| 28 | |
|---|
| 29 | use Cache::Memcached::Fast; |
|---|
| 30 | use Cache::Memcached; |
|---|
| 31 | |
|---|
| 32 | my $max_port = $min_port + $instance_count - 1; |
|---|
| 33 | my @children; |
|---|
| 34 | |
|---|
| 35 | END { |
|---|
| 36 | kill 'SIGTERM', @children; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | foreach my $port ($min_port..$max_port) { |
|---|
| 40 | my $pid = fork; |
|---|
| 41 | die "Can't fork: $!\n" unless defined $pid; |
|---|
| 42 | if ($pid) { |
|---|
| 43 | push @children, $pid; |
|---|
| 44 | } else { |
|---|
| 45 | exec('memcached', '-p', $port, '-m1') == 0 |
|---|
| 46 | or die "Can't exec memcached on $host:$port: $!\n"; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | # Give memcached servers some time to become ready. |
|---|
| 51 | sleep(1); |
|---|
| 52 | |
|---|
| 53 | my @addrs = map { "$host:$_" } ($min_port..$max_port); |
|---|
| 54 | |
|---|
| 55 | my $cm = new Cache::Memcached({ servers => \@addrs, |
|---|
| 56 | select_timeout => 2 }); |
|---|
| 57 | my $cmf = new Cache::Memcached::Fast({ servers => \@addrs, |
|---|
| 58 | select_timeout => 2 }); |
|---|
| 59 | |
|---|
| 60 | foreach my $i (1..$key_count) { |
|---|
| 61 | my $key = int(rand($key_count)); |
|---|
| 62 | $cmf->set($key, $i) or die "Can't set key $key\n"; |
|---|
| 63 | my $res = $cm->get($key); |
|---|
| 64 | die "Fetch failed for key $key ($i), got @{[ defined $res |
|---|
| 65 | ? $res : '(undef)' ]}\n" |
|---|
| 66 | unless defined $res and $res == $i; |
|---|
| 67 | } |
|---|