UuidGenerationBench.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * This file is part of the ramsey/uuid library
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
  9. * @license http://opensource.org/licenses/MIT MIT
  10. */
  11. declare(strict_types=1);
  12. namespace Ramsey\Uuid\Benchmark;
  13. use DateTimeImmutable;
  14. use Ramsey\Uuid\Provider\Node\StaticNodeProvider;
  15. use Ramsey\Uuid\Type\Hexadecimal;
  16. use Ramsey\Uuid\Type\Integer as IntegerIdentifier;
  17. use Ramsey\Uuid\Uuid;
  18. use Ramsey\Uuid\UuidInterface;
  19. final class UuidGenerationBench
  20. {
  21. /** @var Hexadecimal */
  22. private $node;
  23. /** @var int */
  24. private $clockSequence;
  25. /** @var IntegerIdentifier */
  26. private $localIdentifier;
  27. /** @var UuidInterface */
  28. private $namespace;
  29. public function __construct()
  30. {
  31. $this->node = (new StaticNodeProvider(new Hexadecimal('121212121212')))
  32. ->getNode();
  33. $this->clockSequence = 16383;
  34. $this->localIdentifier = new IntegerIdentifier(5);
  35. $this->namespace = Uuid::fromString('c485840e-9389-4548-a276-aeecd9730e50');
  36. }
  37. public function benchUuid1GenerationWithoutParameters(): void
  38. {
  39. Uuid::uuid1();
  40. }
  41. public function benchUuid1GenerationWithNode(): void
  42. {
  43. Uuid::uuid1($this->node);
  44. }
  45. public function benchUuid1GenerationWithNodeAndClockSequence(): void
  46. {
  47. Uuid::uuid1($this->node, $this->clockSequence);
  48. }
  49. public function benchUuid2GenerationWithDomainAndLocalIdentifier(): void
  50. {
  51. Uuid::uuid2(Uuid::DCE_DOMAIN_ORG, $this->localIdentifier);
  52. }
  53. public function benchUuid2GenerationWithDomainAndLocalIdentifierAndNode(): void
  54. {
  55. Uuid::uuid2(Uuid::DCE_DOMAIN_ORG, $this->localIdentifier, $this->node);
  56. }
  57. public function benchUuid2GenerationWithDomainAndLocalIdentifierAndNodeAndClockSequence(): void
  58. {
  59. Uuid::uuid2(Uuid::DCE_DOMAIN_ORG, $this->localIdentifier, $this->node, 63);
  60. }
  61. public function benchUuid3Generation(): void
  62. {
  63. Uuid::uuid3($this->namespace, 'name');
  64. }
  65. public function benchUuid4Generation(): void
  66. {
  67. Uuid::uuid4();
  68. }
  69. public function benchUuid5Generation(): void
  70. {
  71. Uuid::uuid5($this->namespace, 'name');
  72. }
  73. public function benchUuid6GenerationWithoutParameters(): void
  74. {
  75. Uuid::uuid6();
  76. }
  77. public function benchUuid6GenerationWithNode(): void
  78. {
  79. Uuid::uuid6($this->node);
  80. }
  81. public function benchUuid6GenerationWithNodeAndClockSequence(): void
  82. {
  83. Uuid::uuid6($this->node, $this->clockSequence);
  84. }
  85. public function benchUuid7Generation(): void
  86. {
  87. Uuid::uuid7();
  88. }
  89. public function benchUuid7GenerationWithDateTime(): void
  90. {
  91. Uuid::uuid7(new DateTimeImmutable('@1663203901.667000'));
  92. }
  93. }