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