app.pl 902 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env perl
  2. use Web::Simple;
  3. use JSON::XS;
  4. use DBI;
  5. my $dsn = "dbi:mysql:database=hello_world;host=localhost";
  6. my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', { RaiseError => 1 });
  7. my $sth = $dbh->prepare('SELECT * FROM World where id = ?');
  8. sub dispatch_request {
  9. sub (/json) {
  10. [ 200, [ 'Content-type' => 'application/json', ],
  11. [ encode_json({ message => 'Hello, World!' }) ] ];
  12. },
  13. sub (/db + ?queries~) {
  14. my ($self, $queries) = @_;
  15. $queries ||= 1;
  16. my $rand;
  17. my @response;
  18. for ( 1 .. $queries ) {
  19. my $id = int(rand 10000) + 1;
  20. $sth->execute($id);
  21. $sth->bind_col(2, \$rand);
  22. if ( my @row = $sth->fetch ) {
  23. push @response, { id => $id, randomNumber => $rand };
  24. }
  25. }
  26. [ 200, [ 'Content-type' => 'application/json', ], [ encode_json(\@response)] ];
  27. }
  28. }
  29. __PACKAGE__->run_if_script;