app.pl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. if ($queries == 1) {
  19. my $id = int(rand 10000) + 1;
  20. $sth->execute($id);
  21. $sth->bind_col(2, \$rand);
  22. if ( my @row = $sth->fetch ) {
  23. [ 200, [ 'Content-type' => 'application/json', ],
  24. [ encode_json({ id => $id, randomNumber => $rand })] ];
  25. }
  26. }
  27. else {
  28. for ( 1 .. $queries ) {
  29. my $id = int(rand 10000) + 1;
  30. $sth->execute($id);
  31. $sth->bind_col(2, \$rand);
  32. if ( my @row = $sth->fetch ) {
  33. push @response, { id => $id, randomNumber => $rand };
  34. }
  35. }
  36. [ 200, [ 'Content-type' => 'application/json', ], [ encode_json(\@response)] ];
  37. }
  38. }
  39. }
  40. __PACKAGE__->run_if_script;