]> Sergey Matveev's repositories - public-inbox.git/blob - xt/perf-nntpd.t
85db036c78997063bc3ef7a538086c155e955612
[public-inbox.git] / xt / perf-nntpd.t
1 # Copyright (C) 2018-2021 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 Net::NNTP;
9 my $inboxdir = $ENV{GIANT_INBOX_DIR} // $ENV{GIANT_PI_DIR};
10 plan skip_all => "GIANT_INBOX_DIR not defined for $0" unless defined($inboxdir);
11 my ($host_port, $group, $s, $td, $tmp_obj);
12 use PublicInbox::TestCommon;
13
14 if (($ENV{NNTP_TEST_URL} || '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
15         ($host_port, $group) = ($1, $2);
16         $host_port .= ":119" unless index($host_port, ':') > 0;
17         my $six = substr($host_port, 0, 1) eq '[' ? '6' : '';
18         my $cls = "IO::Socket::INET$six";
19         $cls->new(Proto => 'tcp', Timeout => 1, PeerAddr => $host_port);
20 } else {
21         $group = 'inbox.test.perf.nntpd';
22         my $ibx = { inboxdir => $inboxdir, newsgroup => $group };
23         $ibx = PublicInbox::Inbox->new($ibx);
24         my $tmpdir;
25         ($tmpdir, $tmp_obj) = tmpdir();
26
27         my $pi_config = "$tmpdir/config";
28         {
29                 open my $fh, '>', $pi_config or die "open($pi_config): $!";
30                 print $fh <<"" or die "print $pi_config: $!";
31 [publicinbox "test"]
32         newsgroup = $group
33         inboxdir = $inboxdir
34         address = test\@example.com
35
36                 close $fh or die "close($pi_config): $!";
37         }
38
39         my $sock = tcp_server();
40         my $cmd = [ '-nntpd', '-W0' ];
41         $td = start_script($cmd, { PI_CONFIG => $pi_config }, { 3 => $sock });
42         $host_port = tcp_host_port($sock);
43         $s = tcp_connect($sock);
44 }
45 my $buf = $s->getline;
46 like($buf, qr/\A201 .* ready - post via email\r\n/s, 'got greeting');
47
48 my $t = timeit(10, sub {
49         ok($s->print("GROUP $group\r\n"), 'changed group');
50         $buf = $s->getline;
51 });
52 diag 'GROUP took: ' . timestr($t);
53
54 my ($tot, $min, $max) = ($buf =~ /\A211 (\d+) (\d+) (\d+) /);
55 ok($tot && $min && $max, 'got GROUP response');
56 my $nr = $max - $min;
57 my $nmax = 50000;
58 my $nmin = $max - $nmax;
59 $nmin = $min if $nmin < $min;
60 my $res;
61 my $spec = "$nmin-$max";
62 my $n;
63
64 sub read_until_dot ($) {
65         my $n = 0;
66         do {
67                 $buf = $s->getline;
68                 ++$n
69         } until $buf eq ".\r\n";
70         $n;
71 }
72
73 $t = timeit(1, sub {
74         $s->print("XOVER $spec\r\n");
75         $n = read_until_dot($s);
76 });
77 diag 'xover took: ' . timestr($t) . " for $n";
78
79 $t = timeit(1, sub {
80         $s->print("HDR From $spec\r\n");
81         $n = read_until_dot($s);
82
83 });
84 diag "XHDR From ". timestr($t) . " for $n";
85
86 my $date = $ENV{NEWNEWS_DATE};
87 unless ($date) {
88         my (undef, undef, undef, $d, $m, $y) = gmtime(time - 30 * 86400);
89         $date = sprintf('%04u%02u%02u', $y + 1900, $m + 1, $d);
90         diag "NEWNEWS_DATE undefined, using $date";
91 }
92 $t = timeit(1, sub {
93         $s->print("NEWNEWS * $date 000000 GMT\r\n");
94         $n = read_until_dot($s);
95 });
96 diag 'newnews took: ' . timestr($t) . " for $n";
97
98 if ($s) {
99         $s->print("QUIT\r\n");
100         $s->getline;
101 }
102
103
104 done_testing();
105
106 1;