app.pl 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env perl
  2. use Kelp::Less;
  3. use HTML::Escape 'escape_html';
  4. use MongoDB;
  5. use DBI;
  6. use utf8;
  7. module 'JSON::XS';
  8. my $mongo = MongoDB::MongoClient->new( host => 'localhost', port => 27017 );
  9. my $mdb = $mongo->get_database('hello_world');
  10. my $world = $mdb->get_collection('World');
  11. my $fortune = $mdb->get_collection('Fortune');
  12. my $dbh = DBI->connect(
  13. "dbi:mysql:database=hello_world;host=localhost;port=3306",
  14. 'benchmarkdbuser',
  15. 'benchmarkdbpass',
  16. { RaiseError => 0, PrintError => 0, mysql_enable_utf8 => 1 }
  17. );
  18. my @sth = map { $dbh->prepare($_) } (
  19. "SELECT * FROM World WHERE id = ?",
  20. "SELECT * FROM Fortune",
  21. "UPDATE World SET randomNumber = ? WHERE id = ?",
  22. );
  23. get '/json' => sub {
  24. { message => 'Hello, World!' };
  25. };
  26. get '/db/?db' => sub {
  27. my ( $self, $db ) = @_;
  28. query( $db // 'mongo', 1 );
  29. };
  30. get '/queries/?db' => sub {
  31. my ( $self, $db ) = @_;
  32. query( $db // 'mongo', $self->param('queries') );
  33. };
  34. get '/fortunes/?db' => sub {
  35. my ( $self, $db ) = @_;
  36. $db //= 'mongo';
  37. my @objects;
  38. if ( $db eq 'mongo' ) {
  39. my $cursor = $fortune->query( {} );
  40. @objects = $cursor->all;
  41. }
  42. else {
  43. $sth[1]->execute();
  44. @objects = @{ $sth[1]->fetchall_arrayref( {} ) };
  45. }
  46. push @objects, { id => 0, message => "Additional fortune added at request time." };
  47. fortunes( \@objects );
  48. };
  49. get '/update/?db' => sub {
  50. my ( $self, $db ) = @_;
  51. $db //= 'mongo';
  52. my $arr = query( $db, $self->param('queries') );
  53. $arr = [$arr] unless ref($arr) eq 'ARRAY';
  54. for my $row (@$arr) {
  55. $row->{randomNumber} = int( rand(10_000) ) + 1;
  56. if ( $db eq 'mongo' ) {
  57. $world->update( { _id => $row->{id} },
  58. { randomNumber => $row->{randomNumber} } );
  59. }
  60. else {
  61. $row->{randomNumber} = int( rand(10_000) ) + 1;
  62. $sth[2]->execute( $row->{randomNumber}, $row->{id} );
  63. }
  64. }
  65. return $arr;
  66. };
  67. get '/plaintext' => sub {
  68. shift->res->text->render('Hello, World!');
  69. };
  70. run;
  71. sub query {
  72. my ( $db, $count ) = @_;
  73. $count //= 1;
  74. $count = 1 if ( $count !~ /^\d+$/ || $count < 1 );
  75. $count = 500 if $count > 500;
  76. my @response;
  77. for ( 1 .. $count ) {
  78. my $id = int rand 10000 + 1;
  79. my $row;
  80. if ( $db eq 'mongo' ) {
  81. $row = $world->find_one( { _id => $id } );
  82. }
  83. else {
  84. $sth[0]->execute($id);
  85. $row = $sth[0]->fetchrow_hashref;
  86. }
  87. if ($row) {
  88. if ( $count == 1 ) {
  89. return { id => $id, randomNumber => $row->{randomNumber} };
  90. }
  91. else {
  92. push @response,
  93. { id => $id, randomNumber => $row->{randomNumber} };
  94. }
  95. }
  96. }
  97. return \@response;
  98. }
  99. sub fortunes {
  100. my ($objects) = @_;
  101. my $res = q[<!DOCTYPE html><html><head><title>Fortunes</title></head>];
  102. $res .= q[<body><table><tr><th>id</th><th>message</th></tr>];
  103. for my $item ( sort { $a->{message} cmp $b->{message} } @$objects ) {
  104. my $id = $item->{id};
  105. my $message = escape_html( $item->{message} );
  106. # HTML::Escape encodes apostrophe as &#39; because IE8 does not
  107. # support &apos;. We forse an &apos; here in order to pass the
  108. # test
  109. $message =~ s/&#39/&apos/g;
  110. $res .= "<tr><td>$id</td><td>$message</td></tr>";
  111. }
  112. $res .= q[</table></body></html>];
  113. return $res;
  114. }