annotate t/memcached-keepalive.t @ 1:ca955a7f651b

Keepalive: common cache for all backends. Known limitations: Connections cached to the upstream pool as a whole, not to individual backend servers, and therefore load distribution may be less than ideal (especially under light load).
author Maxim Dounin <mdounin@mdounin.ru>
date Wed, 22 Oct 2008 03:52:29 +0400
parents
children bef88ba0b378
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
1 #!/usr/bin/perl
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
2
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
3 # (C) Maxim Dounin
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
4
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
5 # Test for memcached with keepalive.
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
6
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
7 ###############################################################################
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
8
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
9 use warnings;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
10 use strict;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
11
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
12 use Test::More;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
13 use Test::Nginx;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
14
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
15 ###############################################################################
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
16
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
17 select STDERR; $| = 1;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
18 select STDOUT; $| = 1;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
19
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
20 eval { require Cache::Memcached; };
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
21 plain(skip_all => 'Cache::Memcached not installed') if $@;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
22
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(7)
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
24 ->write_file_expand('nginx.conf', <<'EOF');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
25
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
26 master_process off;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
27 daemon off;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
28
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
29 events {
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
30 worker_connections 1024;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
31 }
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
32
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
33 http {
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
34 access_log off;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
35
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
36 client_body_temp_path %%TESTDIR%%/client_body_temp;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
37 fastcgi_temp_path %%TESTDIR%%/fastcgi_temp;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
38 proxy_temp_path %%TESTDIR%%/proxy_temp;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
39
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
40 upstream memd {
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
41 server 127.0.0.1:8081;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
42 keepalive;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
43 }
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
44
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
45 server {
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
46 listen localhost:8080;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
47 server_name localhost;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
48
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
49 location / {
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
50 set $memcached_key $uri;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
51 memcached_pass memd;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
52 }
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
53
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
54 location /next {
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
55 set $memcached_key $uri;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
56 memcached_next_upstream not_found;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
57 memcached_pass memd;
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
58 }
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
59 }
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
60 }
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
61
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
62 EOF
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
63
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
64 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
65 $t->run();
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
66
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
67 ###############################################################################
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
68
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
69 my $memd = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]);
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
70 $memd->set('/', 'SEE-THIS');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
71
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
72 my $total = $memd->stats()->{total}->{total_connections};
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
73
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
74 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
75 like(http_get('/notfound'), qr/404/, 'keepalive memcached not found');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
76 like(http_get('/next'), qr/404/,
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
77 'keepalive not found with memcached_next_upstream');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
78 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
79 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
80 like(http_get('/'), qr/SEE-THIS/, 'keepalive memcached request again');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
81
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
82 is($memd->stats()->{total}->{total_connections}, $total + 1, 'keepalive used');
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
83
ca955a7f651b Keepalive: common cache for all backends.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
84 ###############################################################################