Browse Source

Swoole no async update (#6015)

* Delete unused vars and use static function
Add 404

* Separate db and queries tests

* Use static prepared statements
Joan Miquel 4 years ago
parent
commit
cb92124cfc

+ 2 - 2
frameworks/PHP/swoole/benchmark_config.json

@@ -47,9 +47,9 @@
     },
     "no-async": {
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/query?q=",
       "fortune_url": "/fortunes",
-      "update_url": "/updates?queries=",
+      "update_url": "/updates?q=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Platform",

+ 51 - 30
frameworks/PHP/swoole/db-no-async.php

@@ -1,20 +1,51 @@
 <?php
+class Db
+{
+    public static PDOStatement $db;
+    public static PDOStatement $fortune;
+    public static PDOStatement $random;
+    public static PDOStatement $update;
+
+    public static function init()
+    {
+        $pdo = new PDO(
+            "mysql:host=tfb-database;dbname=hello_world",
+            "benchmarkdbuser",
+            "benchmarkdbpass",
+            [
+                PDO::ATTR_DEFAULT_FETCH_MODE  => PDO::FETCH_ASSOC,
+                PDO::ATTR_ERRMODE             => PDO::ERRMODE_EXCEPTION,
+                PDO::ATTR_EMULATE_PREPARES    => false
+            ]
+        );
+
+        self::$db        = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
+        self::$fortune   = $pdo->prepare('SELECT id,message FROM Fortune');
+        self::$random    = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
+        self::$update    = $pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
+    }
+}
 
 /**
  * The DB test
  *
+ * @return string
+ */
+function db(): string
+{
+    Db::$db->execute([mt_rand(1, 10000)]);
+    return json_encode(Db::$db->fetch(), JSON_NUMERIC_CHECK);
+}
+
+/**
+ * The Queries test
+ *
  * @param int $queries
  *
  * @return string
  */
-function db(int $queries = 1) : string {
-    global $pdo;
-    if ( $queries === -1) {
-        $statement = $pdo->prepare("SELECT id,randomNumber FROM World WHERE id=?");
-        $statement->execute([mt_rand(1, 10000)]);
-        return json_encode($statement->fetch(PDO::FETCH_ASSOC), JSON_NUMERIC_CHECK);
-    }
-    
+function query(int $queries = 1): string
+{
     // Read number of queries to run from URL parameter
     $query_count = 1;
     if ($queries > 1) {
@@ -23,18 +54,13 @@ function db(int $queries = 1) : string {
 
     // Create an array with the response string.
     $arr = [];
-    // Define query
-    $db = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
 
     // For each query, store the result set values in the response array
     while ($query_count--) {
-        $db->execute([mt_rand(1, 10000)]);
-        $arr[] = $db->fetch(PDO::FETCH_ASSOC);
+        DB::$db->execute([mt_rand(1, 10000)]);
+        $arr[] = Db::$db->fetch();
     }
 
-    // Use the PHP standard JSON encoder.
-    // http://www.php.net/manual/en/function.json-encode.php
-
     return json_encode($arr, JSON_NUMERIC_CHECK);
 }
 
@@ -43,12 +69,11 @@ function db(int $queries = 1) : string {
  *
  * @return string
  */
-function fortunes() : string {
-    global $pdo;
+function fortunes(): string
+{
     $fortune = [];
-    $db = $pdo->prepare('SELECT id, message FROM Fortune');
-    $db->execute();
-    $fortune = $db->fetchAll(PDO::FETCH_KEY_PAIR);
+    Db::$fortune->execute();
+    $fortune = Db::$fortune->fetchAll(PDO::FETCH_KEY_PAIR);
 
     $fortune[0] = 'Additional fortune added at request time.';
     asort($fortune);
@@ -59,9 +84,7 @@ function fortunes() : string {
         $html .= "<tr><td>$id</td><td>$message</td></tr>";
     }
 
-    return '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'
-            .$html.
-            '</table></body></html>';
+    return "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>$html</table></body></html>";
 }
 
 /**
@@ -71,27 +94,25 @@ function fortunes() : string {
  *
  * @return string
  */
-function updates(int $queries) : string {
-    global $pdo;
+function updates(int $queries): string
+{
     $query_count = 1;
     if ($queries > 1) {
         $query_count = $queries > 500 ? 500 : $queries;
     }
 
-    $statement = $pdo->prepare("SELECT randomNumber FROM World WHERE id=?");
     $update    = '';
 
     while ($query_count--) {
         $id = mt_rand(1, 10000);
-        $statement->execute([$id]);
+        Db::$random->execute([$id]);
 
-        $world = ["id" => $id, "randomNumber" => $statement->fetchColumn()];
+        $world = ["id" => $id, "randomNumber" => Db::$random->fetchColumn()];
         $world['randomNumber'] = mt_rand(1, 10000);
-        $update .="UPDATE World SET randomNumber = {$world['randomNumber']} WHERE id = $id;";
+        Db::$update->execute([$world['randomNumber'], $world['id']]);
 
         $arr[] = $world;
     }
-    $pdo->exec($update);
 
     return json_encode($arr, JSON_NUMERIC_CHECK);
 }

+ 14 - 13
frameworks/PHP/swoole/swoole-server-noasync.php

@@ -15,16 +15,13 @@ $server->set([
  * On start of the PHP worker. One worker per server process is started.
  */
 $server->on('workerStart', function () {
-    global $pdo;
-    $pdo = new PDO("mysql:host=tfb-database;dbname=hello_world", "benchmarkdbuser", "benchmarkdbpass", [
-        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
-    ]);
+    Db::init();
 });
 
 /**
  * On every request to the (web)server, execute the following code
  */
-$server->on('request', function (Request $req, Response $res) use ($db, $fortunes, $updates) {
+$server->on('request', static function (Request $req, Response $res) {
     try {
         switch ($req->server['request_uri']) {
             case '/json':
@@ -39,13 +36,13 @@ $server->on('request', function (Request $req, Response $res) use ($db, $fortune
 
             case '/db':
                 $res->header('Content-Type', 'application/json');
-
-                if (isset($req->get['queries'])) {
-                    $res->end(db((int) $req->get['queries']));
-                } else {
-                    $res->end(db(-1));
-                }
-                break; 
+                $res->end(db());
+                break;
+            
+            case '/query':
+                $res->header('Content-Type', 'application/json');
+                $res->end(query((int) $req->get['q'] ?? 1));
+                break;
 
             case '/fortunes':
                 $res->header('Content-Type', 'text/html; charset=utf-8');
@@ -54,8 +51,12 @@ $server->on('request', function (Request $req, Response $res) use ($db, $fortune
 
             case '/updates':
                 $res->header('Content-Type', 'application/json');
-                $res->end(updates((int) $req->get['queries'] ?? 1));
+                $res->end(updates((int) $req->get['q'] ?? 1));
                 break;
+
+            default:
+                $res->status(404);
+                $res->end('Not Found.');
         }
 
     } catch (\Throwable $e) {