Browse Source

Add fixes and setup to Perl frameworks

Stefan Geneshky 12 năm trước cách đây
mục cha
commit
36a8612cbf
11 tập tin đã thay đổi với 145 bổ sung42 xóa
  1. 13 7
      dancer/app.pl
  2. 13 0
      dancer/benchmark_config
  3. 14 0
      dancer/setup.py
  4. 21 14
      installer.py
  5. 12 11
      kelp/app.pl
  6. 13 0
      kelp/benchmark_config
  7. 3 0
      kelp/conf/config.pl
  8. 14 0
      kelp/setup.py
  9. 10 10
      mojolicious/app.pl
  10. 13 0
      mojolicious/benchmark_config
  11. 19 0
      mojolicious/setup.py

+ 13 - 7
dancer/app.pl

@@ -3,11 +3,15 @@ use strict;
 use warnings;
 
 use Dancer ':syntax';
-use Dancer::Plugin::Database;
+use DBI;
+
 set serializer => 'JSON';
 
-#my $dbh = database({ driver => 'mysql', database => 'test' });
-my $dbh = database({ driver => 'mysql', host => 'ip-10-34-150-134.eu-west-1.compute.internal', database => 'test', username => 'root' });
+my $database = 'hello_world';
+my $host     = 'localhost';
+my $dsn      = "dbi:mysql:database=$database;host=$host;port=3306";
+my $dbh      = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
+my $sth      = $dbh->prepare("SELECT * FROM World where id = ?");
 
 get '/json' => sub {
     { message => 'Hello, World!' }
@@ -16,13 +20,15 @@ get '/json' => sub {
 get '/db' => sub {
     my $queries = params->{queries} || 1;
     my @response;
-    for( 1 .. $queries ) {
+    for ( 1 .. $queries ) {
         my $id = int rand 10000 + 1;
-        if ( my $row = $dbh->quick_select( 'world', { id => $id } ) ) {
-            push @response, { id => $id, randomNumber => $row->{randomnumber} };
+        $sth->execute($id);
+        if ( my $row = $sth->fetchrow_hashref ) {
+            push @response,
+              { id => $id, randomNumber => $row->{randomnumber} };
         }
     }
-    { \@response }
+    return \@response;
 };
 
 Dancer->dance;

+ 13 - 0
dancer/benchmark_config

@@ -0,0 +1,13 @@
+{
+  "framework": "dancer",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "port": 8080,
+      "sort": 27
+    }
+  }]
+}

+ 14 - 0
dancer/setup.py

@@ -0,0 +1,14 @@
+import subprocess
+
+def start(args):
+  setup_util.replace_text("dancer/app.pl", "localhost", args.database_host)
+  subprocess.Popen("plackup -E deployment -s Starman -p 8080 dancer/app.pl".rsplit(" "))
+  return 0
+def stop():
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if 'plackup' in line:
+      pid = int(line.split(None, 2)[1])
+      os.kill(pid, 9)
+  return 0

+ 21 - 14
installer.py

@@ -2,7 +2,7 @@ import subprocess
 import os
 
 class Installer:
-  
+
   ############################################################
   # install_software
   ############################################################
@@ -21,7 +21,7 @@ class Installer:
     # Prerequisites
     #######################################
     self.__run_command("sudo apt-get update", True)
-    self.__run_command("sudo apt-get upgrade", True)    
+    self.__run_command("sudo apt-get upgrade", True)
     self.__run_command("sudo apt-get install build-essential libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev python-software-properties unzip git-core libcurl4-openssl-dev libbz2-dev libmysqlclient-dev mongodb-clients libreadline6-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev libgdbm-dev ncurses-dev automake libffi-dev htop libtool bison libevent-dev libgstreamer-plugins-base0.10-0 libgstreamer0.10-0 liborc-0.4-0 libwxbase2.8-0 libwxgtk2.8-0 libgnutls-dev libjson0-dev", True)
 
     self.__run_command("cp ../config/benchmark_profile ../../.bash_profile")
@@ -38,7 +38,7 @@ class Installer:
     self.__run_command("wget -O - http://binaries.erlang-solutions.com/debian/erlang_solutions.asc | sudo apt-key add -")
     self.__run_command("sudo apt-get update")
     self.__run_command("sudo apt-get install esl-erlang", True)
-    
+
     #
     # Python
     #
@@ -62,10 +62,10 @@ class Installer:
     #
     # Java
     #
-    
+
     self.__run_command("sudo apt-get install openjdk-7-jdk", True)
     self.__run_command("sudo apt-get remove --purge openjdk-6-jre openjdk-6-jre-headless", True)
-    
+
     #
     # Ruby/JRuby
     #
@@ -76,7 +76,7 @@ class Installer:
     subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm 2.0.0-p0 do gem install bundler"])
     subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm install jruby-1.7.3"])
     subprocess.call(["bash", "-c", "source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do gem install bundler"])
-    
+
     # We need a newer version of jruby-rack
     self.__run_command("git clone git://github.com/jruby/jruby-rack.git")
     subprocess.call(["bash", "-c", "cd installs/jruby-rack && source ~/.rvm/scripts/'rvm' && rvm jruby-1.7.3 do bundle install"])
@@ -89,6 +89,13 @@ class Installer:
 
     self.__run_command("curl http://go.googlecode.com/files/go1.1beta1.linux-amd64.tar.gz | tar xvz")
 
+    #
+    # Perl
+    #
+
+    self.__run_command("curl -L http://cpanmin.us | perl - --sudo App::cpanminus")
+    self.__run_command("cpanm -S DBI DBD::mysql Kelp Dancer Mojolicious Kelp::Module::JSON::XS Dancer::Plugin::Database")
+
     #
     # php
     #
@@ -126,7 +133,7 @@ class Installer:
     self.__run_command("sudo mv /etc/apache2/ports.conf /etc/apache2/ports.conf.orig")
     self.__run_command("sudo sh -c \"cat ../config/ports.conf > /etc/apache2/ports.conf\"")
     self.__run_command("sudo /etc/init.d/apache2 stop")
-    
+
     #
     # Nginx
     #
@@ -134,7 +141,7 @@ class Installer:
     self.__run_command("./configure", cwd="nginx-1.2.7")
     self.__run_command("make", cwd="nginx-1.2.7")
     self.__run_command("sudo make install", cwd="nginx-1.2.7")
-    
+
     #
     # Openresty (nginx with openresty stuff)
     #
@@ -142,7 +149,7 @@ class Installer:
     self.__run_command("./configure --with-luajit", cwd="ngx_openresty-1.2.7.5")
     self.__run_command("make", cwd="ngx_openresty-1.2.7.5")
     self.__run_command("sudo make install", cwd="ngx_openresty-1.2.7.5")
-    
+
     #
     # Gunicorn
     #
@@ -220,7 +227,7 @@ class Installer:
     self.__run_command("wget http://dist.springframework.org.s3.amazonaws.com/release/GRAILS/grails-2.1.1.zip")
     self.__run_command("unzip -o grails-2.1.1.zip")
     self.__run_command("rm grails-2.1.1.zip")
-    
+
 
     ##############################
     # Flask
@@ -308,7 +315,7 @@ class Installer:
     ./waf build
     sudo ./waf install
     cd ~
-    
+
     ##############################
     # wrk
     ##############################
@@ -324,9 +331,9 @@ class Installer:
     ##############################
     sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
     sudo cp 10gen.list /etc/apt/sources.list.d/10gen.list
-    sudo apt-get update 
+    sudo apt-get update
     yes | sudo apt-get install mongodb-10gen
-    
+
     sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
     sudo mv mongodb.conf /etc/mongodb.conf
     sudo restart mongodb
@@ -334,7 +341,7 @@ class Installer:
 
     p = subprocess.Popen(self.benchmarker.ssh_string.split(" "), stdin=subprocess.PIPE)
     p.communicate(remote_script)
-    
+
   ############################################################
   # End __parse_results
   ############################################################

+ 12 - 11
kelp/app.pl

@@ -2,12 +2,11 @@
 use Kelp::Less;
 use DBI;
 
-attr dbh => sub {
-    my $database = 'test';
-    my $host     = 'ip-10-34-150-134.eu-west-1.compute.internal';
-    my $dsn      = 'dbi:mysql:database=$database;host=$host;port=3306';
-    DBI->connect( $dsn, 'root', '', {} );
-};
+my $database = 'hello_world';
+my $host     = 'localhost';
+my $dsn      = "dbi:mysql:database=$database;host=$host;port=3306";
+my $dbh      = DBI->connect( $dsn, 'benchmarkdbuser', 'benchmarkdbpass', {} );
+my $sth      = $dbh->prepare("SELECT * FROM World where id = ?");
 
 get '/json' => sub {
     { message => 'Hello, World!' }
@@ -15,15 +14,17 @@ get '/json' => sub {
 
 get '/db' => sub {
     my $self = shift;
-    my $queries = param->{queries} || 1;
+    my $queries = $self->param('queries') || 1;
     my @response;
-    for( 1 .. $queries ) {
+    for ( 1 .. $queries ) {
         my $id = int rand 10000 + 1;
-        if ( my $row = $self->dbh->quick_select( 'world', { id => $id } ) ) {
-            push @response, { id => $id, randomNumber => $row->{randomnumber} };
+        $sth->execute($id);
+        if ( my $row = $sth->fetchrow_hashref ) {
+            push @response,
+              { id => $id, randomNumber => $row->{randomnumber} };
         }
     }
-    { \@response }
+    return \@response;
 };
 
 run;

+ 13 - 0
kelp/benchmark_config

@@ -0,0 +1,13 @@
+{
+  "framework": "kelp",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "port": 8080,
+      "sort": 27
+    }
+  }]
+}

+ 3 - 0
kelp/conf/config.pl

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

+ 14 - 0
kelp/setup.py

@@ -0,0 +1,14 @@
+import subprocess
+
+def start(args):
+  setup_util.replace_text("kelp/app.pl", "localhost", args.database_host)
+  subprocess.Popen("plackup -E deployment -s Starman -p 8080 kelp/app.pl".rsplit(" "))
+  return 0
+def stop():
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if 'plackup' in line:
+      pid = int(line.split(None, 2)[1])
+      os.kill(pid, 9)
+  return 0

+ 10 - 10
mojolicious/app.pl

@@ -1,12 +1,12 @@
 #!/usr/bin/env perl
 use Mojolicious::Lite;
-use Mojolicious::Plugin::Database;
+use DBI;
 
-plugin 'database', {
-    dsn      => 'dbi:mysql:dbname=test',
-    username => 'root',
-    password => ''
-};
+my $database = 'hello_world';
+my $host     = 'localhost';
+my $dsn      = "dbi:mysql:database=$database;host=$host;port=3306";
+my $dbh      = DBI->connect( $dsn, 'root', '', {} );
+my $sth      = $dbh->prepare("SELECT * FROM World where id = ?");
 
 get '/json' => sub {
     my $self = shift;
@@ -17,12 +17,12 @@ get '/db' => sub {
     my $self = shift;
     my $queries = $self->param('queries') || 1;
     my @response;
-    my $sth = $self->db->prepare( 'SELECT randomnumber FROM world WHERE id = ?' );
     for ( 1 .. $queries ) {
         my $id = int rand 10000 + 1;
-        my $res = $sth->execute( $id );
-        if ( my $row = $sth->fetchrow_arrayref ) {
-            push @response, { id => $id, randomNumber => $row->[0] };
+        $sth->execute($id);
+        if ( my $row = $sth->fetchrow_hashref ) {
+            push @response,
+              { id => $id, randomNumber => $row->{randomnumber} };
         }
     }
     $self->render( json => \@response );

+ 13 - 0
mojolicious/benchmark_config

@@ -0,0 +1,13 @@
+{
+  "framework": "mojolicious",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "port": 8080,
+      "sort": 27
+    }
+  }]
+}

+ 19 - 0
mojolicious/setup.py

@@ -0,0 +1,19 @@
+import subprocess
+import sys
+import os
+import setup_util
+
+def start(args):
+  setup_util.replace_text("mojolicious/app.pl", "localhost", args.database_host)
+  os.environ["MOJO_MODE"] = "production"
+  subprocess.Popen("hypnotoad -f mojolicious/app.pl".rsplit(" "))
+  return 0
+
+def stop():
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if 'hypnotoad' in line:
+      pid = int(line.split(None, 2)[1])
+      os.kill(pid, 9)
+  return 0