index_raw.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('172.16.98.120', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  18. $row = mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000));
  19. return new Response(json_encode(mysqli_fetch_assoc($row)), 200, array('Content-Type' => 'application/json'));
  20. });
  21. // Test 3: Multiple database queries
  22. $app->get('/queries', function(Request $request) {
  23. $queries = max(1, min($request->query->get('queries'), 500));
  24. $db = new mysqli('172.16.98.120', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  25. for ($i=0; $i<$queries; $i++) {
  26. $rows[] = mysqli_fetch_assoc(mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000)));
  27. }
  28. return new Response(json_encode($rows), 200, array('Content-Type' => 'application/json'));
  29. });
  30. // Test 4: Fortunes
  31. $app->get('/fortunes', function() {
  32. $db = new mysqli('172.16.98.120', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  33. $result = mysqli_query($db, 'SELECT * FROM Fortune');
  34. while ($row = mysqli_fetch_row($result)) {
  35. $fortunes[$row[0]] = htmlspecialchars($row[1], ENT_IGNORE);
  36. }
  37. $fortunes[] = 'Additional fortune added at request time.';
  38. asort($fortunes);
  39. foreach ($fortunes as $i => $fortune) {
  40. $templates[$i] = '<tr><td>'.$i.'</td><td>'.$fortune.'</td></tr>';
  41. }
  42. 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>');
  43. });
  44. // Test 5: Database updates
  45. $app->get('/updates', function(Request $request) {
  46. $queries = max(1, min($request->query->get('queries'), 500));
  47. $db = new mysqli('172.16.98.120', 'benchmarkdbuser', 'benchmarkdbpass', 'hello_world');
  48. for ($i=0; $i<$queries; $i++) {
  49. $rows[] = mysqli_fetch_assoc(mysqli_query($db, 'SELECT id, randomNumber FROM World WHERE id = '.rand(1, 10000)));
  50. }
  51. mysqli_autocommit($db, FALSE);
  52. foreach ($rows as $i => $row) {
  53. $rows[$i]['randomNumber'] = rand(1, 10000);
  54. mysqli_query($db, "UPDATE World SET randomNumber = {$rows[$i]['randomNumber']} WHERE id = {$row['id']}");
  55. }
  56. mysqli_commit($db);
  57. return new Response(json_encode($rows), 200, array('Content-Type' => 'application/json'));
  58. });
  59. // Test 6: Plaintext
  60. $app->get('/plaintext', function() {
  61. return new Response('Hello World!', 200, array('Content-Type' => 'text/plain'));
  62. });
  63. $app->run();