Skip to content

Commit

Permalink
Fix slim hhvm fortunes, some cleanup, and a little enhancement (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
herloct authored and knewmanTE committed Nov 4, 2016
1 parent 94b14d0 commit 67ff278
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
1 change: 1 addition & 0 deletions frameworks/PHP/slim/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"db_url": "/db",
"query_url": "/dbs?queries=",
"update_url": "/updates?queries=",
"fortune_url": "/fortunes",
"port": 8080,
"approach": "Realistic",
"classification": "Micro",
Expand Down
33 changes: 20 additions & 13 deletions frameworks/PHP/slim/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@

require_once __DIR__.'/vendor/autoload.php';

$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/");
$app = new Slim\App(array(
'db' => function ($c) {
$pdo = new PDO('mysql:host=localhost;dbname=hello_world;charset=utf8', 'benchmarkdbuser', 'benchmarkdbpass');
$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/");
}
));

// Test 1: Plaintext
$app->get('/plaintext', function ($request, $response) {
Expand All @@ -30,7 +33,6 @@
;
});


// Test 3: Single database query
$app->get('/db', function ($request, $response) {
$sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
Expand Down Expand Up @@ -72,6 +74,8 @@
$queries = max(1, min($request->getParam('queries'), 500));

$sth = $this->db->prepare('SELECT * FROM World WHERE id = ?');
$updateSth = $this->db->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');

$worlds = array();
for ($i = 0; $i < $queries; ++$i) {
$id = mt_rand(1, 10000);
Expand All @@ -81,8 +85,9 @@
# 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']));

$updateSth->execute(array($world['randomNumber'], $world['id']));

$worlds[] = $world;
}

Expand All @@ -97,10 +102,12 @@
$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]);
});

Expand Down

0 comments on commit 67ff278

Please sign in to comment.