Browse Source

Merge pull request #363 from javiereguiluz/master

Added a raw Silex benchmark with all the required tests
michaelhixson 12 years ago
parent
commit
8fa7e024f2

+ 13 - 2
php-silex/benchmark_config

@@ -1,12 +1,23 @@
 {
   "framework": "silex",
   "tests": [{
-    "raw": {
+    "default": {
       "setup_file": "setup",
       "db_url": "/db",
       "query_url": "/db?queries=",
       "port": 8080,
       "sort": 64
+    },
+    "raw": {
+      "setup_file": "setup_raw",
+      "json_url": "/json",
+      "db_url": "/db",
+      "query_url": "/queries?queries=",
+      "fortune_url": "/fortune",
+      "update_url": "/update?queries=",
+      "plaintext_url": "/plaintext",
+      "port": 8080,
+      "sort": 144
     }
   }]
-}
+}

+ 125 - 0
php-silex/deploy/nginx_raw.conf

@@ -0,0 +1,125 @@
+#user  nobody;
+worker_processes  8;
+
+#error_log  logs/error.log;
+#error_log  logs/error.log  notice;
+#error_log  logs/error.log  info;
+
+#pid        logs/nginx.pid;
+
+
+events {
+    worker_connections  1024;
+}
+
+
+http {
+    include       /usr/local/nginx/conf/mime.types;
+    default_type  application/octet-stream;
+
+    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
+    #                  '$status $body_bytes_sent "$http_referer" '
+    #                  '"$http_user_agent" "$http_x_forwarded_for"';
+
+    #access_log  logs/access.log  main;
+
+    sendfile        on;
+    #tcp_nopush     on;
+
+    #keepalive_timeout  0;
+    keepalive_timeout  65;
+
+    #gzip  on;
+
+    server {
+        listen       8080;
+        server_name  localhost;
+
+        #charset koi8-r;
+
+        #access_log  logs/host.access.log  main;
+
+        #location / {
+        #    root   html;
+        #    index  index.html index.htm;
+        #}
+
+        #error_page  404              /404.html;
+
+        # redirect server error pages to the static page /50x.html
+        #
+        #error_page   500 502 503 504  /50x.html;
+        #location = /50x.html {
+        #    root   html;
+        #}
+
+        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
+        #
+        #location ~ \.php$ {
+        #    proxy_pass   http://127.0.0.1;
+        #}
+
+        root /home/ubuntu/FrameworkBenchmarks/php-silex/web/;
+        index  index_raw.php;
+
+        location / {
+            try_files $uri $uri/ /index_raw.php?$uri&$args;
+        }
+
+        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
+        #
+        location ~ \.php$ {
+            try_files $uri =404;
+            fastcgi_pass   127.0.0.1:9001;
+            fastcgi_index  index_raw.php;
+#            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
+            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
+            include        /usr/local/nginx/conf/fastcgi_params;
+        }
+
+        # deny access to .htaccess files, if Apache's document root
+        # concurs with nginx's one
+        #
+        #location ~ /\.ht {
+        #    deny  all;
+        #}
+    }
+
+
+    # another virtual host using mix of IP-, name-, and port-based configuration
+    #
+    #server {
+    #    listen       8000;
+    #    listen       somename:8080;
+    #    server_name  somename  alias  another.alias;
+
+    #    location / {
+    #        root   html;
+    #        index  index.html index.htm;
+    #    }
+    #}
+
+
+    # HTTPS server
+    #
+    #server {
+    #    listen       443;
+    #    server_name  localhost;
+
+    #    ssl                  on;
+    #    ssl_certificate      cert.pem;
+    #    ssl_certificate_key  cert.key;
+
+    #    ssl_session_timeout  5m;
+
+    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
+    #    ssl_ciphers  HIGH:!aNULL:!MD5;
+    #    ssl_prefer_server_ciphers   on;
+
+    #    location / {
+    #        root   html;
+    #        index  index.html index.htm;
+    #    }
+    #}
+
+}

+ 28 - 0
php-silex/setup_raw.py

