]> Sergey Matveev's repositories - public-inbox.git/blob - t/perf-nntpd.t
nntp: make XOVER, XHDR, OVER, HDR and NEWNEWS faster
[public-inbox.git] / t / perf-nntpd.t
1 # Copyright (C) 2018 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);
7 use PublicInbox::Inbox;
8 use File::Temp qw/tempdir/;
9 use POSIX qw(dup2);
10 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
11 use Net::NNTP;
12 my $pi_dir = $ENV{GIANT_PI_DIR};
13 plan skip_all => "GIANT_PI_DIR not defined for $0" unless $pi_dir;
14 eval { require PublicInbox::Search };
15 my ($host_port, $group, %opts, $s, $pid);
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         %opts = (
48                 LocalAddr => '127.0.0.1',
49                 ReuseAddr => 1,
50                 Proto => 'tcp',
51                 Listen => 1024,
52         );
53         my $sock = IO::Socket::INET->new(%opts);
54
55         ok($sock, 'sock created');
56         $! = 0;
57         $pid = fork;
58         if ($pid == 0) {
59                 # pretend to be systemd
60                 my $fl = fcntl($sock, F_GETFD, 0);
61                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
62                 dup2(1, 2) or die "dup2 failed: $!\n";
63                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
64                 $ENV{LISTEN_PID} = $$;
65                 $ENV{LISTEN_FDS} = 1;
66                 $ENV{PI_CONFIG} = $pi_config;
67                 exec $nntpd, '-W0';
68                 die "FAIL: $!\n";
69         }
70         ok(defined $pid, 'forked nntpd process successfully');
71         $host_port = $sock->sockhost . ':' . $sock->sockport;
72 }
73 %opts = (
74         PeerAddr => $host_port,
75         Proto => 'tcp',
76         Timeout => 1,
77 );
78 $s = IO::Socket::INET->new(%opts);
79 $s->autoflush(1);
80 my $buf = $s->getline;
81 is($buf, "201 server ready - post via email\r\n", 'got greeting');
82 ok($s->print("GROUP $group\r\n"), 'changed group');
83 $buf = $s->getline;
84 my ($tot, $min, $max) = ($buf =~ /\A211 (\d+) (\d+) (\d+) /);
85 ok($tot && $min && $max, 'got GROUP response');
86 my $nr = $max - $min;
87 my $nmax = 50000;
88 my $nmin = $max - $nmax;
89 $nmin = $min if $nmin < $min;
90 my $res;
91 my $spec = "$nmin-$max";
92 my $n;
93
94 sub read_until_dot ($) {
95         my $n = 0;
96         do {
97                 $buf = $s->getline;
98                 ++$n
99         } until $buf eq ".\r\n";
100         $n;
101 }
102
103 my $t = timeit(1, sub {
104         $s->print("XOVER $spec\r\n");
105         $n = read_until_dot($s);
106 });
107 diag 'xover took: ' . timestr($t) . " for $n";
108
109 $t = timeit(1, sub {
110         $s->print("HDR From $spec\r\n");
111         $n = read_until_dot($s);
112
113 });
114 diag "XHDR From ". timestr($t) . " for $n";
115
116 my $date = $ENV{NEWNEWS_DATE};
117 unless ($date) {
118         my (undef, undef, undef, $d, $m, $y) = gmtime(time - 30 * 86400);
119         $date = sprintf('%04u%02u%02u', $y + 1900, $m, $d);
120         diag "NEWNEWS_DATE undefined, using $date";
121 }
122 $t = timeit(1, sub {
123         $s->print("NEWNEWS * $date 000000 GMT\r\n");
124         $n = read_until_dot($s);
125 });
126 diag 'newnews took: ' . timestr($t) . " for $n";
127
128 done_testing();
129
130 1;