|
@@ -2,23 +2,77 @@
|
|
use Kelp::Less;
|
|
use Kelp::Less;
|
|
use DBI;
|
|
use DBI;
|
|
|
|
|
|
-my $dsn = "dbi:mysql:database=hello_world;host=localhost;port=3306";
|
|
|
|
-my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
|
|
|
|
-my $sth = $dbh->prepare("SELECT * FROM World where id = ?");
|
|
|
|
|
|
+my $dsn = "dbi:mysql:database=hello_world;host=localhost;port=3306";
|
|
|
|
+my $dbh = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
|
|
|
|
+my $sth = $dbh->prepare("SELECT * FROM World WHERE id = ?");
|
|
|
|
+my $sth1 = $dbh->prepare("SELECT * FROM Fortune");
|
|
|
|
+my $sth2 = $dbh->prepare("UPDATE World SET randomNumber = ? WHERE id = ?");
|
|
|
|
+
|
|
|
|
+get '/populate' => sub {
|
|
|
|
+ $dbh->do("DELETE FROM World");
|
|
|
|
+ $dbh->do("DELETE FROM Fortune");
|
|
|
|
+ srand;
|
|
|
|
+
|
|
|
|
+ # Add some random numbers
|
|
|
|
+ my @rand = map {'(' . $_ . ',' . int(rand(10_000)) . ')'} (1 .. 10_000);
|
|
|
|
+ $dbh->do(q[INSERT INTO World (id, randomNumber) VALUES ] . join(',', @rand));
|
|
|
|
+
|
|
|
|
+ # Add some fortunes
|
|
|
|
+ my @fortunes = map { '("' . 'x' x (int(rand(20)) + 1) . '")' } (1 .. 30);
|
|
|
|
+ $dbh->do(q[INSERT INTO Fortune (message) VALUES ] . join(',', @fortunes));
|
|
|
|
+
|
|
|
|
+ "OK";
|
|
|
|
+};
|
|
|
|
|
|
get '/json' => sub {
|
|
get '/json' => sub {
|
|
- { message => 'Hello, World!' }
|
|
|
|
|
|
+ my $self = shift;
|
|
|
|
+ { message => 'Hello, World!' };
|
|
};
|
|
};
|
|
|
|
|
|
get '/db' => sub {
|
|
get '/db' => sub {
|
|
|
|
+ query(1);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+get '/queries' => sub {
|
|
|
|
+ my $self = shift;
|
|
|
|
+ my $count = $self->param('queries') || 1;
|
|
|
|
+ query( $count > 500 ? 500 : $count );
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+get '/fortunes' => sub {
|
|
my $self = shift;
|
|
my $self = shift;
|
|
- my $queries = $self->param('queries') || 1;
|
|
|
|
|
|
+ $sth1->execute();
|
|
|
|
+ my $fortunes = $sth1->fetchall_arrayref({});
|
|
|
|
+ $self->template( 'fortunes', { fortunes => $fortunes } );
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+get '/updates' => sub {
|
|
|
|
+ my $self = shift;
|
|
|
|
+ my $count = $self->param('queries');
|
|
|
|
+ my $arr = query( $count > 500 ? 500 : $count );
|
|
|
|
+ for my $row (@$arr) {
|
|
|
|
+ $row->{randomNumber} = int( rand(10_000) ) + 1;
|
|
|
|
+ $sth2->execute( $row->{randomNumber}, $row->{id} );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $arr;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+get '/plaintext' => sub {
|
|
|
|
+ my $self = shift;
|
|
|
|
+ $self->res->text->render('Hello, World!');
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+run;
|
|
|
|
+
|
|
|
|
+sub query {
|
|
|
|
+ my $count = shift;
|
|
my @response;
|
|
my @response;
|
|
- for ( 1 .. $queries ) {
|
|
|
|
|
|
+ for ( 1 .. $count ) {
|
|
my $id = int rand 10000 + 1;
|
|
my $id = int rand 10000 + 1;
|
|
$sth->execute($id);
|
|
$sth->execute($id);
|
|
if ( my $row = $sth->fetchrow_hashref ) {
|
|
if ( my $row = $sth->fetchrow_hashref ) {
|
|
- if ( $queries == 1 ) {
|
|
|
|
|
|
+ if ( $count == 1 ) {
|
|
return { id => $id, randomNumber => $row->{randomNumber} };
|
|
return { id => $id, randomNumber => $row->{randomNumber} };
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
@@ -28,6 +82,4 @@ get '/db' => sub {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return \@response;
|
|
return \@response;
|
|
-};
|
|
|
|
-
|
|
|
|
-run;
|
|
|
|
|
|
+}
|