Nils Diewald | 034ea70 | 2015-01-16 19:41:52 +0000 | [diff] [blame^] | 1 | |
| 2 | use Mojo::Base -strict; |
| 3 | use lib '../lib', 'lib'; |
| 4 | use Test::More; |
| 5 | use Test::Mojo; |
| 6 | use Mojo::URL; |
| 7 | use Benchmark qw/:hireswallclock/; |
| 8 | |
| 9 | my $t = Test::Mojo->new('Korap'); |
| 10 | |
| 11 | $t->app->routes->get('/searchtest')->to( |
| 12 | cb => sub { |
| 13 | my $c = shift; |
| 14 | $c->render(inline => <<'TEMPLATE'); |
| 15 | %= search query => param('q'), start_page => param('p'), no_cache => 1, begin |
| 16 | <h1><%= search->query %></h1> |
| 17 | <p id="api"><%= search->api %></p> |
| 18 | <p id="cutoff"><%= search->cutoff %></p> |
| 19 | <p id="ql"><%= search->query_language %></p> |
| 20 | <p id="no_cache"><%= search->no_cache %></p> |
| 21 | <p id="start_page"><%= search->start_page %></p> |
| 22 | <p id="total_results"><%= search->total_results %></p> |
| 23 | <p id="api_request"><%= search->api_request %></p> |
| 24 | %= search_results begin |
| 25 | <li><%= $_->{ID} %></li> |
| 26 | % end |
| 27 | % end |
| 28 | TEMPLATE |
| 29 | } |
| 30 | ); |
| 31 | |
| 32 | my $exttemplate = <<'EXTTEMPLATE'; |
| 33 | <h1><%= search->query %></h1> |
| 34 | <p id="api"><%= search->api %></p> |
| 35 | <p id="cutoff"><%= search->cutoff %></p> |
| 36 | <p id="ql"><%= search->query_language %></p> |
| 37 | <p id="no_cache"><%= search->no_cache %></p> |
| 38 | <p id="start_page"><%= search->start_page %></p> |
| 39 | <p id="total_results"><%= search->total_results %></p> |
| 40 | <p id="api_request"><%= search->api_request %></p> |
| 41 | %= search_results begin |
| 42 | <li><%= $_->{ID} %></li> |
| 43 | % end |
| 44 | EXTTEMPLATE |
| 45 | |
| 46 | |
| 47 | $t->app->routes->get('/searchasync')->to( |
| 48 | cb => sub { |
| 49 | my $c = shift; |
| 50 | $c->search( |
| 51 | query => $c->param('q'), |
| 52 | start_page => $c->param('p'), |
| 53 | no_cache => 1, |
| 54 | cb => sub { |
| 55 | return $c->render(inline => $exttemplate); |
| 56 | } |
| 57 | ); |
| 58 | } |
| 59 | ); |
| 60 | |
| 61 | my $tracetemplate = <<'TRACETEMPLATE'; |
| 62 | <h1><%= search->query %></h1> |
| 63 | <p id="api"><%= search->api %></p> |
| 64 | <p id="cutoff"><%= search->cutoff %></p> |
| 65 | <p id="ql"><%= search->query_language %></p> |
| 66 | <p id="api_request"><%= search->api_request %></p> |
| 67 | <p id="query-jsonld"><%= dumper search->query_jsonld %></p> |
| 68 | TRACETEMPLATE |
| 69 | |
| 70 | $t->app->routes->get('/traceasync')->to( |
| 71 | cb => sub { |
| 72 | my $c = shift; |
| 73 | $c->search->trace( |
| 74 | query => $c->param('q'), |
| 75 | sub { |
| 76 | return $c->render(inline => $tracetemplate); |
| 77 | } |
| 78 | ); |
| 79 | } |
| 80 | ); |
| 81 | |
| 82 | my $matchtemplate = <<'MATCHTEMPLATE'; |
| 83 | <p id="api"><%= search->api %></p> |
| 84 | <p id="api_request"><%= search->api_request %></p> |
| 85 | <p id="search-result"><%= search->results->first->{docID} %></p> |
| 86 | MATCHTEMPLATE |
| 87 | |
| 88 | |
| 89 | $t->app->routes->get('/matchinfo')->to( |
| 90 | cb => sub { |
| 91 | my $c = shift; |
| 92 | $c->search->match( |
| 93 | corpus_id => $c->param('corpus_id'), |
| 94 | doc_id => $c->param('doc_id'), |
| 95 | match_id => $c->param('match_id'), |
| 96 | foundry => '*', |
| 97 | sub { |
| 98 | return $c->render(inline => $matchtemplate); |
| 99 | } |
| 100 | ); |
| 101 | } |
| 102 | ); |
| 103 | |
| 104 | my $colltemplate = <<'COLLTEMPLATE'; |
| 105 | <p id="api"><%= search->api %></p> |
| 106 | <p id="api_request"><%= search->api_request %></p> |
| 107 | <p id="search-resource"><%= stash('search.resource')->[0]->{name} %></p> |
| 108 | COLLTEMPLATE |
| 109 | |
| 110 | $t->app->routes->get('/collectioninfo')->to( |
| 111 | cb => sub { |
| 112 | my $c = shift; |
| 113 | $c->search->resource( |
| 114 | type => 'collection', |
| 115 | sub { |
| 116 | return $c->render(inline => $colltemplate); |
| 117 | } |
| 118 | ); |
| 119 | } |
| 120 | ); |
| 121 | |
| 122 | |
| 123 | $t->app->routes->get('/collectionandsearch-parallel')->to( |
| 124 | cb => sub { |
| 125 | my $c = shift; |
| 126 | $c->delay( |
| 127 | sub { |
| 128 | my $delay = shift; |
| 129 | $c->search->resource( |
| 130 | type => 'collection', |
| 131 | $delay->begin |
| 132 | ); |
| 133 | |
| 134 | $c->search( |
| 135 | query => $c->param('q'), |
| 136 | start_page => $c->param('p'), |
| 137 | no_cache => 1, |
| 138 | cb => $delay->begin |
| 139 | ); |
| 140 | }, |
| 141 | sub { |
| 142 | return $c->render( |
| 143 | inline => $exttemplate . |
| 144 | q!<p id="search-resource"><%= stash('search.resource')->[0]->{name} %></p>! |
| 145 | ); |
| 146 | } |
| 147 | ) |
| 148 | } |
| 149 | ); |
| 150 | |
| 151 | my $query = 'startswith(<s>,[mate/m=gender:masc]{3,5})'; |
| 152 | |
| 153 | # Search everything in parallel! |
| 154 | $t->get_ok(Mojo::URL->new('/collectionandsearch-parallel')->query({q => $query})) |
| 155 | ->status_is(200) |
| 156 | ->text_is('.notify-error', '') |
| 157 | ->text_is('h1', $query) |
| 158 | ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/') |
| 159 | ->text_is('#ql', 'poliqarp') |
| 160 | ->text_is('#search-resource', 'Wikipedia'); |
| 161 | |
| 162 | $t->get_ok('/collectioninfo') |
| 163 | ->status_is(200) |
| 164 | ->text_is('#search-resource', 'Wikipedia'); |
| 165 | |
| 166 | |
| 167 | # http://10.0.10.14:6666/corpus/WPD/WWW.04738/p265-266 |
| 168 | $t->get_ok(Mojo::URL->new('/matchinfo')->query({ |
| 169 | corpus_id => 'WPD', |
| 170 | doc_id => 'WWW.04738', |
| 171 | match_id => 'p265-266' |
| 172 | })) |
| 173 | ->status_is(200) |
| 174 | ->text_is('#search-result', 'WWW.04738'); |
| 175 | |
| 176 | $t->get_ok(Mojo::URL->new('/traceasync')->query({q => $query})) |
| 177 | ->status_is(200) |
| 178 | ->text_is('.notify-error', '') |
| 179 | ->text_is('h1', $query) |
| 180 | ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/') |
| 181 | ->text_is('#ql', 'poliqarp') |
| 182 | ->text_like('#query-jsonld', qr!korap:boundary!); |
| 183 | |
| 184 | my $t0 = Benchmark->new; |
| 185 | |
| 186 | $t->get_ok(Mojo::URL->new('/searchasync')->query({q => $query})) |
| 187 | ->status_is(200) |
| 188 | ->text_is('.notify-error', '') |
| 189 | ->text_is('h1', $query) |
| 190 | ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/') |
| 191 | ->text_is('#cutoff', '') |
| 192 | ->text_is('#ql', 'poliqarp') |
| 193 | ->text_is('#no_cache', 1) |
| 194 | ->text_is('#start_page', 1) |
| 195 | ->text_is('#total_results', 54215) |
| 196 | # ->text_is('li', 'p265-266') |
| 197 | ; |
| 198 | |
| 199 | my $t1 = Benchmark->new; |
| 200 | |
| 201 | $t->get_ok(Mojo::URL->new('/searchtest')->query({q => $query})) |
| 202 | ->status_is(200) |
| 203 | ->text_is('.notify-error', '') |
| 204 | ->text_is('h1', $query) |
| 205 | ->text_is('#api', 'http://10.0.10.13:7070/api/v0.1/') |
| 206 | ->text_is('#cutoff', '') |
| 207 | ->text_is('#ql', 'poliqarp') |
| 208 | ->text_is('#no_cache', 1) |
| 209 | ->text_is('#start_page', 1) |
| 210 | ->text_is('#total_results', 54215) |
| 211 | # ->text_is('li', 'p265-266') |
| 212 | ; |
| 213 | |
| 214 | diag 'sync ' . timestr(timediff(Benchmark->new, $t1)); |
| 215 | diag 'async ' . timestr(timediff($t1, $t0)); |
| 216 | |
| 217 | # Check time_exceeded! |
| 218 | |
| 219 | done_testing; |
| 220 | __END__ |