1 # Copyright (C) 2018 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): $!";
48 LocalAddr => '127.0.0.1',
53 my $sock = IO::Socket::INET->new(%opts);
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;
61 PeerAddr => $host_port,
65 $s = IO::Socket::INET->new(%opts);
67 my $buf = $s->getline;
68 like($buf, qr/\A201 .* ready - post via email\r\n/s, 'got greeting');
70 my $t = timeit(10, sub {
71 ok($s->print("GROUP $group\r\n"), 'changed group');
74 diag 'GROUP took: ' . timestr($t);
76 my ($tot, $min, $max) = ($buf =~ /\A211 (\d+) (\d+) (\d+) /);
77 ok($tot && $min && $max, 'got GROUP response');
80 my $nmin = $max - $nmax;
81 $nmin = $min if $nmin < $min;
83 my $spec = "$nmin-$max";
86 sub read_until_dot ($) {
91 } until $buf eq ".\r\n";
96 $s->print("XOVER $spec\r\n");
97 $n = read_until_dot($s);
99 diag 'xover took: ' . timestr($t) . " for $n";
102 $s->print("HDR From $spec\r\n");
103 $n = read_until_dot($s);
106 diag "XHDR From ". timestr($t) . " for $n";
108 my $date = $ENV{NEWNEWS_DATE};
110 my (undef, undef, undef, $d, $m, $y) = gmtime(time - 30 * 86400);
111 $date = sprintf('%04u%02u%02u', $y + 1900, $m, $d);
112 diag "NEWNEWS_DATE undefined, using $date";
115 $s->print("NEWNEWS * $date 000000 GMT\r\n");
116 $n = read_until_dot($s);
118 diag 'newnews took: ' . timestr($t) . " for $n";