]> Sergey Matveev's repositories - public-inbox.git/blob - xt/perf-msgview.t
7f92ce858a2e4cb5172a6832447c9f0fe26b14bd
[public-inbox.git] / xt / perf-msgview.t
1 #!perl -w
2 # Copyright (C) all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use v5.10.1;
6 use PublicInbox::TestCommon;
7 use Benchmark qw(:all);
8 use PublicInbox::Inbox;
9 use PublicInbox::View;
10 use PublicInbox::WwwStream;
11
12 my $inboxdir = $ENV{GIANT_INBOX_DIR} // $ENV{GIANT_PI_DIR};
13 my $blob = $ENV{TEST_BLOB};
14 plan skip_all => "GIANT_INBOX_DIR not defined for $0" unless $inboxdir;
15
16 my @cat = qw(cat-file --buffer --batch-check --batch-all-objects);
17 if (require_git(2.19, 1)) {
18         push @cat, '--unordered';
19 } else {
20         warn
21 "git <2.19, cat-file lacks --unordered, locality suffers\n";
22 }
23 require_mods qw(Plack::Util);
24 my $ibx = PublicInbox::Inbox->new({ inboxdir => $inboxdir, name => 'name' });
25 my $git = $ibx->git;
26 my $fh = $blob ? undef : $git->popen(@cat);
27 if ($fh) {
28         my $vec = '';
29         vec($vec, fileno($fh), 1) = 1;
30         select($vec, undef, undef, 60) or
31                 die "timed out waiting for --batch-check";
32 }
33
34 my $ctx = bless {
35         env => { HTTP_HOST => 'example.com', 'psgi.url_scheme' => 'https' },
36         ibx => $ibx,
37         www => Plack::Util::inline_object(style => sub {''}),
38         gz => PublicInbox::GzipFilter::gzip_or_die(),
39 }, 'PublicInbox::WwwStream';
40 my ($eml, $res, $oid, $type);
41 my $n = 0;
42 my $m = 0;
43 ${$ctx->{obuf}} = '';
44 $ctx->{mhref} = '../';
45
46 my $cb = sub {
47         $eml = PublicInbox::Eml->new(shift);
48         $eml->each_part(\&PublicInbox::View::add_text_body, $ctx, 1);
49         $ctx->zflush;
50         ++$m;
51         delete $ctx->{zbuf};
52         ${$ctx->{obuf}} = '';
53 };
54
55 my $t = timeit(1, sub {
56         if (defined $blob) {
57                 my $nr = $ENV{NR} // 10000;
58                 for (1..$nr) {
59                         ++$n;
60                         $git->cat_async($blob, $cb);
61                 }
62         } else {
63                 while (<$fh>) {
64                         ($oid, $type) = split / /;
65                         next if $type ne 'blob';
66                         ++$n;
67                         $git->cat_async($oid, $cb);
68                 }
69         }
70         $git->async_wait_all;
71 });
72 diag 'add_text_body took '.timestr($t)." for $n <=> $m messages";
73 is($m, $n, 'rendered all messages');
74 done_testing();