Browse Source

Clean Workerman (#5060)

* Clean workerman server.php

* Reuse the db statement
Joan Miquel 6 years ago
parent
commit
90d610af84

+ 22 - 18
frameworks/PHP/workerman/dbraw.php

@@ -1,24 +1,28 @@
 <?php
-function dbraw($pdo) {
-  if (! isset($_GET['queries'])) {
-    $statement = $pdo->query( 'SELECT id,randomNumber FROM World WHERE id = '. mt_rand(1, 10000) );
-    echo json_encode($statement->fetch(PDO::FETCH_ASSOC));
+function dbraw($pdo)
+{
+    static $statement;
 
-    return;
-  }
+    $statement = $statement ?? $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
 
-  $query_count = 1;
-  if ($_GET['queries'] > 1) {
-    $query_count = min($_GET['queries'], 500);
-  }
- 
-  $arr = [];
-  $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
+    if ( ! isset($_GET['queries'])) {
+        $statement->execute([mt_rand(1, 10000)]);
+        echo json_encode($statement->fetch(PDO::FETCH_ASSOC));
 
-  while ($query_count--) {
-    $statement->execute([mt_rand(1, 10000)]);
-    $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
-  }
+        return;
+    }
 
-  echo json_encode($arr);
+    $query_count = 1;
+    if ($_GET['queries'] > 1) {
+        $query_count = min($_GET['queries'], 500);
+    }
+
+    $arr = [];
+
+    while ($query_count--) {
+        $statement->execute([mt_rand(1, 10000)]);
+        $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
+    }
+
+    echo json_encode($arr);
 }

+ 11 - 8
frameworks/PHP/workerman/fortune.php

@@ -1,13 +1,16 @@
 <?php
-function fortune($pdo) {
-    $statement = $pdo->query( 'SELECT id,message FROM Fortune' );
-    $arr = $statement->fetchAll(PDO::FETCH_KEY_PAIR); 
-    $arr[0] = 'Additional fortune added at request time.';
+function fortune($pdo)
+{
+    static $statement;
+    $statement = $statement ?? $pdo->prepare('SELECT id,message FROM Fortune');
+    $statement->execute();
+    $arr       = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
+    $arr[0]    = 'Additional fortune added at request time.';
     asort($arr);
 ?>
 <!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>
-<?php foreach ( $arr as $id => $fortune ) : ?>
-<tr><td><?= $id ?></td><td><?= htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8'); ?></td></tr>
-<?php endforeach ?></table></body></html>
+<?php foreach ($arr as $id => $fortune): ?>
+<tr><td><?=$id?></td><td><?=htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8');?></td></tr>
+<?php endforeach?></table></body></html>
 <?php
-}
+}

+ 57 - 50
frameworks/PHP/workerman/server.php

@@ -1,57 +1,64 @@
 <?php
-require_once __DIR__ . '/vendor/autoload.php';
-require_once __DIR__ . '/fortune.php';
-require_once __DIR__ . '/dbraw.php';
-require_once __DIR__ . '/updateraw.php';
-use Workerman\Worker;
+require_once __DIR__.'/vendor/autoload.php';
+require_once __DIR__.'/fortune.php';
+require_once __DIR__.'/dbraw.php';
+require_once __DIR__.'/updateraw.php';
 use Workerman\Protocols\Http;
+use Workerman\Worker;
 
-$http_worker = new Worker('http://0.0.0.0:8080');
-$http_worker->count = (int) shell_exec('nproc') ?? 64;
-$http_worker->onWorkerStart = function()
-{
-  global $pdo;
-  $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8',
-  'benchmarkdbuser', 'benchmarkdbpass');
+$http_worker                = new Worker('http://0.0.0.0:8080');
+$http_worker->count         = (int) shell_exec('nproc') ?? 64;
+$http_worker->onWorkerStart = function () {
+    global $pdo;
+    $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8',
+        'benchmarkdbuser', 'benchmarkdbpass');
 };
-$http_worker->onMessage = function($connection)
-{
-  global $pdo;
-  $base = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
-
-  Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
-
-  if ($base === '/fortune') {
-    Http::header('Content-Type: text/html; charset=utf-8');
-    ob_start();
-    fortune($pdo);
-    $connection->send(ob_get_clean());
-
-  } elseif ($base === '/db') {
-    Http::header('Content-Type: application/json');
-    ob_start();
-    dbraw($pdo);
-    $connection->send(ob_get_clean());
-
-  } elseif ($base === '/update') {
-    Http::header('Content-Type: application/json');
-    ob_start();
-    updateraw($pdo);
-    $connection->send(ob_get_clean());
-
-  } elseif ($base === '/plaintext') {
-    Http::header('Content-Type: text/plain');
-    $connection->send('Hello, World!');
-
-  } elseif ($base === '/json') {
-    Http::header('Content-Type: application/json');
-    $connection->send(json_encode(['message'=>'Hello, World!']));
-  // } elseif ($base === '/info') {
-  //   Http::header('Content-Type: text/plain');
-  //   ob_start();
-  //   phpinfo();
-  //   $connection->send(ob_get_clean());
-  }
+$http_worker->onMessage = function ($connection) {
+    global $pdo;
+
+    Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
+
+    switch (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) {
+        case '/plaintext':
+            Http::header('Content-Type: text/plain');
+            $connection->send('Hello, World!');
+            break;
+
+        case '/json':
+            Http::header('Content-Type: application/json');
+            $connection->send(json_encode(['message' => 'Hello, World!']));
+            break;
+
+        case '/db':
+            Http::header('Content-Type: application/json');
+            ob_start();
+            dbraw($pdo);
+            $connection->send(ob_get_clean());
+            break;
+
+        case '/fortune':
+            Http::header('Content-Type: text/html; charset=utf-8');
+            ob_start();
+            fortune($pdo);
+            $connection->send(ob_get_clean());
+            break;
+
+        case '/update':
+            Http::header('Content-Type: application/json');
+            ob_start();
+            updateraw($pdo);
+            $connection->send(ob_get_clean());
+            break;
+
+            //case '/info':
+            //   Http::header('Content-Type: text/plain');
+            //   ob_start();
+            //   phpinfo();
+            //   $connection->send(ob_get_clean());
+
+            //default:
+            //   $connection->send('error');
+    }
 };
 
 Worker::runAll();