DbResource.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Benchmark\Resources;
  3. use Hamlet\Database\Database;
  4. use Hamlet\Entities\JsonEntity;
  5. use Hamlet\Requests\Request;
  6. use Hamlet\Resources\WebResource;
  7. use Hamlet\Responses\OKResponse;
  8. use Hamlet\Responses\Response;
  9. class DbResource implements WebResource
  10. {
  11. private $database;
  12. public function __construct(Database $database)
  13. {
  14. $this->database = $database;
  15. }
  16. public function getResponse(Request $request): Response
  17. {
  18. $count = $request->parameter('queries');
  19. if ($count !== null && $count > 0) {
  20. $count = min($count, 500);
  21. } else {
  22. $count = 1;
  23. }
  24. $query = '
  25. SELECT id,
  26. randomNumber
  27. FROM World
  28. WHERE id = ?
  29. ';
  30. $procedure = $this->database->prepare($query);
  31. $payload = [];
  32. while ($count-- > 0) {
  33. $id = mt_rand(1, 10000);
  34. $procedure->bindInteger($id);
  35. $payload[] = $procedure->fetchOne();
  36. }
  37. if (!$request->hasParameter('queries')) {
  38. $payload = $payload[0];
  39. }
  40. return new OKResponse(new JsonEntity($payload));
  41. }
  42. }