|
| 1 | +#!/usr/bin/perl |
| 2 | + |
| 3 | +# Copyright (C) 2002-2016 Igor Sysoev |
| 4 | +# Copyright (C) 2011-2016 Nginx, Inc. |
| 5 | +# Copyright (C) 2020 Google Inc. |
| 6 | +# All rights reserved. |
| 7 | +# |
| 8 | +# Redistribution and use in source and binary forms, with or without |
| 9 | +# modification, are permitted provided that the following conditions |
| 10 | +# are met: |
| 11 | +# 1. Redistributions of source code must retain the above copyright |
| 12 | +# notice, this list of conditions and the following disclaimer. |
| 13 | +# 2. Redistributions in binary form must reproduce the above copyright |
| 14 | +# notice, this list of conditions and the following disclaimer in the |
| 15 | +# documentation and/or other materials provided with the distribution. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | +# SUCH DAMAGE. |
| 28 | + |
| 29 | +# tests for the nginx latency status module |
| 30 | +# run with TEST_NGINX_BINARY={path to nginx binary} prove http_latency_status.t |
| 31 | + |
| 32 | +############################################################################### |
| 33 | + |
| 34 | +use warnings; |
| 35 | +use strict; |
| 36 | + |
| 37 | +use Test::More; |
| 38 | + |
| 39 | +BEGIN { use FindBin; chdir($FindBin::Bin); } |
| 40 | + |
| 41 | +use lib "../nginx-tests/lib"; |
| 42 | +use Test::Nginx; |
| 43 | +use JSON::PP qw(decode_json); |
| 44 | +############################################################################### |
| 45 | + |
| 46 | +select STDERR; $| = 1; |
| 47 | +select STDOUT; $| = 1; |
| 48 | + |
| 49 | +my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(17); |
| 50 | + |
| 51 | +$t->write_file_expand('nginx.conf', <<'EOF'); |
| 52 | +%%TEST_GLOBALS%% |
| 53 | +daemon off; |
| 54 | +events { |
| 55 | +} |
| 56 | +http { |
| 57 | + %%TEST_GLOBALS_HTTP%% |
| 58 | + store_latency 2 100 1; |
| 59 | + upstream u { |
| 60 | + server 127.0.0.1:8082; |
| 61 | + } |
| 62 | + server { |
| 63 | + listen 127.0.0.1:8080; |
| 64 | + server_name localhost; |
| 65 | + location /status { |
| 66 | + latency_stub_status; |
| 67 | + } |
| 68 | + location /success { |
| 69 | + return 200 "TEST\n"; |
| 70 | + record_latency on; |
| 71 | + } |
| 72 | + location /pass { |
| 73 | + proxy_pass http://u/; |
| 74 | + record_latency on; |
| 75 | + } |
| 76 | + location /error { |
| 77 | + return 404; |
| 78 | + record_latency on; |
| 79 | + } |
| 80 | + location /pass-to-monitored { |
| 81 | + proxy_pass http://localhost:8081; |
| 82 | + } |
| 83 | + } |
| 84 | + server { |
| 85 | + listen 127.0.0.1:8082; |
| 86 | + server_name localhost; |
| 87 | + location / { |
| 88 | + return 200 "TEST\n"; |
| 89 | + } |
| 90 | + } |
| 91 | + server { |
| 92 | + listen 127.0.0.1:8081; |
| 93 | + server_name localhost; |
| 94 | + record_latency on; |
| 95 | + location / { |
| 96 | + return 200 "TEST\n"; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | +EOF |
| 101 | + |
| 102 | +$t->run(); |
| 103 | + |
| 104 | +my $initial_status = "{\n \"accepted_connections\": 1,\n". |
| 105 | + " \"handled_connections\": 1,\n". |
| 106 | + " \"active_connections\": 1,\n". |
| 107 | + " \"requests\": 1,\n". |
| 108 | + " \"reading_connections\": 0,\n". |
| 109 | + " \"writing_connections\": 1,\n". |
| 110 | + " \"waiting_connections\": 0,\n". |
| 111 | + " \"request_latency\":{\n". |
| 112 | + " \"latency_sum\": 0,\n". |
| 113 | + " \"request_count\": 0,\n". |
| 114 | + " \"sum_squares\": 0,\n". |
| 115 | + " \"distribution\": [0, 0, 0]\n". |
| 116 | + " },\n". |
| 117 | + " \"upstream_latency\":{\n". |
| 118 | + " \"latency_sum\": 0,\n". |
| 119 | + " \"request_count\": 0,\n". |
| 120 | + " \"sum_squares\": 0,\n". |
| 121 | + " \"distribution\": [0, 0, 0]\n". |
| 122 | + " },\n". |
| 123 | + " \"websocket_latency\":{\n". |
| 124 | + " \"latency_sum\": 0,\n". |
| 125 | + " \"request_count\": 0,\n". |
| 126 | + " \"sum_squares\": 0,\n". |
| 127 | + " \"distribution\": [0, 0, 0]\n". |
| 128 | + " },\n". |
| 129 | + " \"latency_bucket_bounds\": [ 100, 200]\n". |
| 130 | + "}\n"; |
| 131 | + |
| 132 | +like(http_get('/status'), qr/\Q${initial_status}\E/, 'check initial stats twice'); |
| 133 | + |
| 134 | +http_get('/success'); |
| 135 | +my $status = decode_json(http_get_body('/status')); |
| 136 | + |
| 137 | +is($status->{'request_latency'}{'request_count'}, 1, 'request count after 1 monitored request'); |
| 138 | +is(get_distribution_sum($status, 'request_latency'), 1, 'The sum of the distribution bucket counts after 1 monitored request'); |
| 139 | +is($status->{'upstream_latency'}{'request_count'}, 0, 'upstream request count when there have been no proxied requests'); |
| 140 | +is(get_distribution_sum($status, 'upstream_latency'), 0, 'sum of upstream distribution bucket counts when there haven been no proxied requests'); |
| 141 | +is($status->{'websocket_latency'}{'request_count'}, 0, 'websocket request count when there have been no websocket requests'); |
| 142 | +is(get_distribution_sum($status, 'websocket_latency'), 0, 'sum of websocket distribution bucket counts when there have been no websocket requests'); |
| 143 | + |
| 144 | +http_get('/error'); |
| 145 | +$status = decode_json(http_get_body('/status')); |
| 146 | + |
| 147 | +is($status->{'request_latency'}{'request_count'}, 2, 'erroring requests add to the request count'); |
| 148 | +is(get_distribution_sum($status, 'request_latency'), 2, 'erroring requests add to distribution bucket counts'); |
| 149 | + |
| 150 | +http_get('/pass'); |
| 151 | +$status = decode_json(http_get_body('/status')); |
| 152 | + |
| 153 | +is($status->{'request_latency'}{'request_count'}, 3, 'proxied requests add to the request count'); |
| 154 | +is(get_distribution_sum($status, 'request_latency'), 3, 'proxied requests add to the distribution bucket counts'); |
| 155 | +is($status->{'upstream_latency'}{'request_count'}, 1, 'proxied requests add to the upstream request count'); |
| 156 | +is(get_distribution_sum($status, 'upstream_latency'), 1, 'proxied requests add to the upstream distribution bucket counts'); |
| 157 | +is($status->{'websocket_latency'}{'request_count'}, 0, 'a non-websocket proxied request does not add to the websocket request count'); |
| 158 | +is(get_distribution_sum($status, 'websocket_latency'), 0, 'non-websocket proxied request does not add to websocket distribution bucket counts'); |
| 159 | + |
| 160 | +http_get('/pass-to-monitored'); |
| 161 | +$status = decode_json(http_get_body('/status')); |
| 162 | + |
| 163 | +is($status->{'request_latency'}{'request_count'}, 4, 'record_latency set at the server level adds to the request count'); |
| 164 | +is(get_distribution_sum($status, 'request_latency'), 4, 'record_latency set at the server level adds to the distribution bucket counts'); |
| 165 | + |
| 166 | +sub get_distribution_sum { |
| 167 | + my ($status, $distribution_name) = @_; |
| 168 | + |
| 169 | + my $latency_count = 0; |
| 170 | + my @distribution = $status->{$distribution_name}{'distribution'}; |
| 171 | + for(my $i = 0; $i < @distribution; $i++) { |
| 172 | + $latency_count += $status->{$distribution_name}{'distribution'}[$i]; |
| 173 | + } |
| 174 | + return $latency_count; |
| 175 | +} |
| 176 | + |
| 177 | +sub http_get_body { |
| 178 | + my ($uri) = @_; |
| 179 | + |
| 180 | + return undef if !defined $uri; |
| 181 | + |
| 182 | + my $text = http_get($uri); |
| 183 | + |
| 184 | + if ($text !~ /(.*?)\x0d\x0a?\x0d\x0a?(.*)/ms) { |
| 185 | + return undef; |
| 186 | + } |
| 187 | + |
| 188 | + return $2; |
| 189 | +} |
0 commit comments