]> Sergey Matveev's repositories - public-inbox.git/blob - t/perf-nntpd.t
ce3cc409b138e9ff751bea1f0f9955589f65ae70
[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 :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         %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         my $cmd = [ $nntpd, '-W0' ];
57         $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
58         $host_port = $sock->sockhost . ':' . $sock->sockport;
59 }
60 %opts = (
61         PeerAddr => $host_port,
62         Proto => 'tcp',
63         Timeout => 1,
64 );
65 $s = IO::Socket::INET->new(%opts);
66 $s->autoflush(1);
67 my $buf = $s->getline;
68 like($buf, qr/\A201 .* ready - post via email\r\n/s, 'got greeting');
69
70 my $t = timeit(10, sub {
71         ok($s->print("GROUP $group\r\n"), 'changed group');
72         $buf = $s->getline;
73 });
74 diag 'GROUP took: ' . timestr($t);
75
76 my ($tot, $min, $max) = ($buf =~ /\A211 (\d+) (\d+) (\d+) /);
77 ok($tot && $min && $max, 'got GROUP response');
78 my $nr = $max - $min;
79 my $nmax = 50000;
80 my $nmin = $max - $nmax;
81 $nmin = $min if $nmin < $min;
82 my $res;
83 my $spec = "$nmin-$max";
84 my $n;
85
86 sub read_until_dot ($) {
87         my $n = 0;
88         do {
89                 $buf = $s->getline;
90                 ++$n
91         } until $buf eq ".\r\n";
92         $n;
93 }
94
95 $t = timeit(1, sub {
96         $s->print("XOVER $spec\r\n");
97         $n = read_until_dot($s);
98 });
99 diag 'xover took: ' . timestr($t) . " for $n";
100
101 $t = timeit(1, sub {
102         $s->print("HDR From $spec\r\n");
103         $n = read_until_dot($s);
104
105 });
106 diag "XHDR From ". timestr($t) . " for $n";
107
108 my $date = $ENV{NEWNEWS_DATE};
109 unless ($date) {
110         my (undef, undef, undef, $d, $m, $y) = gmtime(time - 30 * 86400);
111         $date = sprintf('%04u%02u%02u', $y + 1900, $m + 1, $d);
112         diag "NEWNEWS_DATE undefined, using $date";
113 }
114 $t = timeit(1, sub {
115         $s->print("NEWNEWS * $date 000000 GMT\r\n");
116         $n = read_until_dot($s);
117 });
118 diag 'newnews took: ' . timestr($t) . " for $n";
119
120 done_testing();
121
122 1;