Browse Source

Swoole using normal functions in no-async (#5159)

* Swoole using normal functions in no-async

* Delete extra ';'

* Move the app code to external file
Joan Miquel 5 years ago
parent
commit
e305ba336d

+ 98 - 0
frameworks/PHP/swoole/db-no-async.php

@@ -0,0 +1,98 @@
+<?php
+
+/**
+ * The DB 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);
+    }
+    
+    // Read number of queries to run from URL parameter
+    $query_count = 1;
+    if ($queries > 1) {
+        $query_count = $queries > 500 ? 500 : $queries;
+    }
+
+    // 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);
+    }
+
+    // Use the PHP standard JSON encoder.
+    // http://www.php.net/manual/en/function.json-encode.php
+
+    return json_encode($arr, JSON_NUMERIC_CHECK);
+}
+
+/**
+ * The Fortunes test
+ *
+ * @return string
+ */
+function fortunes() : string {
+    global $pdo;
+    $fortune = [];
+    $db = $pdo->prepare('SELECT id, message FROM Fortune');
+    $db->execute();
+    $fortune = $db->fetchAll(PDO::FETCH_KEY_PAIR);
+
+    $fortune[0] = 'Additional fortune added at request time.';
+    asort($fortune);
+
+    $html = '';
+    foreach ($fortune as $id => $message) {
+        $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
+        $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>';
+}
+
+/**
+ * The Updates test
+ *
+ * @param int $queries
+ *
+ * @return string
+ */
+function updates(int $queries) : string {
+    global $pdo;
+    $query_count = 1;
+    if ($queries > 1) {
+        $query_count = $queries > 500 ? 500 : $queries;
+    }
+
+    $statement       = $pdo->prepare("SELECT randomNumber FROM World WHERE id=?");
+    $updateStatement = $pdo->prepare("UPDATE World SET randomNumber=? WHERE id=?");
+
+    while ($query_count--) {
+        $id = mt_rand(1, 10000);
+        $statement->execute([$id]);
+
+        $world = ["id" => $id, "randomNumber" => $statement->fetchColumn()];
+        $updateStatement->execute(
+            [$world["randomNumber"] = mt_rand(1, 10000), $id]
+        );
+
+        $arr[] = $world;
+    }
+
+
+    return json_encode($arr, JSON_NUMERIC_CHECK);
+}

+ 2 - 1
frameworks/PHP/swoole/swoole-no-async.dockerfile

@@ -5,8 +5,9 @@ RUN pecl install swoole > /dev/null && \
 
 
 RUN docker-php-ext-install pdo_mysql > /dev/null
 RUN docker-php-ext-install pdo_mysql > /dev/null
 
 
+ADD ./ /swoole
 WORKDIR /swoole
 WORKDIR /swoole
-COPY swoole-server-noasync.php swoole-server-noasync.php
+
 COPY php.ini /usr/local/etc/php/
 COPY php.ini /usr/local/etc/php/
 
 
 CMD php swoole-server-noasync.php
 CMD php swoole-server-noasync.php

+ 11 - 105
frameworks/PHP/swoole/swoole-server-noasync.php

@@ -1,4 +1,5 @@
 <?php
 <?php
+require_once __DIR__.'/db-no-async.php';
 
 
 use Swoole\Http\Request;
 use Swoole\Http\Request;
 use Swoole\Http\Response;
 use Swoole\Http\Response;
@@ -8,110 +9,15 @@ $server->set([
     'worker_num' => swoole_cpu_num()
     'worker_num' => swoole_cpu_num()
 ]);
 ]);
 
 
-$pdo = new PDO("mysql:host=tfb-database;dbname=hello_world", "benchmarkdbuser", "benchmarkdbpass", [
-    PDO::ATTR_PERSISTENT => true
-]);
-
-/**
- * The DB test
- *
- * @param int $queries
- *
- * @return string
- */
-$db = function (int $queries = 1) use ($pdo): string {
-    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);
-    }
-    
-    // Read number of queries to run from URL parameter
-    $query_count = 1;
-    if ($queries > 1) {
-        $query_count = $queries > 500 ? 500 : $queries;
-    }
-
-    // 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);
-    }
-
-    // Use the PHP standard JSON encoder.
-    // http://www.php.net/manual/en/function.json-encode.php
-
-    return json_encode($arr, JSON_NUMERIC_CHECK);
-};
-
-/**
- * The Fortunes test
- *
- * @return string
- */
-$fortunes = function () use ($pdo): string {
-
-    $fortune = [];
-    $db = $pdo->prepare('SELECT id, message FROM Fortune');
-    $db->execute();
-    $fortune = $db->fetchAll(PDO::FETCH_KEY_PAIR);
-
-    $fortune[0] = 'Additional fortune added at request time.';
-    asort($fortune);
-
-    $html = '';
-    foreach ($fortune as $id => $message) {
-        $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
-        $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>';
-};
-
-/**
- * The Updates test
- *
- * @param int $queries
- *
- * @return string
- */
-$updates = function (int $queries) use ($pdo): string {
-    $query_count = 1;
-    if ($queries > 1) {
-        $query_count = $queries > 500 ? 500 : $queries;
-    }
-
-    $statement       = $pdo->prepare("SELECT randomNumber FROM World WHERE id=?");
-    $updateStatement = $pdo->prepare("UPDATE World SET randomNumber=? WHERE id=?");
-
-    while ($query_count--) {
-        $id = mt_rand(1, 10000);
-        $statement->execute([$id]);
-
-        $world = ["id" => $id, "randomNumber" => $statement->fetchColumn()];
-        $updateStatement->execute(
-            [$world["randomNumber"] = mt_rand(1, 10000), $id]
-        );
-
-        $arr[] = $world;
-    }
-
-
-    return json_encode($arr, JSON_NUMERIC_CHECK);
-};
-
 /**
 /**
  * On start of the PHP worker. One worker per server process is started.
  * On start of the PHP worker. One worker per server process is started.
  */
  */
-//$server->on('workerStart', function () use ($pool) {
-//});
+$server->on('workerStart', function () {
+    global $pdo;
+    $pdo = new PDO("mysql:host=tfb-database;dbname=hello_world", "benchmarkdbuser", "benchmarkdbpass", [
+        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
+    ]);
+});
 
 
 /**
 /**
  * On every request to the (web)server, execute the following code
  * On every request to the (web)server, execute the following code
@@ -133,20 +39,20 @@ $server->on('request', function (Request $req, Response $res) use ($db, $fortune
                 $res->header('Content-Type', 'application/json');
                 $res->header('Content-Type', 'application/json');
 
 
                 if (isset($req->get['queries'])) {
                 if (isset($req->get['queries'])) {
-                    $res->end($db((int) $req->get['queries']));
+                    $res->end(db((int) $req->get['queries']));
                 } else {
                 } else {
-                    $res->end($db(-1));
+                    $res->end(db(-1));
                 }
                 }
                 break; 
                 break; 
 
 
             case '/fortunes':
             case '/fortunes':
                 $res->header('Content-Type', 'text/html; charset=utf-8');
                 $res->header('Content-Type', 'text/html; charset=utf-8');
-                $res->end($fortunes());
+                $res->end(fortunes());
                 break;
                 break;
 
 
             case '/updates':
             case '/updates':
                 $res->header('Content-Type', 'application/json');
                 $res->header('Content-Type', 'application/json');
-                $res->end($updates((int) $req->get['queries'] ?? 1));
+                $res->end(updates((int) $req->get['queries'] ?? 1));
                 break;
                 break;
         }
         }