Simon Elliott 12 years ago
parent
commit
1e69819909
1 changed files with 10 additions and 8 deletions
  1. 10 8
      web-simple/app.pl

+ 10 - 8
web-simple/app.pl

@@ -4,26 +4,28 @@ use JSON::XS;
 use DBI;
 
 my $dsn = "dbi:mysql:database=hello_world;host=localhost";
-my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
+my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', { RaiseError => 1 });
 my $sth = $dbh->prepare('SELECT * FROM World where id = ?');
 
 sub dispatch_request {
  sub (/json) {
-    [ 200, [ 'Content-type' => 'application/json; charset=utf-8', ],
+    [ 200, [ 'Content-type' => 'application/json', ],
       [ encode_json({ message => 'Hello, World!' }) ] ];
   },
   sub (/db + ?queries~) {
     my ($self, $queries) = @_;
+    $queries ||= 1;
+    my $rand;
     my @response;
-    for ( 1 .. $queries || 1 ) {
-        my $id = int rand 10000 + 1;
+    for ( 1 .. $queries ) {
+        my $id = int(rand 10000) + 1;
         $sth->execute($id);
-        if ( my @row = $sth->fetchrow_array ) {
-          push @response, { id => $id, randomNumber => $row[1] };
+        $sth->bind_col(2, \$rand);
+        if ( my @row = $sth->fetch ) {
+          push @response, { id => $id, randomNumber => $rand };
         }
     }
-    [ 200, [ 'Content-type' => 'application/json; charset=utf-8', ],
-      [ encode_json(\@response)] ];
+    [ 200, [ 'Content-type' => 'application/json', ], [ encode_json(\@response)] ];
   }
 }