File Coverage

blib/lib/BenchmarkAnything/Storage/Frontend/HTTP.pm
Criterion Covered Total %
statement 33 37 89.1
branch 1 2 50.0
condition 3 6 50.0
subroutine 5 7 71.4
pod 1 1 100.0
total 43 53 81.1


line stmt bran cond sub pod time code
1 2     2   765543 use 5.008;
  2         7  
2 2     2   13 use strict;
  2         4  
  2         69  
3 2     2   10 use warnings;
  2         3  
  2         158  
4             package BenchmarkAnything::Storage::Frontend::HTTP;
5             # git description: v0.009-1-g424b68f
6              
7             our $AUTHORITY = 'cpan:SCHWIGON';
8             # ABSTRACT: Access a BenchmarkAnything store via HTTP
9             $BenchmarkAnything::Storage::Frontend::HTTP::VERSION = '0.010';
10 2     2   9 use Mojo::Base 'Mojolicious';
  2         3  
  2         17  
11              
12             require File::HomeDir; # MUST 'require', 'use' conflicts with Mojolicious
13              
14             has bacfg => sub
15             {
16             require BenchmarkAnything::Config;
17             return BenchmarkAnything::Config->new;
18             };
19              
20             has balib => sub
21             {
22             my $self = shift;
23              
24             require BenchmarkAnything::Storage::Frontend::Lib;
25             return BenchmarkAnything::Storage::Frontend::Lib->new;
26             };
27              
28             has backend => sub
29             {
30             my $self = shift;
31              
32             require BenchmarkAnything::Storage::Backend::SQL;
33             return BenchmarkAnything::Storage::Backend::SQL->new ({ dbh => $self->app->balib->{dbh}, debug => 0 });
34             };
35              
36              
37              
38             # This method will run once at server start.
39             #
40             # IMPORTANT:
41             # ----------
42             # YOU MUST NOT CALL ->balib() INSIDE startup()!
43             # THAT WOULD INSTANTIATE THE SAME DB CONNECTION FOR MULTIPLE
44             # PREFORKED PROCESSES AND THEREFORE MIX UP TRANSACTIONS.
45             sub startup {
46 2     2 1 23456 my $self = shift;
47              
48 2         16 $self->log->debug("Using BenchmarkAnything");
49 2         340 $self->log->debug(" - Configfile: ".$self->app->bacfg->{cfgfile});
50 2         3933 $self->log->debug(" - Backend: ".$self->app->bacfg->{benchmarkanything}{backend});
51 2         198 $self->log->debug(" - DSN: ".$self->app->bacfg->{benchmarkanything}{storage}{backend}{sql}{dsn});
52             die
53             "Config backend:".$self->app->bacfg->{benchmarkanything}{backend}.
54             "' not yet supported (".$self->app->bacfg->{cfgfile}.
55             "), must be 'local'.\n"
56 2 50       154 if $self->app->bacfg->{benchmarkanything}{backend} ne 'local';
57              
58 2   50     123 my $queueing_processing_batch_size = $self->app->bacfg->{benchmarkanything}{storage}{backend}{sql}{queueing}{processing_batch_size} || 100;
59 2   50     126 my $queueing_processing_sleep = $self->app->bacfg->{benchmarkanything}{storage}{backend}{sql}{queueing}{processing_sleep} || 30;
60 2   50     128 my $queueing_gc_sleep = $self->app->bacfg->{benchmarkanything}{storage}{backend}{sql}{queueing}{gc_sleep} || 120;
61              
62 2         122 $self->log->debug(" - Q.batch_size: $queueing_processing_batch_size");
63 2         42 $self->log->debug(" - Q.sleep: $queueing_processing_sleep");
64 2         33 $self->log->debug(" - Q.gc_sleep: $queueing_gc_sleep");
65              
66 2         35 $self->plugin('InstallablePaths');
67              
68             # recurrinbox worker
69             Mojo::IOLoop->recurring($queueing_processing_sleep => sub {
70 0     0   0 $self->log->debug("process bench queue (batchsize: $queueing_processing_batch_size) [".~~localtime."]");
71 0         0 $self->app->balib->process_raw_result_queue($queueing_processing_batch_size);
72 2         8039 });
73             Mojo::IOLoop->recurring($queueing_gc_sleep => sub {
74 0     0   0 $self->log->debug("garbage collection [".~~localtime."]");
75 0         0 $self->app->balib->gc();
76 2         271 });
77              
78             # routes
79 2         83 my $routes = $self->routes;
80 2         42 $routes
81             ->any('/api/v1/search/:value_id' => [value_id => qr/\d+/])
82             ->to('search#search', value_id => 0);
83 2         809 $routes
84             ->any('/api/v1/listnames/:pattern' => [pattern => qr/[^\/]+/])
85             ->to('search#listnames', pattern => '');
86 2         521 $routes
87             ->any('/api/v1/listkeys/:pattern' => [pattern => qr/[^\/]+/])
88             ->to('search#listkeys', pattern => '');
89 2         525 $routes
90             ->any('/api/v1/hello')
91             ->to('search#hello');
92 2         470 $routes
93             ->any('/api/v1/add')
94             ->to('submit#add');
95 2         427 $routes
96             ->any('/api/v1/stats')
97             ->to('search#stats');
98             }
99              
100             1;
101              
102             __END__
103              
104             =pod
105              
106             =encoding UTF-8
107              
108             =head1 NAME
109              
110             BenchmarkAnything::Storage::Frontend::HTTP - Access a BenchmarkAnything store via HTTP
111              
112             =head1 AUTHOR
113              
114             Steffen Schwigon <ss5@renormalist.net>
115              
116             =head1 COPYRIGHT AND LICENSE
117              
118             This software is copyright (c) 2017 by Steffen Schwigon.
119              
120             This is free software; you can redistribute it and/or modify it under
121             the same terms as the Perl 5 programming language system itself.
122              
123             =cut