Benchmark.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. $am = $a->message;
  32. $bm = $b->message;
  33. if ($am==$bm) return 0;
  34. return ($am<$bm)?-1:1;
  35. } );
  36. $view = $this->pixie->view('fortunes');
  37. $view->fortunes = $fortunes;
  38. $this->response->body = $view->render();
  39. }
  40. public function action_updates(){
  41. $this->response->add_header("Content-Type: application/json");
  42. $n = (int)$this->request->get('queries', 1);
  43. if ($n < 1)
  44. $n = 1;
  45. if ($n > 500)
  46. $n = 500;
  47. $res = array();
  48. for ($i = 0; $i < $n; $i++) {
  49. $world = $this->fetch_random_world();
  50. $world->randomNumber = rand(1, 10000);
  51. $world->save();
  52. $res[]=$world->as_array();
  53. }
  54. $this->response->body = json_encode($res);
  55. }
  56. public function action_plaintext() {
  57. $this->response-> add_header("Content-Type: application/json");
  58. $this->response->body = "Hello, World!";
  59. }
  60. protected function fetch_random_world() {
  61. return $this->pixie->orm->get('world')
  62. ->where('id', rand(1, 10000))
  63. ->find();
  64. }
  65. }