Benchmark.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Controller;
  3. class Benchmark extends \PHPixie\Controller {
  4. public function action_json(){
  5. $this->response->add_header("Content-Type: application/json");
  6. $this->response-> body = json_encode(array('message' => 'Hello, World!'));
  7. }
  8. public function action_db() {
  9. $this->response->add_header("Content-Type: application/json");
  10. $this->response->body = json_encode($this->fetch_random_world()->as_array());
  11. }
  12. public function action_queries() {
  13. $this->response->add_header("Content-Type: application/json");
  14. $n = (int)$this->request->get('queries', 1);
  15. if ($n < 1)
  16. $n = 1;
  17. if ($n > 500)
  18. $n = 500;
  19. $res = array();
  20. for ($i = 0; $i < $n; $i++)
  21. $res[] = $this->fetch_random_world()->as_array();
  22. $this->response->body = json_encode($res);
  23. }
  24. public function action_fortunes() {
  25. $fortunes = $this->pixie-> orm->get('fortune')->find_all()->as_array();
  26. $fortune = $this->pixie->orm->get('fortune');
  27. $fortune->id = 0;
  28. $fortune->message = 'Additional fortune added at request time.';
  29. $fortunes[] = $fortune;
  30. usort($fortunes, function($a, $b) {
  31. if ($a->message===$b->message) return 0;
  32. return ($a->message<$b->message)?-1:1;
  33. } );
  34. $view = $this->pixie->view('fortunes');
  35. $view->fortunes = $fortunes;
  36. $this->response->body = $view->render();
  37. }
  38. public function action_updates(){
  39. $this->response->add_header("Content-Type: application/json");
  40. $n = (int)$this->request->get('queries', 1);
  41. if ($n < 1)
  42. $n = 1;
  43. if ($n > 500)
  44. $n = 500;
  45. $res = array();
  46. for ($i = 0; $i < $n; $i++) {
  47. $world = $this->fetch_random_world();
  48. $world->randomNumber = rand(1, 10000);
  49. $world->save();
  50. $res[]=$world->as_array();
  51. }
  52. $this->response->body = json_encode($res);
  53. }
  54. public function action_plaintext() {
  55. $this->response-> add_header("Content-Type: application/json");
  56. $this->response->body = "Hello, World!";
  57. }
  58. protected function fetch_random_world() {
  59. return $this->pixie->orm->get('world')
  60. ->where('id', rand(1, 10000))
  61. ->find();
  62. }
  63. }