index_raw.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. require_once __DIR__.'/../vendor/autoload.php';
  5. $app = new Silex\Application();
  6. // Test 1: JSON serialization
  7. $app->get('/json', function() {
  8. // The following code would be handier:
  9. // return new JsonResponse(array("message" => "Hello World!"));
  10. //
  11. // But JsonResponse does some checks that are unneeded for this benchmark
  12. // and therefore, a plain Response object is faster.
  13. return new Response(json_encode(array('message' => 'Hello, World!')), 200, array('Content-Type' => 'application/json'));
  14. });
  15. // Test 2: Single database query
  16. $app->get('/db', function() {
  17. $db = new mysqli('localhost', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  18. $row = mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000));
  19. $result = mysqli_fetch_assoc($row);
  20. $result['id'] = (int) $result['id'];
  21. $result['randomNumber'] = (int) $result['randomNumber'];
  22. return new Response(json_encode($result), 200, array('Content-Type' => 'application/json'));
  23. });
  24. // Test 3: Multiple database queries
  25. $app->get('/queries', function(Request $request) {
  26. $queries = max(1, min($request->query->get('queries'), 500));
  27. $db = new mysqli('localhost', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  28. for ($i=0; $i<$queries; $i++) {
  29. $result = mysqli_fetch_assoc(mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000)));
  30. $result['id'] = (int) $result['id'];
  31. $result['randomNumber'] = (int) $result['randomNumber'];
  32. $rows[] = $result;
  33. }
  34. return new Response(json_encode($rows), 200, array('Content-Type' => 'application/json'));
  35. });
  36. // Test 4: Fortunes
  37. $app->get('/fortunes', function() {
  38. $db = new mysqli('localhost', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  39. $result = mysqli_query($db, 'SELECT * FROM Fortune');
  40. while ($row = mysqli_fetch_row($result)) {
  41. $fortunes[$row[0]] = htmlspecialchars($row[1], ENT_IGNORE);
  42. }
  43. $fortunes[0] = 'Additional fortune added at request time.';
  44. asort($fortunes);
  45. foreach ($fortunes as $i => $fortune) {
  46. $templates[$i] = '<tr><td>'.$i.'</td><td>'.$fortune.'</td></tr>';
  47. }
  48. return new Response('<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'.implode('', $templates).'</table></body></html>');
  49. });
  50. // Test 5: Database updates
  51. $app->get('/updates', function(Request $request) {
  52. $queries = max(1, min($request->query->get('queries'), 500));
  53. $db = new mysqli('localhost', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  54. for ($i=0; $i<$queries; $i++) {
  55. $rows[] = mysqli_fetch_assoc(mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000)));
  56. }
  57. mysqli_autocommit($db, FALSE);
  58. foreach ($rows as $i => $row) {
  59. $rows[$i]['id'] = (int) $rows[$i]['id'];
  60. $rows[$i]['randomNumber'] = rand(1, 10000);
  61. mysqli_query($db, "UPDATE World SET randomNumber = {$rows[$i]['randomNumber']} WHERE id = {$row['id']}");
  62. }
  63. mysqli_commit($db);
  64. return new Response(json_encode($rows), 200, array('Content-Type' => 'application/json'));
  65. });
  66. // Test 6: Plaintext
  67. $app->get('/plaintext', function() {
  68. return new Response('Hello, World!', 200, array('Content-Type' => 'text/plain'));
  69. });
  70. $app->run();