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>
6 use Benchmark qw(:all :hireswallclock);
7 use PublicInbox::Inbox;
8 use File::Temp qw/tempdir/;
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';
18 $s->print("QUIT\r\n");
22 kill 'TERM', $pid if defined $pid;
25 if (($ENV{NNTP_TEST_URL} || '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
26 ($host_port, $group) = ($1, $2);
27 $host_port .= ":119" unless index($host_port, ':') > 0;
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);
35 my $pi_config = "$tmpdir/config";
37 open my $fh, '>', $pi_config or die "open($pi_config): $!";
38 print $fh <<"" or die "print $pi_config: $!";
42 address = test\@example.com
44 close $fh or die "close($pi_config): $!";
47 my $sock = tcp_server();
48 ok($sock, 'sock created');
49 my $cmd = [ $nntpd, '-W0' ];
50 $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
51 $host_port = $sock->sockhost . ':' . $sock->sockport;
54 PeerAddr => $host_port,
58 $s = IO::Socket::INET->new(%opts);
60 my $buf = $s->getline;
61 like($buf, qr/\A201 .* ready - post via email\r\n/s, 'got greeting');
63 my $t = timeit(10, sub {
64 ok($s->print("GROUP $group\r\n"), 'changed group');
67 diag 'GROUP took: ' . timestr($t);
69 my ($tot, $min, $max) = ($buf =~ /\A211 (\d+) (\d+) (\d+) /);
70 ok($tot && $min && $max, 'got GROUP response');
73 my $nmin = $max - $nmax;
74 $nmin = $min if $nmin < $min;
76 my $spec = "$nmin-$max";
79 sub read_until_dot ($) {
84 } until $buf eq ".\r\n";
89 $s->print("XOVER $spec\r\n");
90 $n = read_until_dot($s);
92 diag 'xover took: ' . timestr($t) . " for $n";
95 $s->print("HDR From $spec\r\n");
96 $n = read_until_dot($s);
99 diag "XHDR From ". timestr($t) . " for $n";
101 my $date = $ENV{NEWNEWS_DATE};
103 my (undef, undef, undef, $d, $m, $y) = gmtime(time - 30 * 86400);
104 $date = sprintf('%04u%02u%02u', $y + 1900, $m + 1, $d);
105 diag "NEWNEWS_DATE undefined, using $date";
108 $s->print("NEWNEWS * $date 000000 GMT\r\n");
109 $n = read_until_dot($s);
111 diag 'newnews took: ' . timestr($t) . " for $n";