Skip to content

Commit 201914d

Browse files
Issue #8: tests for Uuid module
1 parent 429b7fc commit 201914d

File tree

6 files changed

+235
-8
lines changed

6 files changed

+235
-8
lines changed

README.en.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Required modules for full php testing:
2323
- zstd
2424
- igbinary
2525
- msgpack
26+
- uuid
2627

2728
Usually they are already installed or "compiled" in php.
2829

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- snappy
2424
- igbinary
2525
- msgpack
26+
- uuid
2627

2728
Обычно они уже установлены или "вкомпилированны" в php.
2829

UUID.php

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* UUID class
4+
*
5+
* The following class generates VALID RFC 4211 COMPLIANT
6+
* Universally Unique IDentifiers (UUID) version 3, 4 and 5.
7+
*
8+
* UUIDs generated validates using OSSP UUID Tool, and output
9+
* for named-based UUIDs are exactly the same. This is a pure
10+
* PHP implementation.
11+
*
12+
* @author Andrew Moore
13+
* @link http://www.php.net/manual/en/function.uniqid.php#94959
14+
*/
15+
class UUID
16+
{
17+
/**
18+
* Generate v3 UUID
19+
*
20+
* Version 3 UUIDs are named based. They require a namespace (another
21+
* valid UUID) and a value (the name). Given the same namespace and
22+
* name, the output is always the same.
23+
*
24+
* @param uuid $namespace
25+
* @param string $name
26+
*/
27+
public static function v3($namespace, $name)
28+
{
29+
if(!self::is_valid($namespace)) return false;
30+
31+
// Get hexadecimal components of namespace
32+
$nhex = str_replace(array('-','{','}'), '', $namespace);
33+
34+
// Binary Value
35+
$nstr = '';
36+
37+
// Convert Namespace UUID to bits
38+
for($i = 0; $i < strlen($nhex); $i+=2)
39+
{
40+
$nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
41+
}
42+
43+
// Calculate hash value
44+
$hash = md5($nstr . $name);
45+
46+
return sprintf('%08s-%04s-%04x-%04x-%12s',
47+
48+
// 32 bits for "time_low"
49+
substr($hash, 0, 8),
50+
51+
// 16 bits for "time_mid"
52+
substr($hash, 8, 4),
53+
54+
// 16 bits for "time_hi_and_version",
55+
// four most significant bits holds version number 3
56+
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,
57+
58+
// 16 bits, 8 bits for "clk_seq_hi_res",
59+
// 8 bits for "clk_seq_low",
60+
// two most significant bits holds zero and one for variant DCE1.1
61+
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
62+
63+
// 48 bits for "node"
64+
substr($hash, 20, 12)
65+
);
66+
}
67+
68+
/**
69+
*
70+
* Generate v4 UUID
71+
*
72+
* Version 4 UUIDs are pseudo-random.
73+
*/
74+
public static function v4()
75+
{
76+
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
77+
78+
// 32 bits for "time_low"
79+
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
80+
81+
// 16 bits for "time_mid"
82+
mt_rand(0, 0xffff),
83+
84+
// 16 bits for "time_hi_and_version",
85+
// four most significant bits holds version number 4
86+
mt_rand(0, 0x0fff) | 0x4000,
87+
88+
// 16 bits, 8 bits for "clk_seq_hi_res",
89+
// 8 bits for "clk_seq_low",
90+
// two most significant bits holds zero and one for variant DCE1.1
91+
mt_rand(0, 0x3fff) | 0x8000,
92+
93+
// 48 bits for "node"
94+
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
95+
);
96+
}
97+
98+
/**
99+
* Generate v5 UUID
100+
*
101+
* Version 5 UUIDs are named based. They require a namespace (another
102+
* valid UUID) and a value (the name). Given the same namespace and
103+
* name, the output is always the same.
104+
*
105+
* @param uuid $namespace
106+
* @param string $name
107+
*/
108+
public static function v5($namespace, $name)
109+
{
110+
if(!self::is_valid($namespace)) return false;
111+
112+
// Get hexadecimal components of namespace
113+
$nhex = str_replace(array('-','{','}'), '', $namespace);
114+
115+
// Binary Value
116+
$nstr = '';
117+
118+
// Convert Namespace UUID to bits
119+
for($i = 0; $i < strlen($nhex); $i+=2)
120+
{
121+
$nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
122+
}
123+
124+
// Calculate hash value
125+
$hash = sha1($nstr . $name);
126+
127+
return sprintf('%08s-%04s-%04x-%04x-%12s',
128+
129+
// 32 bits for "time_low"
130+
substr($hash, 0, 8),
131+
132+
// 16 bits for "time_mid"
133+
substr($hash, 8, 4),
134+
135+
// 16 bits for "time_hi_and_version",
136+
// four most significant bits holds version number 5
137+
(hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,
138+
139+
// 16 bits, 8 bits for "clk_seq_hi_res",
140+
// 8 bits for "clk_seq_low",
141+
// two most significant bits holds zero and one for variant DCE1.1
142+
(hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,
143+
144+
// 48 bits for "node"
145+
substr($hash, 20, 12)
146+
);
147+
}
148+
149+
public static function is_valid($uuid) {
150+
return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
151+
'[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
152+
}
153+
}
154+
?>

bench.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
# Company : Code24 BV, The Netherlands #
1010
# Author : Sergey Dryabzhinsky #
1111
# Company : Rusoft Ltd, Russia #
12-
# Date : Mar 22, 2025 #
13-
# Version : 1.0.58-dev #
12+
# Date : May 29, 2025 #
13+
# Version : 1.0.59-dev #
1414
# License : Creative Commons CC-BY license #
1515
# Website : https://github.com/rusoft/php-simple-benchmark-script #
1616
# Website : https://gitea.rusoft.ru/open-source/php-simple-benchmark-script #
@@ -20,7 +20,7 @@
2020

2121
include_once("php-options.php");
2222

23-
$scriptVersion = '1.0.58-dev';
23+
$scriptVersion = '1.0.59-dev';
2424

2525
// Special string to flush buffers, nginx for example
2626
$flushStr = '<!-- '.str_repeat(" ", 8192).' -->';
@@ -102,6 +102,12 @@
102102
if (extension_loaded('intl')) {
103103
@include_once("intl.inc");
104104
}
105+
if (file_exists('UUID.php') && PHP_VERSION >= '5.0.0') {
106+
@include_once("php-uuid.inc");
107+
}
108+
if (extension_loaded('uuid')) {
109+
@include_once("mod-uuid.inc");
110+
}
105111
if (extension_loaded('gd')) {
106112
@include_once("php-gd-imagick-common.inc");
107113
@include_once("php-gd.inc");
@@ -272,8 +278,9 @@ function print_norm($msg) {
272278
if (!function_exists('gethostname')) {
273279
// 5.3.0+ only
274280
function gethostname() {
275-
on_start();
281+
ob_start();
276282
$last_str = system(`hostname -f`, $errcode);
283+
ob_end_clean();
277284
if ($last_str !== false) {
278285
return $last_str;
279286
}
@@ -406,7 +413,7 @@ function gethostname() {
406413
. PHP_EOL
407414
. ' -h|--help - print this help and exit' . PHP_EOL
408415
. ' -x|--debug - enable debug mode, raise output level' . PHP_EOL
409-
. ' -C|--dont-use-colors - disable printing html-span or color sequences for capable terminal: xterm, *-color, *-256color. And not in JSON/machine mode.' . PHP_EOL
416+
. ' -C|--dont-use-colors - disable printing html-span or color sequences for capable terminal: xterm, *-color, *-256color. And not use it in JSON/machine mode.' . PHP_EOL
410417
. ' -J|--print-json - enable printing only in JSON format, useful for automated tests. disables print-machine.' . PHP_EOL
411418
. ' -M|--print-machine - enable printing only in machine parsable format, useful for automated tests. disables print-json.' . PHP_EOL
412419
. ' -d|--dont-recalc - do not recalculate test times / operations count even if memory of execution time limits are low' . PHP_EOL
@@ -429,7 +436,7 @@ function gethostname() {
429436
. PHP_EOL
430437
. ' -h - print this help and exit' . PHP_EOL
431438
. ' -x - enable debug mode, raise output level' . PHP_EOL
432-
. ' -C - disable printing html-span or color sequences for capable terminal: xterm, *-color, *-256color. And not in JSON/machine mode.' . PHP_EOL
439+
. ' -C - disable printing html-span or color sequences for capable terminal: xterm, *-color, *-256color. And not use it in JSON/machine mode.' . PHP_EOL
433440
. ' -J - enable printing only in JSON format, useful for automated tests. disables print-machine.' . PHP_EOL
434441
. ' -M - enable printing only in machine parsable format, useful for automated tests. disables print-json.' . PHP_EOL
435442
. ' -d - do not recalculate test times / operations count even if memory of execution time limits are low' . PHP_EOL
@@ -742,6 +749,8 @@ function gethostname() {
742749
'36_brotli_compress' => 1000000,
743750
'37_01_php8_str_ccontains' => 100000,
744751
'37_02_php8_str_ccontains_simulate' => 100000,
752+
'38_01_php_uuid' => 1000000,
753+
'38_02_mod_uuid' => 1000000,
745754
);
746755
// Should not be more than X Mb
747756
// Different PHP could use different amount of memory
@@ -798,6 +807,8 @@ function gethostname() {
798807
'36_brotli_compress' => 4,
799808
'37_01_php8_str_ccontains' => 4,
800809
'37_02_php8_str_ccontains_simulate' => 4,
810+
'38_01_php_uuid' => 4,
811+
'38_02_mod_uuid' => 4,
801812
);
802813

803814
/** ---------------------------------- Common functions -------------------------------------------- */
@@ -1657,6 +1668,11 @@ function format_result_test($diffSeconds, $opCount, $memory = 0)
16571668
$has_brotli = "{$colorGreen}yes{$colorReset}";
16581669
}
16591670

1671+
$has_uuid = "{$colorYellow}no{$colorReset}";
1672+
if (extension_loaded('uuid')) {
1673+
$has_uuid = "{$colorGreen}yes{$colorReset}";
1674+
}
1675+
16601676
$has_jsond = "{$colorYellow}no{$colorReset}";
16611677
$has_jsond_as_json = "{$colorYellow}no{$colorReset}";
16621678
if ($jsond = extension_loaded('jsond')) {
@@ -1679,7 +1695,7 @@ function print_results_common()
16791695
global $line, $padHeader, $cpuInfo, $padInfo, $scriptVersion, $maxTime, $originTimeLimit, $originMemoryLimit, $cryptAlgoName, $memoryLimitMb;
16801696
global $flushStr, $has_apc, $has_pcre, $has_intl, $has_json, $has_simplexml, $has_dom, $has_mbstring, $has_opcache, $has_xcache;
16811697
global $has_gd, $has_imagick, $has_igb, $has_msg, $has_jsond, $has_jsond_as_json;
1682-
global $has_zlib, $has_gzip, $has_bz2, $has_lz4, $has_snappy, $has_zstd, $has_brotli;
1698+
global $has_zlib, $has_uuid, $has_gzip, $has_bz2, $has_lz4, $has_snappy, $has_zstd, $has_brotli;
16831699
global $opcache, $has_eacc, $has_xdebug, $xcache, $apcache, $eaccel, $xdebug, $xdbg_mode, $obd_set, $mbover;
16841700
global $showOnlySystemInfo, $padLabel, $functions, $runOnlySelectedTests, $selectedTests, $totalOps;
16851701
global $colorGreen, $colorReset, $colorRed;
@@ -1726,9 +1742,10 @@ function print_results_common()
17261742
. str_pad("gzip", $padInfo, ' ', STR_PAD_LEFT) . " : $has_gzip\n"
17271743
. str_pad("bz2", $padInfo, ' ', STR_PAD_LEFT) . " : $has_bz2\n"
17281744
. str_pad("lz4", $padInfo, ' ', STR_PAD_LEFT) . " : $has_lz4\n"
1729-
. str_pad("snappy", $padInfo, ' ', STR_PAD_LEFT) . " : $has_snappy\n"
1745+
. str_pad("snappy", $padInfo, ' ', STR_PAD_LEFT) . " : $has_snappy\n"
17301746
. str_pad("zstd", $padInfo, ' ', STR_PAD_LEFT) . " : $has_zstd, version:".LIBZSTD_VERSION_STRING."\n"
17311747
. str_pad("brotli", $padInfo, ' ', STR_PAD_LEFT) . " : $has_brotli\n"
1748+
. str_pad("uuid", $padInfo, ' ', STR_PAD_LEFT) . " : $has_uuid\n"
17321749
. str_pad("-affecting->", $padInfo, ' ', STR_PAD_LEFT) . "\n"
17331750
. str_pad("opcache", $padInfo, ' ', STR_PAD_LEFT) . " : $has_opcache; enabled: {$opcache}\n"
17341751
. str_pad("xcache", $padInfo, ' ', STR_PAD_LEFT) . " : $has_xcache; enabled: {$xcache}\n"

mod-uuid.inc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* module Uuuid test functions
4+
* Php 5.2+
5+
*/
6+
7+
/** ---------------------------------- Tests functions -------------------------------------------- */
8+
9+
function test_38_02_mod_uuid()
10+
{
11+
global $stringTest, $emptyResult, $testsLoopLimits, $totalOps;
12+
13+
// print_r(get_defined_functions());
14+
if (!function_exists('uuid_create')) {
15+
print("Function don't exists!\n");
16+
return $emptyResult;
17+
}
18+
19+
$count = $testsLoopLimits['38_02_mod_uuid'];
20+
$time_start = get_microtime();
21+
for ($i = 0; $i < $count; $i++) {
22+
$uuid = uuid_create();
23+
}
24+
$totalOps += $count;
25+
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
26+
}

php-uuid.inc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* php UUID test functions
4+
* Php 5.2+
5+
*/
6+
7+
/** ---------------------------------- Tests functions -------------------------------------------- */
8+
9+
include_once("UUID.php");
10+
function test_38_01_php_uuid()
11+
{
12+
global $stringTest, $emptyResult, $testsLoopLimits, $totalOps;
13+
14+
// print_r(get_declared_classes());
15+
if (!class_exists('UUID',false)) {
16+
print("Class don't exists!\n");
17+
return $emptyResult;
18+
}
19+
20+
$count = $testsLoopLimits['38_01_php_uuid'];
21+
// print("Count runs: $count");
22+
$time_start = get_microtime();
23+
for ($i = 0; $i < $count; $i++) {
24+
$uuid = UUID::v4();
25+
}
26+
$totalOps += $count;
27+
return format_result_test(get_microtime() - $time_start, $count, mymemory_usage());
28+
}

0 commit comments

Comments
 (0)