app.pl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. use Mojolicious::Lite;
  2. use Mojo::Pg;
  3. use Cpanel::JSON::XS 'encode_json';
  4. use Scalar::Util 'looks_like_number';
  5. # configuration
  6. {
  7. my $nproc = `nproc`;
  8. app->config(hypnotoad => {
  9. accepts => 0,
  10. clients => int( 256 / $nproc ) + 1,
  11. graceful_timeout => 1,
  12. requests => 10000,
  13. workers => $nproc,
  14. });
  15. }
  16. {
  17. my $db_host = $ENV{DBHOST} || 'localhost';
  18. helper pg => sub { state $pg = Mojo::Pg->new('postgresql://benchmarkdbuser:benchmarkdbpass@' . $db_host . '/hello_world') };
  19. }
  20. helper render_json => sub {
  21. my $c = shift;
  22. $c->res->headers->content_type('application/json');
  23. $c->render( data => encode_json(shift) );
  24. };
  25. # Routes
  26. get '/json' => sub { shift->helpers->render_json({message => 'Hello, World!'}) };
  27. get '/db' => sub { shift->helpers->render_query(1, {single => 1}) };
  28. get '/queries' => sub {
  29. my $c = shift;
  30. $c->helpers->render_query(scalar $c->param('queries'));
  31. };
  32. get '/fortunes' => sub {
  33. my $c = shift;
  34. my $docs = $c->helpers->pg->db->query('SELECT id, message FROM Fortune')->arrays;
  35. push @$docs, [0, 'Additional fortune added at request time.'];
  36. $c->render( fortunes => docs => $docs->sort(sub{ $a->[1] cmp $b->[1] }) );
  37. };
  38. get '/updates' => sub {
  39. my $c = shift;
  40. $c->helpers->render_query(scalar $c->param('queries'), {update => 1});
  41. };
  42. get '/plaintext' => sub {
  43. my $c = shift;
  44. $c->res->headers->content_type('text/plain');
  45. $c->render( text => 'Hello, World!' );
  46. };
  47. # Additional helpers (shared code)
  48. helper 'render_query' => sub {
  49. my ($self, $q, $args) = @_;
  50. $args ||= {};
  51. my $update = $args->{update};
  52. $q = 1 unless looks_like_number($q);
  53. $q = 1 if $q < 1;
  54. $q = 500 if $q > 500;
  55. my $r = [];
  56. my $tx = $self->tx;
  57. my $db = $self->helpers->pg->db;
  58. foreach (1 .. $q) {
  59. my $id = int rand 10_000;
  60. my $randomNumber = $db->query('SELECT randomnumber FROM World WHERE id=?', $id)->array->[0];
  61. $db->query('UPDATE World SET randomnumber=? WHERE id=?', ($randomNumber = 1 + int rand 10_000), $id) if $update;
  62. push @$r, { id => $id, randomNumber => $randomNumber };
  63. }
  64. $r = $r->[0] if $args->{single};
  65. $self->helpers->render_json($r);
  66. };
  67. app->start;
  68. __DATA__
  69. @@ fortunes.html.ep
  70. <!DOCTYPE html>
  71. <html>
  72. <head><title>Fortunes</title></head>
  73. <body>
  74. <table>
  75. <tr><th>id</th><th>message</th></tr>
  76. % foreach my $doc (@$docs) {
  77. <tr>
  78. <td><%= $doc->[0] %></td>
  79. <td><%= $doc->[1] %></td>
  80. </tr>
  81. % }
  82. </table>
  83. </body>
  84. </html>