Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | use strict; |
| 3 | use warnings; |
| 4 | use Dumbbench; |
| 5 | use File::Basename 'dirname'; |
| 6 | use File::Spec::Functions qw/catfile rel2abs/; |
Akron | 2d547bc | 2020-07-04 10:34:35 +0200 | [diff] [blame] | 7 | use File::Temp 'tempfile'; |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 8 | use Encode qw!decode!; |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 9 | use FindBin; |
| 10 | use Getopt::Long; |
| 11 | |
| 12 | BEGIN { |
| 13 | unshift @INC, "$FindBin::Bin/../lib"; |
| 14 | }; |
| 15 | |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 16 | use Test::KorAP::XML::TEI qw!korap_tempfile!; |
Akron | 95bc98a | 2020-07-11 12:00:12 +0200 | [diff] [blame] | 17 | use KorAP::XML::TEI 'remove_xml_comments'; |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 18 | use KorAP::XML::TEI::Tokenizer::Aggressive; |
| 19 | use KorAP::XML::TEI::Tokenizer::Conservative; |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 20 | |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 21 | my $columns = 0; |
| 22 | my $no_header = 0; |
| 23 | GetOptions( |
| 24 | 'columns|c' => \$columns, |
| 25 | 'no-header|n' => \$no_header, |
| 26 | 'help|h' => sub { |
| 27 | print "--columns|-c Print instances in columns\n"; |
| 28 | print "--no-header|-n Dismiss benchmark names\n"; |
| 29 | print "--help|-h Print this page\n\n"; |
| 30 | exit(0); |
| 31 | } |
| 32 | ); |
| 33 | |
| 34 | our $SCRIPT_NAME = 'tei2korapxml'; |
| 35 | |
| 36 | my $f = dirname(__FILE__); |
| 37 | my $script = rel2abs(catfile($f, '..', 'script', $SCRIPT_NAME)); |
| 38 | |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 39 | # Load example files |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 40 | my $file = rel2abs(catfile($f, '..', 't', 'data', 'goe_sample.i5.xml')); |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 41 | my $goe_tagged = rel2abs(catfile($f, '..', 't', 'data', 'goe_sample_tagged.i5.xml')); |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 42 | |
| 43 | # Create a new benchmark object |
| 44 | my $bench = Dumbbench->new( |
| 45 | verbosity => 0 |
| 46 | ); |
| 47 | |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 48 | my $result; |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 49 | |
| 50 | # Data for delHTMLcom-long |
Peter Harders | 42e18a6 | 2020-07-21 02:43:26 +0200 | [diff] [blame] | 51 | my ($fh, $filename) = korap_tempfile('benchmark'); |
Akron | 2d547bc | 2020-07-04 10:34:35 +0200 | [diff] [blame] | 52 | |
| 53 | print $fh <<'HTML'; |
| 54 | mehrzeiliger |
| 55 | Kommentar |
| 56 | --><!-- Versuch |
| 57 | -->ist <!-- a --><!-- b --> ein Test |
| 58 | HTML |
| 59 | |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 60 | # Data for Tokenization |
| 61 | # Test data |
| 62 | my $t_dataf = catfile(dirname(__FILE__), '..', 't', 'data', 'wikipedia.txt'); |
| 63 | my $t_data = ''; |
| 64 | if ((open(FH, '<' . $t_dataf))) { |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 65 | binmode(FH); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 66 | while (!eof(FH)) { |
| 67 | $t_data .= <FH> |
| 68 | }; |
| 69 | close(FH); |
| 70 | } |
| 71 | else { |
| 72 | die "Unable to load $t_dataf"; |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 75 | my $t_data_utf_8 = decode('utf-8',$t_data); |
| 76 | |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 77 | my $cons_tok = KorAP::XML::TEI::Tokenizer::Conservative->new; |
| 78 | my $aggr_tok = KorAP::XML::TEI::Tokenizer::Aggressive->new; |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 79 | |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 80 | |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 81 | # Add benchmark instances |
| 82 | $bench->add_instances( |
| 83 | Dumbbench::Instance::PerlSub->new( |
| 84 | name => 'SimpleConversion', |
| 85 | code => sub { |
Peter Harders | f9c5124 | 2020-07-21 02:37:44 +0200 | [diff] [blame] | 86 | `cat '$file' | perl '$script' -ti > /dev/null 2>&1` |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 87 | } |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 88 | ), |
| 89 | Dumbbench::Instance::PerlSub->new( |
Akron | e68ec0c | 2020-07-28 18:06:19 +0200 | [diff] [blame] | 90 | name => 'Conversion-with-inline-annotations', |
| 91 | code => sub { |
| 92 | `cat '$goe_tagged' | KORAPXMLTEI_INLINE=1 perl '$script' > /dev/null 2>&1` |
| 93 | } |
| 94 | ), |
| 95 | Dumbbench::Instance::PerlSub->new( |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 96 | name => 'delHTMLcom', |
| 97 | code => sub { |
| 98 | for (1..100_000) { |
Akron | 95bc98a | 2020-07-11 12:00:12 +0200 | [diff] [blame] | 99 | $result = remove_xml_comments( |
Akron | 4f67cd4 | 2020-07-02 12:27:58 +0200 | [diff] [blame] | 100 | \*STDIN, |
| 101 | "This <!-- comment --> is a test " . $_ |
| 102 | ); |
| 103 | }; |
| 104 | } |
| 105 | ), |
Akron | 2d547bc | 2020-07-04 10:34:35 +0200 | [diff] [blame] | 106 | Dumbbench::Instance::PerlSub->new( |
| 107 | name => 'delHTMLcom-long', |
| 108 | code => sub { |
| 109 | for (1..10_000) { |
Akron | 95bc98a | 2020-07-11 12:00:12 +0200 | [diff] [blame] | 110 | $result = remove_xml_comments( |
Akron | 2d547bc | 2020-07-04 10:34:35 +0200 | [diff] [blame] | 111 | $fh, |
| 112 | "This <!--" . $_ |
| 113 | ); |
| 114 | seek($fh, 0, 0); |
| 115 | }; |
| 116 | } |
| 117 | ), |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 118 | Dumbbench::Instance::PerlSub->new( |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 119 | name => 'Tokenizer-conservative', |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 120 | code => sub { |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 121 | $result = $cons_tok->reset->tokenize($t_data); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 122 | $result = 0; |
| 123 | } |
| 124 | ), |
| 125 | Dumbbench::Instance::PerlSub->new( |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 126 | name => 'Tokenizer-conservative-utf-8', |
| 127 | code => sub { |
| 128 | $result = $cons_tok->reset->tokenize($t_data_utf_8); |
| 129 | $result = 0; |
| 130 | } |
| 131 | ), |
| 132 | Dumbbench::Instance::PerlSub->new( |
Akron | d962747 | 2020-07-09 16:53:09 +0200 | [diff] [blame] | 133 | name => 'Tokenizer-aggressive', |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 134 | code => sub { |
Peter Harders | b122717 | 2020-07-21 02:12:10 +0200 | [diff] [blame] | 135 | $result = $aggr_tok->reset->tokenize($t_data); |
Akron | 510a88c | 2020-07-07 10:16:50 +0200 | [diff] [blame] | 136 | $result = 0; |
| 137 | } |
| 138 | ), |
Peter Harders | 994aff7 | 2020-07-25 09:53:35 +0200 | [diff] [blame] | 139 | Dumbbench::Instance::PerlSub->new( |
| 140 | name => 'Tokenizer-aggressive-utf-8', |
| 141 | code => sub { |
| 142 | $result = $aggr_tok->reset->tokenize($t_data_utf_8); |
| 143 | $result = 0; |
| 144 | } |
| 145 | ) |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 146 | ); |
| 147 | |
| 148 | # Run benchmarks |
| 149 | $bench->run; |
| 150 | |
Akron | 2d547bc | 2020-07-04 10:34:35 +0200 | [diff] [blame] | 151 | # Clean up |
| 152 | close($fh); |
| 153 | |
Akron | aa229a2 | 2020-02-18 13:44:25 +0100 | [diff] [blame] | 154 | # Output in a single row |
| 155 | if ($columns) { |
| 156 | unless ($no_header) { |
| 157 | print join("\t", map { $_->name } $bench->instances), "\n"; |
| 158 | }; |
| 159 | print join("\t", map { $_->result->raw_number } $bench->instances), "\n"; |
| 160 | exit(0); |
| 161 | }; |
| 162 | |
| 163 | # Output simple timings for comparation |
| 164 | foreach my $inst ($bench->instances) { |
| 165 | unless ($no_header) { |
| 166 | print $inst->name, ': '; |
| 167 | }; |
| 168 | print $inst->result->raw_number, "\n"; |
| 169 | }; |
| 170 | |
| 171 | exit(0); |
| 172 | |
| 173 | __END__ |