Browse Source

add PHP:workerman (#3084)

* add PHP:workerman

* Update dbraw.php

* Update updateraw.php

* Update dbraw.php

* Update dbraw.php

* Update README.md

* Update README.md

* Update README.md

* Update benchmark_config.json

* Update setup.sh
Kenny Hartono 7 years ago
parent
commit
74316dfdb8

+ 1 - 0
.travis.yml

@@ -153,6 +153,7 @@ env:
     - "TESTDIR=PHP/silex-orm"
     - "TESTDIR=PHP/slim"
     - "TESTDIR=PHP/symfony2"
+    - "TESTDIR=PHP/workerman"
     - "TESTDIR=PHP/yaf"
     - "TESTDIR=PHP/yii2"
     - "TESTDIR=PHP/zend"

+ 46 - 0
frameworks/PHP/workerman/README.md

@@ -0,0 +1,46 @@
+#PHP Benchmarking Test
+
+This is the PHP portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
+NGINX is removed in this test, and substituted by [Workerman, An asynchronous event driven PHP framework](https://github.com/walkor/Workerman). An asynchronous event driven PHP framework for easily building fast, scalable network applications. Supports HTTP, Websocket, SSL and other custom protocols. Supports libevent, HHVM, ReactPHP.
+
+https://github.com/walkor/Workerman
+
+This test doesn't use the standard PHP (fw_require) because it needs PCNTL / Process Control extension. Adding PCNTL in the PHP compilation (--enable-pcntl) will fail other PHP framework test.
+
+```
+Database config
+HOST: DBHOST (from ENV) , or 127.0.0.1 if DBHOST is not available
+User : benchmarkdbuser
+Password : benchmarkdbpass
+DBNAME : hello_world
+```
+MySQL Connection is using PHP PDO::Persistent Connection.
+
+The number of threads count in PHP is (number of cores)*2.
+
+## Infrastructure Software Versions
+The tests were run with:
+* [PHP 7](http://www.php.net/)
+* [MySQL 5.5.54](https://dev.mysql.com/)
+
+Pre-test:
+* [Composer](https://getcomposer.org/)
+
+### JSON Encoding Test
+Using the PHP standard [JSON encoder](http://www.php.net/manual/en/function.json-encode.php)
+
+## Test URLs
+
+### JSON Encoding Test
+http://localhost:8080/json.php
+
+### Data-Store/Database Mapping Test
+http://localhost:8080/dbraw.php
+
+http://localhost:8080/updateraw.php
+
+### Variable Query Test
+http://localhost:8080/dbraw.php?queries=5
+
+### Fortune Test
+http://localhost:8080/fortune.php

+ 29 - 0
frameworks/PHP/workerman/benchmark_config.json

@@ -0,0 +1,29 @@
+{
+  "framework": "workerman",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json.php",
+      "plaintext_url": "/plaintext.php",
+      "db_url": "/dbraw.php",
+      "query_url": "/dbraw.php?queries=",
+      "update_url": "/updateraw.php?queries=",
+      "fortune_url": "/fortune.php",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "MySQL",
+      "framework": "None",
+      "language": "PHP",
+      "flavor": "PHP7",
+      "orm": "Raw",
+      "platform": "None",
+      "webserver": "workerman",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "workerman",
+      "notes": "",
+      "versus": "php7"
+    }
+  }]
+}

+ 5 - 0
frameworks/PHP/workerman/composer.json

@@ -0,0 +1,5 @@
+{
+	"require": {
+		"workerman/workerman": "^3.5"
+	}
+}

+ 23 - 0
frameworks/PHP/workerman/dbraw.php

@@ -0,0 +1,23 @@
+<?php
+function dbraw($pdo) {
+  $query_count = 1;
+  if (isset($_GET['queries']) && $_GET['queries'] > 0) {
+    $query_count = $_GET['queries'];
+  }
+  if ($query_count > 500) $query_count=500;
+  $is_one = $query_count == 1;
+
+  $arr = [];
+  $id = mt_rand(1, 10000);
+  $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = :id');
+  $statement->bindParam(':id', $id, PDO::PARAM_INT);
+
+  while ($query_count--) {
+    $statement->execute();
+    $arr[] = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
+    $id = mt_rand(1, 10000);
+  }
+  if ($is_one) $arr = $arr[0];
+
+  echo json_encode($arr);
+}

+ 31 - 0
frameworks/PHP/workerman/fortune.php

@@ -0,0 +1,31 @@
+<?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.';
+    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><?php echo htmlspecialchars($id, ENT_QUOTES, 'UTF-8'); ?></td>  
+<td><?php echo htmlspecialchars($fortune, ENT_QUOTES, 'UTF-8'); ?></td>
+</tr>
+<?php } ?>
+</table>
+</body>
+</html>
+<?php
+}

+ 55 - 0
frameworks/PHP/workerman/server.php

@@ -0,0 +1,55 @@
+<?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;
+use Workerman\Protocols\Http;
+
+function get_processor_cores_number() {
+  $command = 'cat /proc/cpuinfo | grep processor | wc -l';
+  return  (int) shell_exec($command);
+}
+
+$http_worker = new Worker('http://0.0.0.0:8080');
+$http_worker->count = get_processor_cores_number() * 2 || 8;
+$http_worker->onMessage = function($connection, $data)
+{
+  $pdo = new PDO('mysql:host='.(isset($_ENV['DBHOST'])?$_ENV['DBHOST']:'127.0.0.1').';dbname=hello_world;charset=utf8', 
+  'benchmarkdbuser', 'benchmarkdbpass', array(
+    PDO::ATTR_PERSISTENT => true
+  ));
+  $base = $_SERVER['REQUEST_URI'];
+  $question = strpos($base, '?');
+  if ($question !== false) {
+    $base = substr($base, 0, $question);
+  }
+  Http::header('Date: '.gmdate('D, d M Y H:i:s', time()).' GMT'); 
+  if ($base == '/fortune.php') {
+    Http::header('Content-Type: text/html; charset=utf-8');
+    ob_start();
+    fortune($pdo);
+    $connection->send(ob_get_clean());
+  } else if ($base == '/dbraw.php') {
+    Http::header('Content-Type: application/json');
+    ob_start();
+    dbraw($pdo);
+    $connection->send(ob_get_clean());
+  } else if ($base == '/updateraw.php') {
+    Http::header('Content-Type: application/json');
+    ob_start();
+    updateraw($pdo);
+    $connection->send(ob_get_clean());
+  } else if ($base == '/plaintext.php') {
+    Http::header('Content-Type: text/plain');
+    $connection->send('Hello, World!');
+  } else if ($base == '/json.php') {
+    Http::header('Content-Type: application/json');
+    $connection->send(json_encode(['message'=>'Hello, World!']));
+  } else {
+    Http::header('Content-Type: application/json');
+    $connection->send(json_encode(['message'=>'Hello, World!']));
+  }
+};
+
+Worker::runAll();

+ 10 - 0
frameworks/PHP/workerman/setup.sh

@@ -0,0 +1,10 @@
+#!/bin/bash
+
+fw_depends mysql
+sudo add-apt-repository -y ppa:ondrej/php
+sudo apt-get update
+sudo apt-get -y --force-yes install php-cli php-mysql
+sudo add-apt-repository -y --remove ppa:ondrej/php
+php -r 'file_put_contents("composer.phar", file_get_contents("https://getcomposer.org/composer.phar"));'
+php composer.phar update
+php $TROOT/server.php start -d

+ 33 - 0
frameworks/PHP/workerman/updateraw.php

@@ -0,0 +1,33 @@
+<?php
+function updateraw($pdo) {
+  $query_count = 1;
+  if (isset($_GET['queries']) && $_GET['queries'] > 0) {
+    $query_count = $_GET['queries'];
+  }
+  if ($query_count > 500) $query_count=500;
+
+  $arr = [];
+  $id = mt_rand(1, 10000);
+  $randomNumber = mt_rand(1, 1000);
+
+  $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = :id');
+  $statement->bindParam(':id', $id, PDO::PARAM_INT);
+
+  $updateStatement = $pdo->prepare('UPDATE World SET randomNumber = :randomNumber WHERE id = :id');
+  $updateStatement->bindParam(':id', $id, PDO::PARAM_INT);
+  $updateStatement->bindParam(':randomNumber', $randomNumber, PDO::PARAM_INT);
+
+  while ($query_count--) {
+    $statement->execute();
+    
+    $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
+    $world['randomNumber'] = $randomNumber;
+    $updateStatement->execute();
+    
+    $arr[] = $world;
+    $id = mt_rand(1, 10000);
+    $randomNumber = mt_rand(1, 10000);
+  }
+
+  echo json_encode($arr);
+}