Browse Source

Merge remote-tracking branch 'hamiltont/kelp-944' into perl

Conflicts:
	kelp/install.sh
Joel Berger 11 years ago
parent
commit
943fc35856
7 changed files with 107 additions and 42 deletions
  1. 25 9
      kelp/README.md
  2. 62 10
      kelp/app.pl
  3. 6 20
      kelp/benchmark_config
  4. 1 1
      kelp/conf/config.pl
  5. 1 1
      kelp/install.sh
  6. 2 1
      kelp/nginx.conf
  7. 10 0
      kelp/views/fortunes.tt

+ 25 - 9
kelp/README.md

@@ -6,18 +6,34 @@
 
 # Requirements
 
-* Kelp
-* DBD::mysql
-* Starman (if using Starman as web server)
-* Plack (for plackup)
-* nginx (if you want to front Dancer with nginx, nginx.conf provided)
+* Kelp (install from CPAN)
+* Kelp::Module::JSON::XS (install from CPAN)
+* DBD::mysql (install from CPAN)
+* Starman (install from CPAN)
+* nginx (if you want to front with nginx, nginx.conf provided)
 
 # Deployment
 
-Something along the lines of
+## uWSGI (Recommended)
 
-    plackup -E production -s Starman --workers=2 -l /tmp/frameworks-benchmark.sock -a ./app.pl
+1. Create a configuration file. Bare bones example of app.ini:
 
-if you want to front it with nginx, otherwise
+    [uwsgi]
+    http-socket = :8080
+    psgi = app.pl
 
-    plackup -E production -s Starman --port=8080 --workers=2 -a ./app.pl
+2. Make sure you have installed the psgi plugin.
+
+3. Deploy with uwsgi
+
+    uwsgi --http-modifier1 5 --plugin psgi --ini app.ini
+
+## Plack + Starman
+
+1. Deploy via plackup
+
+    plackup -E production -s Starman --workers=5 -l /tmp/frameworks-benchmark.sock -a ./app.pl
+
+2. If you want to front it with nginx, otherwise
+
+    plackup -E production -s Starman --port=8080 --workers=5 -a ./app.pl

+ 62 - 10
kelp/app.pl

@@ -2,23 +2,77 @@
 use Kelp::Less;
 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 {
-    { message => 'Hello, World!' }
+    my $self = shift;
+    { message => 'Hello, World!' };
 };
 
 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 $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;
-    for ( 1 .. $queries ) {
+    for ( 1 .. $count ) {
         my $id = int rand 10000 + 1;
         $sth->execute($id);
         if ( my $row = $sth->fetchrow_hashref ) {
-            if ( $queries == 1 ) {
+            if ( $count == 1 ) {
                 return { id => $id, randomNumber => $row->{randomNumber} };
             }
             else {
@@ -28,6 +82,4 @@ get '/db' => sub {
         }
     }
     return \@response;
-};
-
-run;
+}

+ 6 - 20
kelp/benchmark_config

@@ -4,32 +4,18 @@
     "default": {
       "setup_file": "setup",
       "json_url": "/json",
-      "port": 8080,
-      "approach": "Realistic",
-      "classification": "Fullstack",
-      "database": "None",
-      "framework": "kelp",
-      "language": "Perl",
-      "orm": "Full",
-      "platform": "Plack",
-      "webserver": "Starman",
-      "os": "Linux",
-      "database_os": "Linux",
-      "display_name": "kelp",
-      "notes": "",
-      "versus": ""
-    },
-    "raw": {
-      "setup_file": "setup",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortune",
+      "update_url": "/update?queries=",
+      "plaintext_url": "/plaintext",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Fullstack",
       "database": "MySQL",
       "framework": "kelp",
       "language": "Perl",
-      "orm": "Raw",
+      "orm": "Full",
       "platform": "Plack",
       "webserver": "Starman",
       "os": "Linux",
@@ -39,4 +25,4 @@
       "versus": ""
     }
   }]
-}
+}

+ 1 - 1
kelp/conf/config.pl

@@ -1,3 +1,3 @@
 {
-    modules => ['JSON::XS']
+    modules    => ['JSON::XS', 'Template'],
 }

+ 1 - 1
kelp/install.sh

@@ -1,6 +1,6 @@
 #!/bin/bash
 
-fw_depends perl
+fw_depends perl nginx
 
 cpanm --notest --no-man-page Kelp DBI DBD::mysql Kelp::Module::JSON::XS Plack Starman
 echo installed Kelp app dependencies

+ 2 - 1
kelp/nginx.conf

@@ -1,4 +1,5 @@
-user tfb;
+# Replaced by setup.py
+user USR;
 error_log stderr error;
 
 worker_processes 2;

+ 10 - 0
kelp/views/fortunes.tt

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+<head><title>Fortunes</title></head>
+<body>
+<table>
+<tr><th>id</th><th>message</th></tr>
+[% FOREACH item IN fortunes  %]<tr><td>[% item.id %]</td><td>[% item.message %]</td></tr>[% END %]
+</table>
+</body>
+</html>