app.pl 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env perl
  2. use Mojolicious::Lite;
  3. use JSON::XS;
  4. use Memoize;
  5. use DBI;
  6. my $dsn = "dbi:mysql:database=hello_world;host=localhost;port=3306";
  7. my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
  8. my $sth = $dbh->prepare("SELECT * FROM World where id = ?");
  9. get '/json' => sub {
  10. my $self = shift;
  11. $self->render( text => JSON::XS::encode_json( { message => 'Hello, world!' } ), format => 'json' );
  12. };
  13. get '/db' => sub {
  14. my $self = shift;
  15. my $queries = $self->param('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. $self->render( json => { id => $id, randomNumber => $row->{randomNumber} } );
  23. }
  24. else {
  25. push @response,
  26. { id => $id, randomNumber => $row->{randomNumber} };
  27. }
  28. }
  29. }
  30. if ( $queries > 1 ) {
  31. $self->render( json => \@response );
  32. }
  33. };
  34. app->start;