vividsnow 12 years ago
parent
commit
5a828a3650
5 changed files with 69 additions and 10 deletions
  1. 25 0
      plack/README.md
  2. 12 10
      plack/app.psgi
  3. 10 0
      plack/benchmark_config
  4. 1 0
      plack/cpanfile
  5. 21 0
      plack/setup.py

+ 25 - 0
plack/README.md

@@ -0,0 +1,25 @@
+# About
+
+Plack
+
+# Setup
+
+* Perl 5.12+
+* MySQL 5.5
+* Wrk 2.0
+
+# Requirements
+
+* Plack
+* Monoceros or Starman
+* EV
+* HTTP::Parser::XS
+* JSON::XS
+* DBI
+* DBD::mysql
+
+# Deployment
+
+    plackup -E production -s Starman --workers=2 -l :8080 app.psgi
+    - or -
+    plackup -E production -s Monoceros --max-workers=2 -l :8080 app.psgi

+ 12 - 10
plack/app.psgi

@@ -1,23 +1,25 @@
-# plackup -s Starman --workers N -E deployment app.psgi
-# -or-
-# plackup -s Twiggy::Prefork --max_workers N -E deployment app.psgi
 use v5.16;
 use v5.16;
 use Plack::Builder;
 use Plack::Builder;
 use Plack::Request;
 use Plack::Request;
 use JSON::XS 'encode_json';
 use JSON::XS 'encode_json';
 use DBI;
 use DBI;
 
 
-my $dbh = DBI->connect('dbi:mysql:dbname=test', 'root') || die $!;
-my $sth = $dbh->prepare('SELECT randomnumber FROM world WHERE id = ?');
-my $header = ['Content-Type' => 'application/json'];
+my $dbh = DBI->connect_cached(
+    'dbi:mysql:database=hello_world;host=localhost;port=3306', 
+    qw(benchmarkdbuser benchmarkdbpassword)
+) || die $!;
+
+my $sth = $dbh->prepare_cached('SELECT randomNumber FROM World WHERE id = ?');
+my $header = [qw(Content-Type application/json)];
 
 
 builder {
 builder {
     mount '/json' => sub { [ 200, $header, [ encode_json({ message => 'Hello, World!' })] ] },
     mount '/json' => sub { [ 200, $header, [ encode_json({ message => 'Hello, World!' })] ] },
-    mount '/dbi' => sub { [ 200, $header, [ encode_json([
-        map { id => $_->[0] + 0, randomnumber => $_->[1] },
+    mount '/db' => sub { [ 200, $header, [ encode_json([
+        map { id => $_->[0] + 0, randomNumber => $_->[1] },
         grep exists $_->[1],
         grep exists $_->[1],
         map [$_, $sth->execute($_) && $sth->fetchrow_array],
         map [$_, $sth->execute($_) && $sth->fetchrow_array],
         map int rand 10000 + 1,
         map int rand 10000 + 1,
         1..Plack::Request->new(shift)->param('queries')//1
         1..Plack::Request->new(shift)->param('queries')//1
-    ]) ] ] }
-};
+    ]) ] ] } };
+
+

+ 10 - 0
plack/benchmark_config

@@ -0,0 +1,10 @@
+{
+  "framework": "plack",
+  "tests": [{
+    "default": {
+      "setup_file": "setup.py",
+      "port": 8080,
+      "sort": 118
+    }
+  }]
+}

+ 1 - 0
plack/cpanfile

@@ -0,0 +1 @@
+map requires($_), qw(JSON::XS EV HTTP::Parser::XS Plack DBI DBD::mysql DBI Monoceros Starman);

+ 21 - 0
plack/setup.py

@@ -0,0 +1,21 @@
+import subprocess
+import sys
+import setup_util
+import os
+
+def start(args):
+  setup_util.replace_text("plack/app.psgi", "localhost", ""+ args.database_host +"")
+  try:
+    subprocess.check_call("curl -L http://cpanmin.us | perl - App::cpanminus", shell=True, cwd="plack")
+    subprocess.check_call("cpanm --installdeps .", shell=True, cwd="plack")
+    pid = subprocess.Popen("plackup -E production -s Monoceros -l :8080 --max-workers=" + str(args.max_threads) + " app.psgi", shell=True, cwd="plack").pid
+    open('plack/app.pid', 'w').write(str(pid))
+    return 0
+  except subprocess.CalledProcessError:
+    return 1
+def stop():
+  try:
+    subprocess.Popen("kill -TERM $(ps --ppid `cat app.pid` -o pid --no-header)", shell=True, cwd="plack")
+    return 0
+  except subprocess.CalledProcessError:
+    return 1