QueriesResource.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Benchmark\Resources;
  3. use Benchmark\Entities\RandomNumber;
  4. use Hamlet\Database\Database;
  5. use Hamlet\Database\Procedure;
  6. use Hamlet\Http\Entities\JsonEntity;
  7. use Hamlet\Http\Requests\Request;
  8. use Hamlet\Http\Responses\Response;
  9. use Hamlet\Http\Responses\SimpleOKResponse;
  10. class QueriesResource extends DbResource
  11. {
  12. /** @var Procedure */
  13. private $procedure;
  14. public function __construct(Database $database)
  15. {
  16. parent::__construct($database);
  17. $query = '
  18. SELECT id,
  19. randomNumber
  20. FROM World
  21. WHERE id = ?
  22. ';
  23. $this->procedure = $this->database->prepare($query);
  24. }
  25. public function getResponse(Request $request): Response
  26. {
  27. $count = $this->getQueriesCount($request);
  28. $payload = [];
  29. while ($count-- > 0) {
  30. $id = mt_rand(1, 10000);
  31. $this->procedure->bindInteger($id);
  32. $payload[] = $this->procedure->processOne()
  33. ->selectAll()->cast(RandomNumber::class)
  34. ->collectHead();
  35. }
  36. return new SimpleOKResponse(new JsonEntity($payload));
  37. }
  38. }