Files
mysql-haversine-benchmark/index.php
Nettika 4c601ee4a4 Test indexed vs non-indexed datasets
Improve benchmark UI

Disable function and php tests (they are very slow)
2025-05-05 13:47:08 -07:00

233 lines
6.0 KiB
PHP

<?php
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
header('HTTP/1.0 401 Unauthorized');
header('WWW-Authenticate: Basic realm="Database access"');
echo 'Unauthorized access. Please provide valid credentials.';
exit;
}
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
$con = mysqli_connect("localhost", $username, $password, "test");
print '<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Haversine Query Benchmarks</title>
<style>
table {
border-collapse: collapse;
thead {
background-color: #ddd;
}
tr.disabled {
background-color: #f2f2f2;
color: #999;
}
th, td {
border: 1px solid black;
padding: 0.8rem 1rem;
text-align: left;
pre {
margin: 0;
}
}
}
</style>
</head>
<body>
<p>Total rows: ' . mysqli_num_rows(mysqli_query($con, "SELECT * FROM floats;")) . '</p>
<p>Selecting rows within 50 miles of Seattle...</p>
<table>
<thead>
<tr>
<th>Test</th>
<th>Time taken</th>
<th>Query</th>
<th>Results</th>
</tr>
</thead>
<tbody>
';
flush();
test(
$con,
'floats : procedure',
"CALL float_coords_within_radius(47.6, -122.33, 50);" // Within 50 miles of Seattle
);
test(
$con,
'floats_indexed : procedure',
"CALL float_indexed_coords_within_radius(47.6, -122.33, 50);" // Within 50 miles of Seattle
);
test(
$con,
'floats : function',
"SELECT * FROM floats WHERE haversine(lat, lon, 47.6, -122.33) < 50;", // Within 50 miles of Seattle
false // disabled
);
test(
$con,
'floats_indexed : function',
"SELECT * FROM floats_indexed WHERE haversine(lat, lon, 47.6, -122.33) < 50;", // Within 50 miles of Seattle
false // disabled
);
test(
$con,
'floats : inlined',
"SELECT * FROM floats WHERE acos(
cos(radians(lat)) *
cos(radians(47.6)) *
cos(radians(lon) - radians(-122.33)) +
sin(radians(lat)) *
sin(radians(47.6))
) * 3959 < 50;" // Within 50 miles of Seattle
);
test(
$con,
'floats_indexed : inlined',
"SELECT * FROM floats_indexed WHERE acos(
cos(radians(lat)) *
cos(radians(47.6)) *
cos(radians(lon) - radians(-122.33)) +
sin(radians(lat)) *
sin(radians(47.6))
) * 3959 < 50;" // Within 50 miles of Seattle
);
{
// $results = [];
// $start = microtime(true);
// $result = mysqli_query($con, "SELECT * FROM floats;");
// while ($row = mysqli_fetch_assoc($result)) {
// if (haversine($row['lat'], $row['lon'], 47.6, -122.33) < 50) { // Within 50 miles of Seattle
// $results[] = $row;
// }
// }
// $end = microtime(true);
// row('floats : php', $end - $start, 'SELECT * FROM floats;', count($results));
row('floats : php', 'disabled', 'SELECT * FROM floats;', 0, true);
}
test(
$con,
'hybrid : procedure',
'CALL hybrid_coords_within_radius(47.6, -122.33, 80467);' // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'hybrid_point : procedure',
'CALL hybrid_coords_within_radius_point(POINT(-122.33, 47.6), 80467);' // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'hybrid_indexed : procedure',
'CALL hybrid_indexed_coords_within_radius(47.6, -122.33, 80467);' // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'hybrid_indexed_point : procedure',
'CALL hybrid_indexed_coords_within_radius_point(POINT(-122.33, 47.6), 80467);' // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'hybrid : inlined',
"SELECT * FROM floats WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), POINT(lon, lat)) < 80467;", // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'hybrid_indexed : inlined',
"SELECT * FROM floats_indexed WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), POINT(lon, lat)) < 80467;" // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'points : procedure',
"CALL point_coords_within_radius(POINT(-122.33, 47.6), 80467);" // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'points_indexed : procedure',
"CALL point_indexed_coords_within_radius(POINT(-122.33, 47.6), 80467);" // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'points : inlined',
"SELECT * FROM points WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), coord) < 80467;" // Within 80,467 meters (50 miles) of Seattle
);
test(
$con,
'points_indexed : inlined',
"SELECT * FROM points_indexed WHERE ST_DISTANCE_SPHERE(POINT(-122.33, 47.6), coord) < 80467;" // Within 80,467 meters (50 miles) of Seattle
);
print '</tbody>
</table>
</body>
</html>
';
function haversine($latFrom, $lonFrom, $latTo, $lonTo)
{
// convert from degrees to radians
$latFrom = deg2rad($latFrom);
$lonFrom = deg2rad($lonFrom);
$latTo = deg2rad($latTo);
$lonTo = deg2rad($lonTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = sin($latDelta / 2) * sin($latDelta / 2) +
cos($latFrom) * cos($latTo) *
sin($lonDelta / 2) * sin($lonDelta / 2);
$chord = 2 * atan2(sqrt($angle), sqrt(1 - $angle));
return 3959 * $chord; // as miles
}
function test($con, $name, $query, $enabled = true)
{
if (!$enabled) {
row($name, 'disabled', $query, 0, true);
return;
}
$start = microtime(true);
$result = mysqli_query($con, $query);
$end = microtime(true);
row($name, $end - $start, $query, mysqli_num_rows($result));
try {
mysqli_next_result($con);
} catch (Exception $e) {}
}
function row($name, $duration, $query, $results, $disabled = false)
{
print '<tr' . ($disabled ? ' class="disabled"' : '') . '>
<td>' . htmlspecialchars($name) . '</td>
<td><time>' . $duration . '</time></td>
<td><pre>' . htmlspecialchars($query) . '</pre></td>
<td>' . $results . '</td>
</tr>
';
flush();
}