Browse Source

Performance changes in workerman (#5075)

* Use return in switch

* Use static anonymous function
Use less memory and will be faster

* Update versus in config
Joan Miquel 5 years ago
parent
commit
39785dc4e0

+ 1 - 1
frameworks/PHP/workerman/benchmark_config.json

@@ -22,7 +22,7 @@
       "database_os": "Linux",
       "display_name": "workerman",
       "notes": "",
-      "versus": "php7"
+      "versus": "php"
     }
   }]
 }

+ 6 - 11
frameworks/PHP/workerman/server.php

@@ -13,7 +13,7 @@ $http_worker->onWorkerStart = function () {
     $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8',
         'benchmarkdbuser', 'benchmarkdbpass');
 };
-$http_worker->onMessage = function ($connection) {
+$http_worker->onMessage = static function ($connection) {
     global $pdo;
 
     Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
@@ -21,28 +21,23 @@ $http_worker->onMessage = function ($connection) {
     switch (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) {
         case '/plaintext':
             Http::header('Content-Type: text/plain');
-            $connection->send('Hello, World!');
-            break;
+            return $connection->send('Hello, World!');
 
         case '/json':
             Http::header('Content-Type: application/json');
-            $connection->send(json_encode(['message' => 'Hello, World!']));
-            break;
+            return $connection->send(json_encode(['message' => 'Hello, World!']));
 
         case '/db':
             Http::header('Content-Type: application/json');
-            $connection->send(dbraw($pdo));
-            break;
+            return $connection->send(dbraw($pdo));
 
         case '/fortune':
             Http::header('Content-Type: text/html; charset=utf-8');
-            $connection->send(fortune($pdo));
-            break;
+            return $connection->send(fortune($pdo));
 
         case '/update':
             Http::header('Content-Type: application/json');
-            $connection->send(updateraw($pdo));
-            break;
+            return $connection->send(updateraw($pdo));
 
             //case '/info':
             //   Http::header('Content-Type: text/plain');