@@ -0,0 +1,28 @@
+import subprocess
+import sys
+import setup_util
+from os.path import expanduser
+
+home = expanduser("~")
+
+def start(args):
+  setup_util.replace_text("php-silex/web/index_raw.php", "192.168.100.102", "" + args.database_host + "")
+  setup_util.replace_text("php-silex/deploy/php-silex", "\".*\/FrameworkBenchmarks", "\"" + home + "/FrameworkBenchmarks")
+  setup_util.replace_text("php-silex/deploy/php-silex", "Directory .*\/FrameworkBenchmarks", "Directory " + home + "/FrameworkBenchmarks")
+  setup_util.replace_text("php-silex/deploy/nginx_raw.conf", "root .*\/FrameworkBenchmarks", "root " + home + "/FrameworkBenchmarks")
+
+  try:
+    subprocess.check_call("composer.phar install --optimize-autoloader", shell=True, cwd="php-silex")
+    subprocess.check_call("sudo php-fpm --fpm-config config/php-fpm.conf -g " + home + "/FrameworkBenchmarks/php-silex/deploy/php-fpm.pid", shell=True)
+    subprocess.check_call("sudo /usr/local/nginx/sbin/nginx -c " + home + "/FrameworkBenchmarks/php-silex/deploy/nginx_raw.conf", shell=True)
+    return 0
+  except subprocess.CalledProcessError:
+    return 1
+
+def stop():
+  try:
+    subprocess.call("sudo /usr/local/nginx/sbin/nginx -s stop", shell=True)
+    subprocess.call("sudo kill -QUIT $( cat php-silex/deploy/php-fpm.pid )", shell=True)
+    return 0
+  except subprocess.CalledProcessError:
+    return 1

+ 0 - 1
php-silex/web/index.php

@@ -40,4 +40,3 @@ $app->run();
 
 
 
-

+ 81 - 0
php-silex/web/index_raw.php

@@ -0,0 +1,81 @@
+<?php
+
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+
+require_once __DIR__.'/../vendor/autoload.php';
+
+$app = new Silex\Application();
+
+// Test 1: JSON serialization
+$app->get('/json', function() {
+    // The following code would be handier:
+    //   return new JsonResponse(array("message" => "Hello World!"));
+    //
+    // But JsonResponse does some checks that are unneeded for this benchmark
+    // and therefore, a plain Response object is faster.
+    return new Response(json_encode(array('message' => 'Hello World!')), 200, array('Content-Type' => 'application/json'));
+});
+
+// Test 2: Single database query
+$app->get('/db', function() {
+    $db = new mysqli('192.168.100.102', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
+    $row = mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000));
+
+    return new Response(json_encode(mysqli_fetch_assoc($row)), 200, array('Content-Type' => 'application/json'));
+});
+
+// Test 3: Multiple database queries
+$app->get('/queries', function(Request $request) {
+    $queries = max(1, min($request->query->get('queries'), 500));
+    $db = new mysqli('192.168.100.102', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
+
+    for ($i=0; $i<$queries; $i++) {
+        $rows[] = mysqli_fetch_assoc(mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000)));
+    }
+
+    return new Response(json_encode($rows), 200, array('Content-Type' => 'application/json'));
+});
+
+// Test 4: Fortunes
+$app->get('/fortunes', function() {
+    $db = new mysqli('192.168.100.102', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
+    $result = mysqli_query($db, 'SELECT * FROM Fortune');
+    while ($row = mysqli_fetch_row($result)) {
+        $fortunes[$row[0]] = htmlspecialchars($row[1], ENT_IGNORE);
+    }
+    $fortunes[] = 'Additional fortune added at request time.';
+
+    asort($fortunes);
+
+    foreach ($fortunes as $i => $fortune) {
+        $templates[$i] = '<tr><td>'.$i.'</td><td>'.$fortune.'</td></tr>';
+    }
+
+    return new Response('<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'.implode('', $templates).'</table></body></html>');
+});
+
+// Test 5: Database updates
+$app->get('/updates', function(Request $request) {
+    $queries = max(1, min($request->query->get('queries'), 500));
+    $db = new mysqli('192.168.100.102', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
+    for ($i=0; $i<$queries; $i++) {
+        $rows[] = mysqli_fetch_assoc(mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000)));
+    }
+
+    mysqli_autocommit($db, FALSE);
+    foreach ($rows as $i => $row) {
+        $rows[$i]['randomNumber'] = rand(1, 10000);
+        mysqli_query($db, "UPDATE World SET randomNumber = {$rows[$i]['randomNumber']} WHERE id = {$row['id']}");
+    }
+    mysqli_commit($db);
+
+    return new Response(json_encode($rows), 200, array('Content-Type' => 'application/json'));
+});
+
+// Test 6: Plaintext
+$app->get('/plaintext', function() {
+    return new Response('Hello World!', 200, array('Content-Type' => 'text/plain'));
+});
+
+$app->run();