]> Sergey Matveev's repositories - public-inbox.git/blob - xt/eml_check_limits.t
cf780c77babaac401b72b6a38eb4925a1bc660c2
[public-inbox.git] / xt / eml_check_limits.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use Test::More;
5 use PublicInbox::TestCommon;
6 use PublicInbox::Eml;
7 use PublicInbox::Inbox;
8 use List::Util qw(max);
9 use Benchmark qw(:all :hireswallclock);
10 use PublicInbox::Spawn qw(popen_rd);
11 use Carp ();
12 require_git(2.19); # for --unordered
13 require_mods(qw(BSD::Resource));
14 BSD::Resource->import(qw(getrusage));
15 my $cls = $ENV{TEST_CLASS};
16 if ($cls) {
17         diag "TEST_CLASS=$cls";
18         require_mods($cls);
19 }
20 $cls //= 'PublicInbox::Eml';
21 my $inboxdir = $ENV{GIANT_INBOX_DIR};
22 plan skip_all => "GIANT_INBOX_DIR not defined for $0" unless $inboxdir;
23 local $PublicInbox::Eml::mime_nesting_limit = 0x7fffffff;
24 local $PublicInbox::Eml::mime_parts_limit = 0x7fffffff;
25 local $PublicInbox::Eml::header_size_limit = 0x7fffffff;
26 my $ibx = PublicInbox::Inbox->new({ inboxdir => $inboxdir, name => 'x' });
27 my $git = $ibx->git;
28 my @cat = qw(cat-file --buffer --batch-check --batch-all-objects --unordered);
29 my $fh = $git->popen(@cat);
30 my ($m, $n);
31 my $max_nest = [ 0, '' ]; # [ bytes, blob oid ]
32 my $max_idx = [ 0, '' ];
33 my $max_parts = [ 0, '' ];
34 my $max_size = [ 0, '' ];
35 my $max_hdr = [ 0, '' ];
36 my $info = [ 0, '' ];
37 my $each_part_cb = sub {
38         my ($p) = @_;
39         my ($part, $depth, $idx) = @$p;
40         $max_nest = [ $depth, $info->[1] ] if $depth > $max_nest->[0];
41         my $max = max(split(/\./, $idx));
42         $max_idx = [ $max, $info->[1] ] if $max > $max_idx->[0];
43         ++$info->[0];
44 };
45
46 my ($bref, $oid, $size);
47 local $SIG{__WARN__} = sub { diag "$inboxdir $oid ", @_ };
48 my $cat_cb = sub {
49         ($bref, $oid, undef, $size) = @_;
50         ++$m;
51         $info = [ 0, $oid ];
52         my $eml = $cls->new($bref);
53         my $hdr_len = length($eml->header_obj->as_string);
54         $max_hdr = [ $hdr_len, $oid ] if $hdr_len > $max_hdr->[0];
55         $eml->each_part($each_part_cb, $info, 1);
56         $max_parts = $info if $info->[0] > $max_parts->[0];
57         $max_size = [ $size, $oid ] if $size > $max_size->[0];
58 };
59
60 my $t = timeit(1, sub {
61         my ($blob, $type);
62         while (<$fh>) {
63                 ($blob, $type) = split / /;
64                 next if $type ne 'blob';
65                 ++$n;
66                 $git->cat_async($blob, $cat_cb);
67         }
68         $git->cat_async_wait;
69 });
70 is($m, $n, 'scanned all messages');
71 diag "$$ $inboxdir took ".timestr($t)." for $n <=> $m messages";
72 diag "$$ max_nest $max_nest->[0] @ $max_nest->[1]";
73 diag "$$ max_idx $max_idx->[0] @ $max_idx->[1]";
74 diag "$$ max_parts $max_parts->[0] @ $max_parts->[1]";
75 diag "$$ max_size $max_size->[0] @ $max_size->[1]";
76 diag "$$ max_hdr $max_hdr->[0] @ $max_hdr->[1]";
77 diag "$$ RSS ".getrusage()->maxrss. ' k';
78 done_testing;