LGV_TZ_Lookup 1.4.2
Time zone lookup for PHP applications and HTTP clients
Loading...
Searching...
No Matches
LGV_TZ_Lookup_Test.php
Go to the documentation of this file.
1<?php
2/***************************************************************************************************************************/
3/**
4 © Copyright 2023-2026, [Little Green Viper Software Development LLC](https://littlegreenviper.com)
5
6 LICENSE:
7
8 MIT License
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
11 files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
12 modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
13 Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 [Little Green Viper Software Development LLC](https://littlegreenviper.com)
23*/
24/***************************************************************************************************************************/
25/**
26 \brief This file has some basic query tests for the server.
27 */
28
29declare(strict_types = 1);
30
31/***************************************************************************************************************************/
32/** \brief Summarize request lookup timings and the PHP request's peak memory. */
33function test_server_performance(array $nanoseconds, int|float $elapsed, int $peakMemory): string {
34 sort($nanoseconds, SORT_NUMERIC);
35 $samples = count($nanoseconds);
36 if ($samples === 0) { return ''; }
37 $middle = intdiv($samples, 2);
38 $median = $samples % 2 ? $nanoseconds[$middle] : ($nanoseconds[$middle - 1] + $nanoseconds[$middle]) / 2;
39 $p95 = $nanoseconds[(int)ceil($samples * 0.95) - 1];
40 return '<section><h2>Performance</h2><ul>'.
41 '<li>Lookups measured: '.$samples.'</li>'.
42 sprintf('<li>Total test time: %.3f s</li>', $elapsed / 1e9).
43 sprintf('<li>Average lookup: %.3f ms</li>', array_sum($nanoseconds) / $samples / 1e6).
44 sprintf('<li>Median lookup: %.3f ms</li>', $median / 1e6).
45 sprintf('<li>95th-percentile lookup: %.3f ms</li>', $p95 / 1e6).
46 sprintf('<li>Slowest lookup: %.3f ms</li>', $nanoseconds[$samples - 1] / 1e6).
47 sprintf('<li>PHP request peak memory: %.2f MiB</li>', $peakMemory / 1048576).
48 '</ul><p>Lookup timings include configuration loading, authentication, the database connection, and the query. '.
49 'They exclude HTTP transport and browser rendering. Peak memory includes this PHP request and its test results; '.
50 'database server memory is outside this measurement.</p></section>';
51}
52
53/***************************************************************************************************************************/
54/**
55This is a basic tester. It runs a list of long/lat pairs through the server, and compares the results, with the expected ones.
56
57\returns: HTML of the results.
58 */
59function test_server() {
60 $started = hrtime(true);
61 /***********************************************************************************************************************/
62 /**
63 This is a simple generator for query strings, based on the given long/lat.
64
65 \returns: the query string.
66 */
67 function _testllGen($inLng, ///< The longitude to use
68 $inLat ///< The latitude to use
69 ) {
70 include __CONFIG_FILE_;
71
72 $queryString = isset($g_server_secret) ? "secret=$g_server_secret" : "";
73
74 if (isset($inLng) && isset($inLat)) {
75 if (!empty($queryString)) {
76 $queryString .= '&';
77 }
78 $queryString .= "ll=$inLng,$inLat";
79 }
80
81 return $queryString;
82 }
83
84 /***********************************************************************************************************************/
85 /**
86 This runs the test, and returns the HTML for the results.
87
88 \returns: the test result, as HTML.
89 */
90 function _callTestServer( $inTitle, ///< The title to display
91 $inLng, ///< The longitude to use
92 $inLat, ///< The latitude to use
93 $inResult, ///< The expected result
94 array &$timings ///< Lookup duration samples in nanoseconds.
95 ) {
96 global $count;
97 global $failures;
98 $count++;
99 $queryString = _testllGen($inLng, $inLat);
100 $lookupStarted = hrtime(true);
101 $result = call_server($queryString, false);
102 $duration = hrtime(true) - $lookupStarted;
103 $timings[] = $duration;
104 $style = $result == $inResult ? "pass" : "fail";
105 $failAddendum = "";
106 if ($result != $inResult) {
107 $failure = ['title' => $inTitle, 'id' => "test-$count"];
108 $failures[] = $failure;
109 $failAddendum = ' <em class="fail">(Expected &quot;'.htmlspecialchars($inResult).'&quot;)</em>';
110 }
111 $ret = "<strong class=\"$style\" id=\"test-$count\">$inTitle</strong>";
112 $ret .= "<ul><li>Longitude: $inLng</li><li>Latitude: $inLat</li>";
113 $ret .= "<li>Result: &quot;$result&quot;$failAddendum</li>";
114 $ret .= sprintf('<li>Lookup time: %.3f ms</li></ul>', $duration / 1e6);
115
116 return $ret;
117 }
118
119 global $failures;
120 global $count;
121 $ret = '';
122 $count = 0;
123 $failures = [];
124 $timings = [];
125
126 include __DIR__.'/TestLocations.php'; // This establishes the $test_locations_param_array
127
128 foreach ($test_locations_param_array as $test) {
129 $orig_longitude = isset($test['params']['lng']) ? $test['params']['lng'] : NULL;
130 $orig_latitude = isset($test['params']['lat']) ? $test['params']['lat'] : NULL;
131
132 $ret .= _callTestServer($test['title'], $orig_longitude, $orig_latitude, $test['result'], $timings);
133 }
134
135 if (!empty($failures)) {
136 $failureText = '<h2 class="fail">'.count($failures).' Test Failures!</h2><ul>';
137 foreach ($failures as $failure) {
138 $failureText .= '<li><a href="#'.$failure['id'].'">'.htmlspecialchars($failure['title']).'</a></li>';
139 }
140 $failureText .= "</ul>";
141
142 $ret = $failureText.$ret;
143 } else {
144 $ret = '<h2 class="pass">All Tests ('.$count.') Passed!</h2>'.$ret;
145 }
146
147 return $ret.test_server_performance($timings, hrtime(true) - $started, memory_get_peak_usage(true));
148};
test_server()
test_server_performance(array $nanoseconds, int|float $elapsed, int $peakMemory)
Summarize request lookup timings and the PHP request's peak memory.
$test_locations_param_array
This file has a list of long/lat pairs, along with the expected results, when presented to the server...
$failures
Definition benchmark.php:79
const __CONFIG_FILE_
Definition server.php:45
call_server( $inQuery, $inIsCLI)
Definition index.php:71
$queryString
Definition index.php:61