app.pl 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Dancer ':syntax';
  5. use DBI;
  6. use JSON::XS; # Ensure that the fast implementation of the serializer is installed
  7. set serializer => 'JSON';
  8. my $dsn = "dbi:mysql:database=hello_world;host=tfb-database;port=3306";
  9. my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', { mysql_auto_reconnect=>1 } );
  10. my $sth = $dbh->prepare("SELECT * FROM World where id = ?");
  11. get '/json' => sub {
  12. { message => 'Hello, World!' }
  13. };
  14. get '/dbquery' => sub {
  15. my $queries = params->{queries} || 1;
  16. $queries = 1 if ( $queries !~ /^\d+$/ || $queries < 1 );
  17. $queries = 500 if $queries > 500;
  18. my @response;
  19. for ( 1 .. $queries ) {
  20. my $id = int rand 10000 + 1;
  21. $sth->execute($id);
  22. if ( my $row = $sth->fetchrow_hashref ) {
  23. push @response,
  24. { id => $id, randomNumber => $row->{randomNumber} };
  25. }
  26. }
  27. return \@response;
  28. };
  29. get '/db' => sub {
  30. my $id = int rand 10000 + 1;
  31. $sth->execute($id);
  32. if ( my $row = $sth->fetchrow_hashref ) {
  33. return { id => $id, randomNumber => $row->{randomNumber} };
  34. }
  35. };
  36. Dancer->dance;