]> Sergey Matveev's repositories - public-inbox.git/blob - t/perf-nntpd.t
run update-copyrights from gnulib for 2019
[public-inbox.git] / t / perf-nntpd.t
1 # Copyright (C) 2018-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use Benchmark qw(:all :hireswallclock);
7 use PublicInbox::Inbox;
8 use File::Temp qw/tempdir/;
9 use Net::NNTP;
10 my $pi_dir = $ENV{GIANT_PI_DIR};
11 plan skip_all => "GIANT_PI_DIR not defined for $0" unless $pi_dir;
12 eval { require PublicInbox::Search };
13 my ($host_port, $group, %opts, $s, $pid);
14 require './t/common.perl';
15
16 END {
17         if ($s) {
18                 $s->print("QUIT\r\n");
19                 $s->getline;
20                 $s = undef;
21         }
22         kill 'TERM', $pid if defined $pid;
23 };
24
25 if (($ENV{NNTP_TEST_URL} || '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
26         ($host_port, $group) = ($1, $2);
27         $host_port .= ":119" unless index($host_port, ':') > 0;
28 } else {
29         $group = 'inbox.test.perf.nntpd';
30         my $ibx = { mainrepo => $pi_dir, newsgroup => $group };
31         $ibx = PublicInbox::Inbox->new($ibx);
32         my $nntpd = 'blib/script/public-inbox-nntpd';
33         my $tmpdir = tempdir('perf-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
34
35         my $pi_config = "$tmpdir/config";
36         {
37                 open my $fh, '>', $pi_config or die "open($pi_config): $!";
38                 print $fh <<"" or die "print $pi_config: $!";
39 [publicinbox "test"]
40         newsgroup = $group
41         mainrepo = $pi_dir
42         address = test\@example.com
43
44                 close $fh or die "close($pi_config): $!";
45         }
46
47         my $sock = tcp_server();
48         ok($sock, 'sock created');
49         my $cmd = [ $nntpd, '-W0' ];
50         $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
51         $host_port = $sock->sockhost . ':' . $sock->sockport;
52 }
53 %opts = (
54         PeerAddr => $host_port,
55         Proto => 'tcp',
56         Timeout => 1,
57 );
58 $s = IO::Socket::INET->new(%opts);
59 $s->autoflush(1);
60 my $buf = $s->getline;
61 like($buf, qr/\A201 .* ready - post via email\r\n/s, 'got greeting');
62
63 my $t = timeit(10, sub {
64         ok($s->print("GROUP $group\r\n"), 'changed group');
65         $buf = $s->getline;
66 });
67 diag 'GROUP took: ' . timestr($t);
68
69 my ($tot, $min, $max) = ($buf =~ /\A211 (\d+) (\d+) (\d+) /);
70 ok($tot && $min && $max, 'got GROUP response');
71 my $nr = $max - $min;
72 my $nmax = 50000;
73 my $nmin = $max - $nmax;
74 $nmin = $min if $nmin < $min;
75 my $res;
76 my $spec = "$nmin-$max";
77 my $n;
78
79 sub read_until_dot ($) {
80         my $n = 0;
81         do {
82                 $buf = $s->getline;
83                 ++$n
84         } until $buf eq ".\r\n";
85         $n;
86 }
87
88 $t = timeit(1, sub {
89         $s->print("XOVER $spec\r\n");
90         $n = read_until_dot($s);
91 });
92 diag 'xover took: ' . timestr($t) . " for $n";
93
94 $t = timeit(1, sub {
95         $s->print("HDR From $spec\r\n");
96         $n = read_until_dot($s);
97
98 });
99 diag "XHDR From ". timestr($t) . " for $n";
100
101 my $date = $ENV{NEWNEWS_DATE};
102 unless ($date) {
103         my (undef, undef, undef, $d, $m, $y) = gmtime(time - 30 * 86400);
104         $date = sprintf('%04u%02u%02u', $y + 1900, $m + 1, $d);
105         diag "NEWNEWS_DATE undefined, using $date";
106 }
107 $t = timeit(1, sub {
108         $s->print("NEWNEWS * $date 000000 GMT\r\n");
109         $n = read_until_dot($s);
110 });
111 diag 'newnews took: ' . timestr($t) . " for $n";
112
113 done_testing();
114
115 1;