Skip to content

Commit f84f0eb

Browse files
authored
Benchmark cmp (#21)
1 parent 93823b9 commit f84f0eb

File tree

5 files changed

+92
-10
lines changed

5 files changed

+92
-10
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,26 @@ A small PHP class for generating [RFC 4122](http://tools.ietf.org/html/rfc4122)
77

88
If all you want is a unique ID, you should call `uuid4()`.
99

10+
## Minimal UUID v4 implementation
11+
12+
Credits go to [this answer](https://stackoverflow.com/a/15875555) on Stackoverflow for this minimal RFC 4122 compliant solution.
13+
```php
14+
<?php
15+
function uuid4()
16+
{
17+
$data = random_bytes(16);
18+
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
19+
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
20+
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
21+
}
22+
23+
echo uuid4();
24+
```
25+
1026
## Installation
1127

28+
If you need comparison tools or sortable identifiers like in version 6, you might find this small and fast package useful. It doesn't require any other dependencies.
29+
1230
```bash
1331
composer require oittaa/uuid
1432
```

phpbench.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"runner.bootstrap": "vendor/autoload.php",
33
"runner.path": "tests/Benchmark",
4+
"runner.retry_threshold": 5,
45
"runner.iterations": 5,
56
"runner.revs": 1000
67
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace UUID\Benchmark;
6+
7+
use UUID\UUID;
8+
9+
class UUIDComparisonBench
10+
{
11+
public function benchComparisonWithEquals(): void
12+
{
13+
UUID::cmp(
14+
'urn:uuid:c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
15+
'{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}'
16+
);
17+
}
18+
19+
public function benchComparisonWithDifference(): void
20+
{
21+
UUID::cmp(
22+
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
23+
'2140a926-4a47-465c-b622-4571ad9bb378'
24+
);
25+
}
26+
27+
public function benchEquals(): void
28+
{
29+
UUID::equals(
30+
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
31+
'C4A760A8-DBCF-5254-A0D9-6A4474BD1B62'
32+
);
33+
}
34+
35+
public function benchNotEquals(): void
36+
{
37+
UUID::equals(
38+
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62',
39+
'c4a760a8-dbcf-5254-a0d9-6a4474bd1b63'
40+
);
41+
}
42+
43+
public function benchIsValid(): void
44+
{
45+
UUID::isValid('11a38b9a-b3da-360f-9353-a5a725514269');
46+
}
47+
48+
public function benchIsNotValid(): void
49+
{
50+
UUID::isValid('11a38b9a-b3da-xxxx-9353-a5a725514269');
51+
}
52+
}

tests/Benchmark/UUIDGenerationBench.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ public function benchUUID6Generation(): void
2727
{
2828
UUID::uuid6();
2929
}
30+
public function benchUUIDToString(): void
31+
{
32+
UUID::toString('{C4A760A8-DBCF-5254-A0D9-6A4474BD1B62}');
33+
}
3034
}

tests/UuidTest.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ public function testCanGenerateValidVersion3()
2323
public function testCanGenerateValidVersion4()
2424
{
2525
$uuid1 = UUID::uuid4();
26-
$this->assertMatchesRegularExpression(
27-
'/^[0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12}$/',
28-
$uuid1
29-
);
30-
$uuid2 = UUID::uuid4();
31-
$this->assertNotEquals(
32-
$uuid1,
33-
$uuid2
34-
);
26+
for ($x = 0; $x < 10; $x++) {
27+
$this->assertMatchesRegularExpression(
28+
'/^[0-9a-f]{8}\-[0-9a-f]{4}\-4[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}$/',
29+
$uuid1
30+
);
31+
$uuid2 = UUID::uuid4();
32+
$this->assertNotEquals(
33+
$uuid1,
34+
$uuid2
35+
);
36+
$uuid1 = $uuid2;
37+
}
3538
}
3639

3740
public function testCanGenerateValidVersion5()
@@ -45,7 +48,11 @@ public function testCanGenerateValidVersion5()
4548
public function testCanGenerateValidVersion6()
4649
{
4750
$uuid1 = UUID::uuid6();
48-
for ($x = 0; $x <= 10; $x++) {
51+
for ($x = 0; $x < 10; $x++) {
52+
$this->assertMatchesRegularExpression(
53+
'/^[0-9a-f]{8}\-[0-9a-f]{4}\-6[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}$/',
54+
$uuid1
55+
);
4956
usleep(1);
5057
$uuid2 = UUID::uuid6();
5158
$this->assertGreaterThan(

0 commit comments

Comments
 (0)