Browse Source

add Simps (#5618)

* add Simps

* update docker

* update readme

* Update frameworks/PHP/simps/benchmark_config.json

Co-Authored-By: Joan Miquel <[email protected]>

* add query_url

* Update DbModel.php

Co-authored-by: Joan Miquel <[email protected]>
Luffy 5 years ago
parent
commit
2f8c3f86e1

+ 1 - 1
.travis.yml

@@ -63,7 +63,7 @@ env:
     - 'TESTDIR="PHP/php"'
     - 'TESTDIR="PHP/cakephp PHP/codeigniter PHP/fat-free PHP/fuel PHP/kumbiaphp PHP/phpixie PHP/slim PHP/symfony PHP/yii2 PHP/zend PHP/spiral"'
     - 'TESTDIR="PHP/amp PHP/hhvm PHP/peachpie PHP/php-ngx PHP/workerman PHP/phalcon"'
-    - 'TESTDIR="PHP/hamlet PHP/laravel PHP/lumen PHP/swoole PHP/ubiquity PHP/hyperf PHP/sw-fw-less PHP/imi"'
+    - 'TESTDIR="PHP/hamlet PHP/laravel PHP/lumen PHP/swoole PHP/ubiquity PHP/hyperf PHP/sw-fw-less PHP/imi PHP/simps"'
     - 'TESTDIR="Python/aiohttp Python/api_hour Python/apidaora Python/blacksheep Python/bottle Python/cherrypy Python/django Python/emmett Python/eve Python/falcon Python/fastapi Python/flask"'
     - 'TESTDIR="Python/hug Python/japronto Python/klein Python/morepath Python/pyramid Python/quart Python/responder Python/sanic Python/spyne Python/starlette"'
     - 'TESTDIR="Python/tornado Python/turbogears Python/uvicorn Python/uwsgi Python/vibora Python/web2py Python/webware Python/weppy Python/wsgi"'

+ 55 - 0
frameworks/PHP/simps/README.md

@@ -0,0 +1,55 @@
+<p align="center">
+    <a href="https://simps.io" target="_blank">
+        <img src="https://cdn.jsdelivr.net/gh/sy-records/staticfile/images/simps.png" alt="Simps" />
+    </a>
+</p>
+
+[![Simps License](https://img.shields.io/packagist/l/simple-swoole/simps?color=blue)](https://github.com/simple-swoole/simps/blob/master/LICENSE)
+[![Latest Version](https://img.shields.io/packagist/v/simple-swoole/simps.svg)](https://packagist.org/packages/simple-swoole/simps)
+[![Simps Doc](https://img.shields.io/badge/docs-passing-blue.svg)](https://doc.simps.io)
+[![Contact Simps Team](https://img.shields.io/badge/[email protected]?style=flat)](mailto:[email protected])
+[![Php Version](https://img.shields.io/badge/php-%3E=7.2-brightgreen.svg)](https://www.php.net)
+[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.4.0-brightgreen.svg)](https://github.com/swoole/swoole-src)
+
+# Simps Benchmarking Test
+
+## About Simps
+
+🚀 A simple, lightweight and high-performance PHP coroutine framework.
+
+[Home Page](https://simps.io)
+[Document](https://doc.simps.io)
+
+## Important Libraries
+
+The tests were run with:
+
+* [PHP7](https://www.php.net)
+* [Swoole4](https://www.swoole.com)
+* [Simps](https://simps.io)
+
+## Test URLs
+
+### JSON
+
+http://localhost:8080/json
+
+### PLAINTEXT
+
+http://localhost:8080/plaintext
+
+### DB
+
+http://localhost:8080/db
+
+### UPDATE
+
+http://localhost:8080/updates/
+
+### FORTUNES
+
+http://localhost:8080/fortunes
+
+### QUERY
+
+http://localhost:8080/queries/

+ 123 - 0
frameworks/PHP/simps/app/Controller/IndexController.php

@@ -0,0 +1,123 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * This file is part of Simps.
+ *
+ * @link     https://simps.io
+ * @document https://doc.simps.io
+ * @license  https://github.com/simple-swoole/simps/blob/master/LICENSE
+ */
+
+namespace App\Controller;
+
+use Simps\Server\Protocol\HTTP\SimpleResponse;
+use App\Model\DbModel;
+
+class IndexController
+{
+    public function index($server, $fd)
+    {
+        $server->send(
+            $fd,
+            SimpleResponse::build(
+                json_encode(['message' => 'Hello, World!']),
+                200,
+                ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
+            )
+        );
+    }
+
+    public function plaintext($server, $fd)
+    {
+        $server->send(
+            $fd,
+            SimpleResponse::build(
+                'Hello, World!',
+                200,
+                ['Content-Type' => 'text/plain', 'Date' => gmdate("D, d M Y H:i:s T")]
+            )
+        );
+    }
+
+    public function fortunes($server, $fd)
+    {
+        $db = new DbModel();
+        $fortune = $db->fortunes();
+        $html = '';
+        foreach ($fortune as $id => $message) {
+            $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
+            $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
+        }
+
+        $data = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'
+            . $html .
+            '</table></body></html>';
+
+        $server->send(
+            $fd,
+            SimpleResponse::build(
+                $data,
+                200,
+                ['Content-Type' => 'text/html; charset=utf-8', 'Date' => gmdate("D, d M Y H:i:s T")]
+            )
+        );
+    }
+
+    public function db($server, $fd, $data)
+    {
+        $db = new DbModel();
+        if (isset($data['queries'])) {
+            $res = $db->db((int)$data['queries']);
+        } else {
+            $res = $db->db(-1);
+        }
+
+        $server->send(
+            $fd,
+            SimpleResponse::build(
+                $res,
+                200,
+                ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
+            )
+        );
+    }
+
+    public function queries($server, $fd, $data)
+    {
+        $db = new DbModel();
+        if (isset($data['queries'])) {
+            $res = $db->db((int)$data['queries']);
+        } else {
+            $res = $db->db();
+        }
+
+        $server->send(
+            $fd,
+            SimpleResponse::build(
+                $res,
+                200,
+                ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
+            )
+        );
+    }
+
+    public function updates($server, $fd, $data)
+    {
+        $db = new DbModel();
+        if (isset($data['queries'])) {
+            $res = $db->updates((int)$data['queries']);
+        } else {
+            $res = $db->updates(-1);
+        }
+
+        $server->send(
+            $fd,
+            SimpleResponse::build(
+                $res,
+                200,
+                ['Content-Type' => 'application/json', 'Date' => gmdate("D, d M Y H:i:s T")]
+            )
+        );
+    }
+}

+ 28 - 0
frameworks/PHP/simps/app/Listens/Pool.php

@@ -0,0 +1,28 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * This file is part of Simps.
+ *
+ * @link     https://simps.io
+ * @document https://doc.simps.io
+ * @license  https://github.com/simple-swoole/simps/blob/master/LICENSE
+ */
+
+namespace App\Listens;
+
+use Simps\DB\PDO;
+use Simps\Singleton;
+
+class Pool
+{
+    use Singleton;
+
+    public function workerStart($server, $workerId)
+    {
+        $config = config('database', []);
+        if (! empty($config)) {
+            PDO::getInstance($config);
+        }
+    }
+}

+ 97 - 0
frameworks/PHP/simps/app/Model/DbModel.php

@@ -0,0 +1,97 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * This file is part of Simps.
+ *
+ * @link     https://simps.io
+ * @document https://doc.simps.io
+ * @license  https://github.com/simple-swoole/simps/blob/master/LICENSE
+ */
+
+namespace App\Model;
+
+use Simps\DB\BaseModel;
+
+class DbModel extends BaseModel
+{
+    public function fortunes()
+    {
+        $fortune = [];
+        $this->pdo->fortune_test = $this->pdo->fortune_test ?? $this->pdo->prepare('SELECT id, message FROM Fortune');
+        $this->pdo->fortune_test->execute();
+        $arr = $this->pdo->fortune_test->fetchAll();
+        foreach ($arr as $row) {
+            $fortune[$row['id']] = $row['message'];
+        }
+        $fortune[0] = 'Additional fortune added at request time.';
+        \asort($fortune);
+        return $fortune;
+    }
+
+    public function updates(int $queries = 0)
+    {
+        $query_count = 1;
+        if ($queries > 1) {
+            $query_count = $queries > 500 ? 500 : $queries;
+        }
+
+        $arr = [];
+        $this->pdo->updates_test_select = $this->pdo->updates_test_select ?? $this->pdo->prepare(
+                'SELECT id, randomNumber FROM World WHERE id = ?'
+            );
+        $this->pdo->updates_test_update = $this->pdo->updates_test_update ?? $this->pdo->prepare(
+                'UPDATE World SET randomNumber = ? WHERE id = ?'
+            );
+
+        while ($query_count--) {
+            $id = mt_rand(1, 10000);
+            $randomNumber = mt_rand(1, 10000);
+            $this->pdo->updates_test_select->execute([$id]);
+            $ret = $this->pdo->updates_test_select->fetchAll();
+
+            // Store result in array.
+            $world = ['id' => $id, 'randomNumber' => $ret[0]['randomNumber']];
+            $world['randomNumber'] = $randomNumber;
+            $this->pdo->updates_test_update->execute([$randomNumber, $id]);
+
+            $arr[] = $world;
+        }
+
+        return \json_encode($arr, JSON_NUMERIC_CHECK);
+    }
+
+    public function db(int $queries = 0)
+    {
+        // 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
+        $this->pdo->db_test = $this->pdo->db_test ?? $this->pdo->prepare(
+                'SELECT id, randomNumber FROM World WHERE id = ?'
+            );
+
+        // For each query, store the result set values in the response array
+        while ($query_count--) {
+            $id = mt_rand(1, 10000);
+            $this->pdo->db_test->execute([$id]);
+            $data = $this->pdo->db_test->fetchAll();
+
+            // Store result in array.
+            $arr[] = ['id' => $id, 'randomNumber' => $data[0]['randomNumber']];
+        }
+
+        // Use the PHP standard JSON encoder.
+        // http://www.php.net/manual/en/function.json-encode.php
+        if ($queries === -1) {
+            $arr = $arr[0];
+        }
+
+        return \json_encode($arr, JSON_NUMERIC_CHECK);
+    }
+}

+ 30 - 0
frameworks/PHP/simps/benchmark_config.json

@@ -0,0 +1,30 @@
+{
+  "framework": "simps",
+  "tests": [
+    {
+      "default": {
+        "json_url": "/json",
+        "plaintext_url": "/plaintext",
+        "db_url": "/db",
+        "fortune_url": "/fortunes",
+        "update_url": "/updates/",
+        "query_url": "/queries/",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Micro",
+        "database": "MySQL",
+        "framework": "Simps",
+        "language": "PHP",
+        "flavor": "None",
+        "orm": "Raw",
+        "platform": "swoole",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Simps",
+        "notes": "",
+        "versus": "swoole"
+      }
+    }
+  ]
+}

+ 11 - 0
frameworks/PHP/simps/composer.json

@@ -0,0 +1,11 @@
+{
+  "require": {
+    "simple-swoole/simps": "~1.0.0",
+    "simple-swoole/db": "~1.0.0"
+  },
+  "autoload": {
+    "psr-4": {
+      "App\\": "app/"
+    }
+  }
+}

+ 15 - 0
frameworks/PHP/simps/config/database.php

@@ -0,0 +1,15 @@
+<?php
+
+declare(strict_types=1);
+
+return [
+    'host' => 'tfb-database',
+    'port' => 3306,
+    'database' => 'hello_world',
+    'username' => 'benchmarkdbuser',
+    'password' => 'benchmarkdbpass',
+    'charset' => 'utf8mb4',
+    'options' => [
+    ],
+    'size' => \intdiv(512, swoole_cpu_num()) // 连接池size
+];

+ 9 - 0
frameworks/PHP/simps/config/listeners.php

@@ -0,0 +1,9 @@
+<?php
+
+declare(strict_types=1);
+
+return [
+    'simpleWorkerStart' => [
+        [\App\Listens\Pool::class, 'workerStart'],
+    ],
+];

+ 19 - 0
frameworks/PHP/simps/config/routes.php

@@ -0,0 +1,19 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * This file is part of Simps.
+ *
+ * @link     https://simps.io
+ * @document https://doc.simps.io
+ * @license  https://github.com/simple-swoole/simps/blob/master/LICENSE
+ */
+
+return [
+    ['GET', '/json', '\App\Controller\IndexController@index'],
+    ['GET', '/plaintext', '\App\Controller\IndexController@plaintext'],
+    ['GET', '/fortunes', '\App\Controller\IndexController@fortunes'],
+    ['GET', '/db', '\App\Controller\IndexController@db'],
+    ['GET', '/queries/[{queries}]', '\App\Controller\IndexController@queries'],
+    ['GET', '/updates/[{queries}]', '\App\Controller\IndexController@updates'],
+];

+ 25 - 0
frameworks/PHP/simps/config/servers.php

@@ -0,0 +1,25 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * This file is part of Simps.
+ *
+ * @link     https://simps.io
+ * @document https://doc.simps.io
+ * @license  https://github.com/simple-swoole/simps/blob/master/LICENSE
+ */
+
+return [
+    'mode' => SWOOLE_BASE,
+    'http' => [
+        'ip' => '0.0.0.0',
+        'port' => 8080,
+        'sock_type' => SWOOLE_SOCK_TCP,
+        'callbacks' => [
+        ],
+        'settings' => [
+            'worker_num' => swoole_cpu_num(),
+            'only_simple_http' => true,
+        ],
+    ],
+];

+ 2 - 0
frameworks/PHP/simps/php.ini

@@ -0,0 +1,2 @@
+opcache.enable_cli=1
+opcache.validate_timestamps=0

+ 8 - 0
frameworks/PHP/simps/sbin/simps.php

@@ -0,0 +1,8 @@
+#!/usr/bin/env php
+<?php
+! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
+! defined('CONFIG_PATH') && define('CONFIG_PATH', dirname(__DIR__) . '/config/');
+
+require BASE_PATH . '/vendor/autoload.php';
+
+Simps\Application::run();

+ 20 - 0
frameworks/PHP/simps/simps.dockerfile

@@ -0,0 +1,20 @@
+FROM php:7.4
+
+RUN pecl install swoole > /dev/null && \
+    docker-php-ext-enable swoole
+
+RUN docker-php-ext-install pdo_mysql > /dev/null
+
+RUN apt -yqq update > /dev/null && \
+    apt -yqq install git unzip > /dev/null
+
+WORKDIR /simps
+
+COPY . /simps
+COPY php.ini /usr/local/etc/php/
+
+RUN curl -sSL https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
+RUN composer install --no-dev --classmap-authoritative --quiet > /dev/null
+RUN composer dumpautoload -o
+
+CMD php sbin/simps.php http:start