app.pl 746 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Dancer ':syntax';
  5. use DBI;
  6. set serializer => 'JSON';
  7. my $dsn = "dbi:mysql:database=hello_world;host=localhost;port=3306";
  8. my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
  9. my $sth = $dbh->prepare("SELECT * FROM World where id = ?");
  10. get '/json' => sub {
  11. { message => 'Hello, World!' }
  12. };
  13. get '/db' => sub {
  14. my $queries = params->{queries} || 1;
  15. my @response;
  16. for ( 1 .. $queries ) {
  17. my $id = int rand 10000 + 1;
  18. $sth->execute($id);
  19. if ( my $row = $sth->fetchrow_hashref ) {
  20. push @response,
  21. { id => $id, randomNumber => $row->{randomNumber} };
  22. }
  23. }
  24. return \@response;
  25. };
  26. Dancer->dance;