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