]> Sergey Matveev's repositories - public-inbox.git/blob - xt/git_async_cmp.t
d66b371ff760153d8b2ee5b8b560d734807953b9
[public-inbox.git] / xt / git_async_cmp.t
1 #!perl -w
2 # Copyright (C) 2019-2021 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 Test::More;
6 use Benchmark qw(:all);
7 use Digest::SHA;
8 use PublicInbox::TestCommon;
9 my $git_dir = $ENV{GIANT_GIT_DIR};
10 plan 'skip_all' => "GIANT_GIT_DIR not defined for $0" unless defined($git_dir);
11 use_ok 'PublicInbox::Git';
12 my $git = PublicInbox::Git->new($git_dir);
13 my @cat = qw(cat-file --buffer --batch-check --batch-all-objects);
14 if (require_git(2.19, 1)) {
15         push @cat, '--unordered';
16 } else {
17         warn "git <2.19, cat-file lacks --unordered, locality suffers\n";
18 }
19 my @dig;
20 my $nr = $ENV{NR} || 1;
21 diag "NR=$nr";
22 my $async = timeit($nr, sub {
23         my $dig = Digest::SHA->new(1);
24         my $cb = sub {
25                 my ($bref) = @_;
26                 $dig->add($$bref);
27         };
28         my $cat = $git->popen(@cat);
29
30         while (<$cat>) {
31                 my ($oid, undef, undef) = split(/ /);
32                 $git->cat_async($oid, $cb);
33         }
34         close $cat or die "cat: $?";
35         $git->async_wait_all;
36         push @dig, ['async', $dig->hexdigest ];
37 });
38
39 my $sync = timeit($nr, sub {
40         my $dig = Digest::SHA->new(1);
41         my $cat = $git->popen(@cat);
42         while (<$cat>) {
43                 my ($oid, undef, undef) = split(/ /);
44                 my $bref = $git->cat_file($oid);
45                 $dig->add($$bref);
46         }
47         close $cat or die "cat: $?";
48         push @dig, ['sync', $dig->hexdigest ];
49 });
50
51 ok(scalar(@dig) >= 2, 'got some digests');
52 my $ref = shift @dig;
53 my $exp = $ref->[1];
54 isnt($exp, Digest::SHA->new(1)->hexdigest, 'not empty');
55 foreach (@dig) {
56         is($_->[1], $exp, "digest matches $_->[0] <=> $ref->[0]");
57 }
58 diag "sync=".timestr($sync);
59 diag "async=".timestr($async);
60 done_testing;
61 1;