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