|
@@ -3,55 +3,61 @@ error_reporting(-1);
|
|
|
|
|
|
require_once __DIR__.'/vendor/autoload.php';
|
|
require_once __DIR__.'/vendor/autoload.php';
|
|
|
|
|
|
-$app = new Slim\App(array(
|
|
|
|
- 'db' => function ($c) {
|
|
|
|
- $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world;charset=utf8', 'benchmarkdbuser', 'benchmarkdbpass', array(
|
|
|
|
- PDO::ATTR_PERSISTENT => true,
|
|
|
|
- ));
|
|
|
|
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
- $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
|
|
|
-
|
|
|
|
- return $pdo;
|
|
|
|
- },
|
|
|
|
-
|
|
|
|
- 'view' => function ($c) {
|
|
|
|
- return new Slim\Views\PhpRenderer("templates/");
|
|
|
|
- }
|
|
|
|
-));
|
|
|
|
|
|
+use DI\Container;
|
|
|
|
+use Psr\Http\Message\ResponseInterface as Response;
|
|
|
|
+use Psr\Http\Message\ServerRequestInterface as Request;
|
|
|
|
+use Slim\Factory\AppFactory;
|
|
|
|
+use Slim\Views\PhpRenderer;
|
|
|
|
+
|
|
|
|
+//global $app; // workerman
|
|
|
|
+
|
|
|
|
+$container = new Container();
|
|
|
|
+
|
|
|
|
+$container->set('db', new PDO(
|
|
|
|
+ 'mysql:host=tfb-database;dbname=hello_world;charset=utf8',
|
|
|
|
+ 'benchmarkdbuser',
|
|
|
|
+ 'benchmarkdbpass',
|
|
|
|
+ [
|
|
|
|
+ PDO::ATTR_PERSISTENT => true,
|
|
|
|
+ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
|
|
+ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
|
|
+ //PDO::ATTR_EMULATE_PREPARES => false, // workerman
|
|
|
|
+ ]));
|
|
|
|
+
|
|
|
|
+$container->set('view', new PhpRenderer('templates'));
|
|
|
|
+
|
|
|
|
+AppFactory::setContainer($container);
|
|
|
|
+$app = AppFactory::create();
|
|
|
|
+
|
|
|
|
|
|
// Test 1: Plaintext
|
|
// Test 1: Plaintext
|
|
-$app->get('/plaintext', function ($request, $response) {
|
|
|
|
- return $response
|
|
|
|
|
|
+$app->get('/plaintext', fn(Request $request, Response $response) =>
|
|
|
|
+ $response
|
|
->write('Hello, World!')
|
|
->write('Hello, World!')
|
|
->withHeader('Content-Type', 'text/plain')
|
|
->withHeader('Content-Type', 'text/plain')
|
|
- ;
|
|
|
|
-});
|
|
|
|
|
|
+);
|
|
|
|
|
|
// Test 2: JSON serialization
|
|
// Test 2: JSON serialization
|
|
-$app->get('/json', function ($request, $response) {
|
|
|
|
- return $response
|
|
|
|
- ->withJson(array('message' => 'Hello, World!'))
|
|
|
|
- ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
|
|
|
|
- ;
|
|
|
|
-});
|
|
|
|
|
|
+$app->get('/json', fn(Request $request, Response $response) =>
|
|
|
|
+ $response
|
|
|
|
+ ->withJson(['message' => 'Hello, World!'])
|
|
|
|
+);
|
|
|
|
|
|
// Test 3: Single database query
|
|
// Test 3: Single database query
|
|
-$app->get('/db', function ($request, $response) {
|
|
|
|
- $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
|
|
|
|
- $sth->execute(array(mt_rand(1, 10000)));
|
|
|
|
|
|
+$app->get('/db', function (Request $request, Response $response) {
|
|
|
|
+ $sth = $this->get('db')->prepare('SELECT * FROM World WHERE id = ?');
|
|
|
|
+ $sth->execute([mt_rand(1, 10000)]);
|
|
$world = $sth->fetch();
|
|
$world = $sth->fetch();
|
|
# Cast fields to int so they don't get wrapped with quotes
|
|
# Cast fields to int so they don't get wrapped with quotes
|
|
$world['id'] = (int) $world['id'];
|
|
$world['id'] = (int) $world['id'];
|
|
$world['randomNumber'] = (int) $world['randomNumber'];
|
|
$world['randomNumber'] = (int) $world['randomNumber'];
|
|
|
|
|
|
return $response
|
|
return $response
|
|
- ->withJson($world)
|
|
|
|
- ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
|
|
|
|
- ;
|
|
|
|
|
|
+ ->withJson($world);
|
|
});
|
|
});
|
|
|
|
|
|
// Test 4: Multiple database queries
|
|
// Test 4: Multiple database queries
|
|
-$app->get('/dbs', function ($request, $response) {
|
|
|
|
|
|
+$app->get('/dbs', function (Request $request, Response $response) {
|
|
$queries = $request->getParam('queries');
|
|
$queries = $request->getParam('queries');
|
|
if (is_numeric($queries)) {
|
|
if (is_numeric($queries)) {
|
|
$queries = max(1, min($queries, 500));
|
|
$queries = max(1, min($queries, 500));
|
|
@@ -59,10 +65,10 @@ $app->get('/dbs', function ($request, $response) {
|
|
$queries = 1;
|
|
$queries = 1;
|
|
}
|
|
}
|
|
|
|
|
|
- $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
|
|
|
|
- $worlds = array();
|
|
|
|
|
|
+ $sth = $this->get('db')->prepare('SELECT * FROM World WHERE id = ?');
|
|
|
|
+ $worlds = [];
|
|
for ($i = 0; $i < $queries; ++$i) {
|
|
for ($i = 0; $i < $queries; ++$i) {
|
|
- $sth->execute(array(mt_rand(1, 10000)));
|
|
|
|
|
|
+ $sth->execute([mt_rand(1, 10000)]);
|
|
$world = $sth->fetch();
|
|
$world = $sth->fetch();
|
|
# Cast fields to int so they don't get wrapped with quotes
|
|
# Cast fields to int so they don't get wrapped with quotes
|
|
$world['id'] = (int) $world['id'];
|
|
$world['id'] = (int) $world['id'];
|
|
@@ -71,13 +77,11 @@ $app->get('/dbs', function ($request, $response) {
|
|
}
|
|
}
|
|
|
|
|
|
return $response
|
|
return $response
|
|
- ->withJson($worlds)
|
|
|
|
- ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
|
|
|
|
- ;
|
|
|
|
|
|
+ ->withJson($worlds);
|
|
});
|
|
});
|
|
|
|
|
|
// Test 5: Updates
|
|
// Test 5: Updates
|
|
-$app->get('/updates', function ($request, $response) {
|
|
|
|
|
|
+$app->get('/updates', function (Request $request, Response $response) {
|
|
$queries = $request->getParam('queries');
|
|
$queries = $request->getParam('queries');
|
|
if (is_numeric($queries)) {
|
|
if (is_numeric($queries)) {
|
|
$queries = max(1, min($queries, 500));
|
|
$queries = max(1, min($queries, 500));
|
|
@@ -85,38 +89,48 @@ $app->get('/updates', function ($request, $response) {
|
|
$queries = 1;
|
|
$queries = 1;
|
|
}
|
|
}
|
|
|
|
|
|
- $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
|
|
|
|
- $updateSth = $this->db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
|
|
|
|
|
|
+ $sth = $this->get('db')->prepare('SELECT * FROM World WHERE id = ?');
|
|
|
|
+ $updateSth = $this->get('db')->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
|
|
|
|
|
|
- $worlds = array();
|
|
|
|
|
|
+ $worlds = [];
|
|
for ($i = 0; $i < $queries; ++$i) {
|
|
for ($i = 0; $i < $queries; ++$i) {
|
|
$id = mt_rand(1, 10000);
|
|
$id = mt_rand(1, 10000);
|
|
$random_number = mt_rand(1, 10000);
|
|
$random_number = mt_rand(1, 10000);
|
|
- $sth->execute(array($id));
|
|
|
|
|
|
+ $sth->execute([$id]);
|
|
$world = $sth->fetch();
|
|
$world = $sth->fetch();
|
|
# Cast fields to int so they don't get wrapped with quotes
|
|
# Cast fields to int so they don't get wrapped with quotes
|
|
$world['id'] = (int) $world['id'];
|
|
$world['id'] = (int) $world['id'];
|
|
$world['randomNumber'] = $random_number;
|
|
$world['randomNumber'] = $random_number;
|
|
|
|
|
|
- $updateSth->execute(array($world['randomNumber'], $world['id']));
|
|
|
|
|
|
+ $updateSth->execute([$world['randomNumber'], $world['id']]);
|
|
|
|
|
|
$worlds[] = $world;
|
|
$worlds[] = $world;
|
|
}
|
|
}
|
|
|
|
|
|
return $response
|
|
return $response
|
|
- ->withJson($worlds)
|
|
|
|
- ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
|
|
|
|
- ;
|
|
|
|
|
|
+ ->withJson($worlds);
|
|
});
|
|
});
|
|
|
|
|
|
// Test 6: Fortunes
|
|
// Test 6: Fortunes
|
|
-$app->get('/fortunes', function ($request, $response) {
|
|
|
|
- $fortunes = $this->db->query('SELECT * FROM Fortune')->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
|
|
|
|
+$app->get('/fortunes', function (Request $request, Response $response) {
|
|
|
|
+ $fortunes = $this->get('db')->query('SELECT * FROM Fortune')->fetchAll(PDO::FETCH_KEY_PAIR);
|
|
|
|
|
|
$fortunes[0] = 'Additional fortune added at request time.';
|
|
$fortunes[0] = 'Additional fortune added at request time.';
|
|
asort($fortunes);
|
|
asort($fortunes);
|
|
|
|
|
|
- return $this->view->render($response, "fortunes.php", ["fortunes" => $fortunes]);
|
|
|
|
|
|
+ return $this->get('view')->render($response, 'fortunes.php', ['fortunes' => $fortunes]);
|
|
});
|
|
});
|
|
|
|
|
|
-$app->run();
|
|
|
|
|
|
+$app->run(); // comented with workerman
|
|
|
|
+
|
|
|
|
+// used by Workerman
|
|
|
|
+function run(): string
|
|
|
|
+{
|
|
|
|
+ global $app;
|
|
|
|
+ ob_start();
|
|
|
|
+
|
|
|
|
+ $app->run();
|
|
|
|
+ header(HeaderDate::$date); // To pass the bench, nginx auto add it
|
|
|
|
+
|
|
|
|
+ return ob_get_clean();
|
|
|
|
+}
|