|
@@ -4,8 +4,25 @@ error_reporting(-1);
|
|
require_once __DIR__.'/vendor/autoload.php';
|
|
require_once __DIR__.'/vendor/autoload.php';
|
|
|
|
|
|
$app = new \Slim\App;
|
|
$app = new \Slim\App;
|
|
|
|
+$container = $app->getContainer();
|
|
|
|
+$container['db'] = function ($c) {
|
|
|
|
+ $db = $c['settings']['db'];
|
|
|
|
+ $pdo = new PDO('mysql:host=localhost;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
|
|
|
|
+ $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
+ $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
|
|
|
+ return $pdo;
|
|
|
|
+};
|
|
|
|
+$container['view'] = new \Slim\Views\PhpRenderer("templates/");
|
|
|
|
+
|
|
|
|
+// Test 1: Plaintext
|
|
|
|
+$app->get('/plaintext', function ($request, $response) {
|
|
|
|
+ return $response
|
|
|
|
+ ->write('Hello, World!')
|
|
|
|
+ ->withHeader('Content-Type', 'text/plain')
|
|
|
|
+ ;
|
|
|
|
+});
|
|
|
|
|
|
-// Test 1: JSON serialization
|
|
|
|
|
|
+// Test 2: JSON serialization
|
|
$app->get('/json', function ($request, $response) {
|
|
$app->get('/json', function ($request, $response) {
|
|
return $response
|
|
return $response
|
|
->withJson(array('message' => 'Hello, World!'))
|
|
->withJson(array('message' => 'Hello, World!'))
|
|
@@ -13,16 +30,8 @@ $app->get('/json', function ($request, $response) {
|
|
;
|
|
;
|
|
});
|
|
});
|
|
|
|
|
|
-$container = $app->getContainer();
|
|
|
|
-$container['db'] = function ($c) {
|
|
|
|
- $db = $c['settings']['db'];
|
|
|
|
- $pdo = new PDO('mysql:host=localhost;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass');
|
|
|
|
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
- $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
|
|
|
|
- return $pdo;
|
|
|
|
-};
|
|
|
|
|
|
|
|
-// Test 2: Single database query
|
|
|
|
|
|
+// Test 3: Single database query
|
|
$app->get('/db', function ($request, $response) {
|
|
$app->get('/db', function ($request, $response) {
|
|
$sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
|
|
$sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
|
|
$sth->execute(array(mt_rand(1, 10000)));
|
|
$sth->execute(array(mt_rand(1, 10000)));
|
|
@@ -37,7 +46,7 @@ $app->get('/db', function ($request, $response) {
|
|
;
|
|
;
|
|
});
|
|
});
|
|
|
|
|
|
-// Test 3: Multiple database queries
|
|
|
|
|
|
+// Test 4: Multiple database queries
|
|
$app->get('/dbs', function ($request, $response) {
|
|
$app->get('/dbs', function ($request, $response) {
|
|
$queries = max(1, min($request->getParam('queries'), 500));
|
|
$queries = max(1, min($request->getParam('queries'), 500));
|
|
|
|
|
|
@@ -58,4 +67,41 @@ $app->get('/dbs', function ($request, $response) {
|
|
;
|
|
;
|
|
});
|
|
});
|
|
|
|
|
|
|
|
+// Test 5: Updates
|
|
|
|
+$app->get('/updates', function ($request, $response) {
|
|
|
|
+ $queries = max(1, min($request->getParam('queries'), 500));
|
|
|
|
+
|
|
|
|
+ $sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
|
|
|
|
+ $worlds = array();
|
|
|
|
+ for ($i = 0; $i < $queries; ++$i) {
|
|
|
|
+ $id = mt_rand(1, 10000);
|
|
|
|
+ $random_number = mt_rand(1, 10000);
|
|
|
|
+ $sth->execute(array($id));
|
|
|
|
+ $world = $sth->fetch();
|
|
|
|
+ # Cast fields to int so they don't get wrapped with quotes
|
|
|
|
+ $world['id'] = (int) $world['id'];
|
|
|
|
+ $world['randomNumber'] = $random_number;
|
|
|
|
+ $update_query = $this->db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
|
|
|
|
+ $update_query->execute(array($world['randomNumber'], $world['id']));
|
|
|
|
+ $worlds[] = $world;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $response
|
|
|
|
+ ->withJson($worlds)
|
|
|
|
+ ->withHeader('Content-Type', 'application/json') // fixes utf-8 warning
|
|
|
|
+ ;
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+// Test 6: Fortunes
|
|
|
|
+$app->get('/fortunes', function ($request, $response) {
|
|
|
|
+ $sth = $this->db->prepare('SELECT * FROM Fortune');
|
|
|
|
+ $sth->execute();
|
|
|
|
+ $fortunes = $sth->fetchAll();
|
|
|
|
+ array_push($fortunes, array('id'=> 0, 'message' => 'Additional fortune added at request time.'));
|
|
|
|
+ usort($fortunes, function($left, $right) {
|
|
|
|
+ return strcmp($left['message'], $right['message']);
|
|
|
|
+ });
|
|
|
|
+ return $this->view->render($response, "fortunes.php", ["fortunes" => $fortunes]);
|
|
|
|
+});
|
|
|
|
+
|
|
$app->run();
|
|
$app->run();
|