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 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
12 my $pi_dir = $ENV{GIANT_PI_DIR};
13 plan skip_all => "GIANT_PI_DIR not defined for $0" unless $pi_dir;
14 eval { require PublicInbox::Search };
15 my ($host_port, $group, %opts, $s, $pid);
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');
59 # pretend to be systemd
60 my $fl = fcntl($sock, F_GETFD, 0);
61 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
62 dup2(1, 2) or die "dup2 failed: $!\n";
63 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
64 $ENV{LISTEN_PID} = $$;
66 $ENV{PI_CONFIG} = $pi_config;
70 ok(defined $pid, 'forked nntpd process successfully');
71 $host_port = $sock->sockhost . ':' . $sock->sockport;
74 PeerAddr => $host_port,
78 $s = IO::Socket::INET->new(%opts);
80 my $buf = $s->getline;
81 is($buf, "201 server ready - post via email\r\n", 'got greeting');
83 my $t = timeit(10, sub {
84 ok($s->print("GROUP $group\r\n"), 'changed group');
87 diag 'GROUP took: ' . timestr($t);
89 my ($tot, $min, $max) = ($buf =~ /\A211 (\d+) (\d+) (\d+) /);
90 ok($tot && $min && $max, 'got GROUP response');
93 my $nmin = $max - $nmax;
94 $nmin = $min if $nmin < $min;
96 my $spec = "$nmin-$max";
99 sub read_until_dot ($) {
104 } until $buf eq ".\r\n";
109 $s->print("XOVER $spec\r\n");
110 $n = read_until_dot($s);
112 diag 'xover took: ' . timestr($t) . " for $n";
115 $s->print("HDR From $spec\r\n");
116 $n = read_until_dot($s);
119 diag "XHDR From ". timestr($t) . " for $n";
121 my $date = $ENV{NEWNEWS_DATE};
123 my (undef, undef, undef, $d, $m, $y) = gmtime(time - 30 * 86400);
124 $date = sprintf('%04u%02u%02u', $y + 1900, $m, $d);
125 diag "NEWNEWS_DATE undefined, using $date";
128 $s->print("NEWNEWS * $date 000000 GMT\r\n");
129 $n = read_until_dot($s);
131 diag 'newnews took: ' . timestr($t) . " for $n";