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