Browse Source

Swoole include all tests + using SWOOLE_BASE mode for performance (#3657)

* Using SWOOLE_BASE mode for performance

* Included all the tests for Swoole

* Add the Swoole Api settings

* Add PHP MySQL driver to the Swoole docker file

* Removed failing and unused ActiveRecord query

* Removed unused activerecord query

* Request get is needed instead of the global $_GET
Wulfklaue 7 years ago
parent
commit
4152eaa8ee

+ 6 - 1
frameworks/PHP/php/benchmark_config.json

@@ -87,10 +87,15 @@
     },
     },
     "swoole": {
     "swoole": {
       "json_url": "/json",
       "json_url": "/json",
+      "plaintext_url": "/plaintext",
+      "db_url": "/db",
+      "query_url": "/db?queries=",
+      "fortune_url": "/fortunes",
+      "update_url": "/updates?queries=",    
       "port": 8080,
       "port": 8080,
       "approach": "Realistic",
       "approach": "Realistic",
       "classification": "Platform",
       "classification": "Platform",
-      "database": "None",
+      "database": "MySQL",
       "framework": "None",
       "framework": "None",
       "language": "PHP",
       "language": "PHP",
       "flavor": "PHP7",
       "flavor": "PHP7",

+ 3 - 1
frameworks/PHP/php/php-swoole.dockerfile

@@ -7,8 +7,10 @@ RUN cd /tmp && curl -sSL "https://github.com/swoole/swoole-src/archive/v${SWOOLE
         && phpize && ./configure > /dev/null && make > /dev/null && make install > /dev/null \
         && phpize && ./configure > /dev/null && make > /dev/null && make install > /dev/null \
         && docker-php-ext-enable swoole
         && docker-php-ext-enable swoole
 
 
+RUN docker-php-ext-install pdo_mysql
+
 ADD ./ /swoole
 ADD ./ /swoole
 WORKDIR /swoole
 WORKDIR /swoole
 
 
 CMD sed -i 's|NUMCORES|'"$(nproc)"'|g' swoole-server.php && \
 CMD sed -i 's|NUMCORES|'"$(nproc)"'|g' swoole-server.php && \
-    php swoole-server.php
+    php swoole-server.php

+ 103 - 5
frameworks/PHP/php/swoole-server.php

@@ -1,10 +1,108 @@
 <?php
 <?php
-$server = new swoole_http_server('0.0.0.0', 8080);
+$server = new swoole_http_server('0.0.0.0', 8080, SWOOLE_BASE);
 $server->set(array(
 $server->set(array(
-    'worker_num' => NUMCORES,
+    'worker_num' => NUMCORES
 ));
 ));
 $server->on('request', function ($req, $res) {
 $server->on('request', function ($req, $res) {
-    $res->header('Content-type', 'application/json');
-    $res->end(json_encode(array('message' => 'Hello, World!')));
+
+    switch ($req->server['request_uri'])
+    {
+        case "/json":
+            $res->header('Content-type', 'application/json');
+            $res->end(json_encode(array('message' => 'Hello, World!')));
+            break;
+
+        case "/plaintext":
+            $res->header('Content-Type', 'text/plain');
+            $res->end('Hello, World!');
+            break;
+
+        case "/db":
+            $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
+                PDO::ATTR_PERSISTENT => true
+            ));
+            // Read number of queries to run from URL parameter
+            $query_count = 1;
+            if (isset($req->get['queries']) && $req->get['queries'] > 0)
+                $query_count = $req->get['queries'] > 500 ? 500 : $req->get['queries'];
+
+            // Create an array with the response string.
+            $arr = array();
+            // Define query
+            $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
+            // For each query, store the result set values in the response array
+            while (0 < $query_count--)
+            {
+                $id = mt_rand(1, 10000);
+                $statement->execute(array($id));
+
+                // Store result in array.
+                $arr[] = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
+            }
+            // Use the PHP standard JSON encoder.
+            // http://www.php.net/manual/en/function.json-encode.php
+            if (count($arr) === 1)
+                $arr = $arr[0];
+
+            $res->header('Content-Type', 'application/json');
+            $res->end(json_encode($arr));
+            break;
+
+        case "/fortunes":
+            $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8', 'benchmarkdbuser', 'benchmarkdbpass', array(
+                PDO::ATTR_PERSISTENT => true
+            ));
+            // Define query
+            $statement = $pdo->query('SELECT id, message FROM Fortune');
+
+            // Store result in array.
+            $arr = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
+            $arr[0] = 'Additional fortune added at request time.';
+            asort($arr);
+
+            $html = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
+            foreach ($arr as $id => $fortune)
+                $html .= "<tr><td>" . $id . "</td><td>" . htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8') . "</td></tr>";
+
+            $html .= "</table></body></html>";
+
+            $res->header('Content-Type', 'text/html; charset=utf-8');
+            $res->end($html);
+            break;
+
+        case "/updates":
+            $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
+                PDO::ATTR_PERSISTENT => true
+            ));
+
+            $query_count = 1;
+            if (isset($req->get['queries']) && $req->get['queries'] > 0)
+            {
+                $query_count = $req->get['queries'] > 500 ? 500 : $req->get['queries'];
+            }
+
+            $arr = array();
+            $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
+            $updateStatement = $pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
+
+            while (0 < $query_count--)
+            {
+                $id = mt_rand(1, 10000);
+                $randomNumber = mt_rand(1, 10000);
+                $statement->execute(array($id));
+
+                // Store result in array.
+                $world = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
+                $world['randomNumber'] = $randomNumber;
+                $updateStatement->execute(array($randomNumber, $id));
+
+                $arr[] = $world;
+            }
+
+            $res->header('Content-Type', 'application/json');
+            $res->end(json_encode($arr));
+            break;
+    }
+
 });
 });
-$server->start();
+$server->start();