Browse Source

use nginx as reverse proxy

Masahiro Nagano 11 years ago
parent
commit
c2c94a6bd9
5 changed files with 51 additions and 7 deletions
  1. 3 1
      plack/app.psgi
  2. 2 2
      plack/benchmark_config
  3. 32 0
      plack/nginx.conf
  4. 12 4
      plack/setup.py
  5. 2 0
      plack/source_code

+ 3 - 1
plack/app.psgi

@@ -13,17 +13,19 @@ my $dbh = DBI->connect_cached(
 
 
 my $query = 'SELECT id, randomNumber FROM World WHERE id = ?';
 my $query = 'SELECT id, randomNumber FROM World WHERE id = ?';
 my $header = [qw(Content-Type application/json)];
 my $header = [qw(Content-Type application/json)];
+my $message = { message => 'Hello, World!' };
 
 
 my $app = sub {
 my $app = sub {
     my $env = shift;
     my $env = shift;
     if ( $env->{PATH_INFO} eq '/json' ) {
     if ( $env->{PATH_INFO} eq '/json' ) {
-        return [ 200, $header, [ encode_json({ message => 'Hello, World!' }) ]];
+        return [ 200, $header, [ encode_json($message) ]];
     }
     }
     elsif ( $env->{PATH_INFO} eq '/db' ) {
     elsif ( $env->{PATH_INFO} eq '/db' ) {
         my ($n) = ($env->{QUERY_STRING} || "" ) =~ m!queries=(\d+)!;
         my ($n) = ($env->{QUERY_STRING} || "" ) =~ m!queries=(\d+)!;
         $n //= 1;
         $n //= 1;
         my @rs = map {{id=>$_->[0]+0,randomNumber=>$_->[1]+0}} 
         my @rs = map {{id=>$_->[0]+0,randomNumber=>$_->[1]+0}} 
             map { $dbh->selectrow_arrayref($query,{},int rand 10000 + 1) } 1..$n;
             map { $dbh->selectrow_arrayref($query,{},int rand 10000 + 1) } 1..$n;
+        return [ 200, $header, [ '{}' ]] unless @rs;
         return [ 200, $header, [ encode_json( @rs > 1 ? \@rs : $rs[0] ) ]];
         return [ 200, $header, [ encode_json( @rs > 1 ? \@rs : $rs[0] ) ]];
     }
     }
     [ 404, [], ['not found']];
     [ 404, [], ['not found']];

+ 2 - 2
plack/benchmark_config

@@ -12,8 +12,8 @@
       "framework": "plack",
       "framework": "plack",
       "language": "Perl",
       "language": "Perl",
       "orm": "Raw",
       "orm": "Raw",
-      "platform": "Plack",
-      "webserver": "Starlet",
+      "platform": "Starlet",
+      "webserver": "nginx",
       "os": "Linux",
       "os": "Linux",
       "database_os": "Linux",
       "database_os": "Linux",
       "display_name": "plack",
       "display_name": "plack",

+ 32 - 0
plack/nginx.conf

@@ -0,0 +1,32 @@
+user tfb;
+error_log stderr error;
+
+worker_processes 4;
+
+events {
+  worker_connections  4096;
+  multi_accept on;
+}
+
+http {
+  access_log       off;
+  tcp_nodelay      on;
+  keepalive_requests 50000;
+  etag off;
+
+  upstream backendurl {
+    server unix:/home/tfb/FrameworkBenchmarks/plack/app.sock;
+    keepalive 20;
+  }
+
+  server {
+    listen 8080;
+    server_name localhost;
+    location / {
+      proxy_http_version 1.1;
+      proxy_set_header Connection "";
+      proxy_pass http://backendurl;
+    }
+  }
+}
+

+ 12 - 4
plack/setup.py

@@ -1,21 +1,29 @@
 import subprocess
 import subprocess
 import sys
 import sys
 import setup_util
 import setup_util
+from os.path import expanduser
 import os
 import os
+import getpass
+
+home = expanduser("~")
 
 
 def start(args, logfile, errfile):
 def start(args, logfile, errfile):
   setup_util.replace_text("plack/app.psgi", "localhost", ""+ args.database_host +"")
   setup_util.replace_text("plack/app.psgi", "localhost", ""+ args.database_host +"")
+  setup_util.replace_text("plack/nginx.conf", "USR", getpass.getuser())
+  setup_util.replace_text("plack/nginx.conf", "server unix:.*\/FrameworkBenchmarks", "server unix:" + home + "/FrameworkBenchmarks")
   try:
   try:
     subprocess.check_call("curl -L http://cpanmin.us | perl - App::cpanminus", shell=True, cwd="plack", stderr=errfile, stdout=logfile)
     subprocess.check_call("curl -L http://cpanmin.us | perl - App::cpanminus", shell=True, cwd="plack", stderr=errfile, stdout=logfile)
-    subprocess.check_call("cpanm --notest --no-man-pages --installdeps .", shell=True, cwd="plack", stderr=errfile, stdout=logfile)
-    pid = subprocess.Popen("plackup -E production -s Starlet -l :8080 --max-keepalive-reqs 5000 --max-reqs-per-child 50000 --min-reqs-per-child 40000 --max-workers=" + str(args.max_threads+4) + " app.psgi", shell=True, cwd="plack", stderr=errfile, stdout=logfile).pid
-    open('plack/app.pid', 'w').write(str(pid))
+    subprocess.check_call("cpanm --notest --no-man-pages --installdeps "+home+"/FrameworkBenchmarks/plack", shell=True, cwd="plack", stderr=errfile, stdout=logfile)
+    subprocess.Popen("start_server --backlog=16384 --pid-file="+home+"/FrameworkBenchmarks/plack/app.pid --path="+home+"/FrameworkBenchmarks/plack/app.sock -- plackup -E production -s Starlet --max-keepalive-reqs 1000 --max-reqs-per-child 50000 --min-reqs-per-child 40000 --max-workers=" + str(args.max_threads+128) + " -a "+home+"/FrameworkBenchmarks/plack/app.psgi", shell=True, cwd="plack", stderr=errfile, stdout=logfile)
+    subprocess.check_call("sudo /usr/local/nginx/sbin/nginx -c " + home + "/FrameworkBenchmarks/plack/nginx.conf", shell=True, stderr=errfile, stdout=logfile)
     return 0
     return 0
   except subprocess.CalledProcessError:
   except subprocess.CalledProcessError:
     return 1
     return 1
 def stop(logfile, errfile):
 def stop(logfile, errfile):
   try:
   try:
-    subprocess.Popen("kill -TERM $(ps --ppid `cat app.pid` -o pid --no-header)", shell=True, cwd="plack", stderr=errfile, stdout=logfile)
+    subprocess.call('kill -TERM $(cat '+home+"/FrameworkBenchmarks/plack/app.pid)", shell=True, cwd="plack", stderr=errfile, stdout=logfile)
+    subprocess.call("sudo /usr/local/nginx/sbin/nginx -s stop", shell=True)
     return 0
     return 0
   except subprocess.CalledProcessError:
   except subprocess.CalledProcessError:
     return 1
     return 1
+

+ 2 - 0
plack/source_code

@@ -1,2 +1,4 @@
 ./plack/app.psgi
 ./plack/app.psgi
 ./plack/cpanfile
 ./plack/cpanfile
+./plack/nginx.conf
+