app.pl 996 B

1234567891011121314151617181920212223242526272829303132333435363738
  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=localhost;port=3306";
  9. my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
  10. my $sth = $dbh->prepare("SELECT * FROM World where id = ?");
  11. get '/json' => sub {
  12. { message => 'Hello, World!' }
  13. };
  14. get '/db' => sub {
  15. my $queries = params->{queries} || 1;
  16. my @response;
  17. for ( 1 .. $queries ) {
  18. my $id = int rand 10000 + 1;
  19. $sth->execute($id);
  20. if ( my $row = $sth->fetchrow_hashref ) {
  21. if ( $queries == 1 ) {
  22. return { id => $id, randomNumber => $row->{randomNumber} };
  23. }
  24. else {
  25. push @response,
  26. { id => $id, randomNumber => $row->{randomNumber} };
  27. }
  28. }
  29. }
  30. return \@response;
  31. };
  32. Dancer->dance;