Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.maps.android.clustering.algo

import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.clustering.Cluster
import com.google.maps.android.clustering.ClusterItem
import org.junit.Test
import java.util.Random

class BenchmarkTest {

private class MyItem(lat: Double, lng: Double) : ClusterItem {
override val position: LatLng = LatLng(lat, lng)
override val title: String? = null
override val snippet: String? = null
override val zIndex: Float? = null
}

private fun generateItems(count: Int): List<MyItem> {
val random = Random(12345) // Seed for consistency
return List(count) {
val lat = (random.nextDouble() - 0.5) * 170 // -85 to 85
val lng = (random.nextDouble() - 0.5) * 360 // -180 to 180
MyItem(lat, lng)
}
}

@Test
fun benchmarkGridBasedAlgorithm() {
runBenchmark(GridBasedAlgorithm(), "GridBasedAlgorithm")
}

@Test
fun benchmarkNonHierarchicalDistanceBasedAlgorithm() {
runBenchmark(NonHierarchicalDistanceBasedAlgorithm(), "NonHierarchicalDistanceBasedAlgorithm")
}

@Test
fun benchmarkCentroidNonHierarchicalDistanceBasedAlgorithm() {
runBenchmark(CentroidNonHierarchicalDistanceBasedAlgorithm(), "CentroidNonHierarchicalDistanceBasedAlgorithm")
}

@Test
fun benchmarkContinuousZoomEuclideanCentroidAlgorithm() {
runBenchmark(ContinuousZoomEuclideanCentroidAlgorithm(), "ContinuousZoomEuclideanCentroidAlgorithm")
}

@Test
fun benchmarkPreCachingAlgorithmDecorator() {
runBenchmark(PreCachingAlgorithmDecorator(NonHierarchicalDistanceBasedAlgorithm()), "PreCachingAlgorithmDecorator")
}

private fun runBenchmark(algorithm: Algorithm<MyItem>, name: String) {
println("--- Benchmarking $name ---")
val count = 50000
val items = generateItems(count)

// Warmup
algorithm.addItems(items.take(1000))
algorithm.getClusters(10f)
algorithm.clearItems()

System.gc()

// 1. Benchmark Adding Items
val startAdd = System.nanoTime()
algorithm.addItems(items)
val endAdd = System.nanoTime()
System.out.printf("addItems(%,d) took %.2f ms%n", count, (endAdd - startAdd) / 1000000.0)

// 2. Benchmark getClusters at various zoom levels
val zoomLevels = floatArrayOf(4f, 8f, 12f, 16f)
for (zoom in zoomLevels) {
System.gc()
val startCluster = System.nanoTime()
val clusters = algorithm.getClusters(zoom)
val endCluster = System.nanoTime()
System.out.printf("getClusters(zoom=%.1f) created %,d clusters in %.2f ms%n",
zoom, clusters.size, (endCluster - startCluster) / 1000000.0)
}
}
}
95 changes: 95 additions & 0 deletions clustering_benchmark_dashboard.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Clustering Benchmark Dashboard</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; margin: 0; padding: 20px; background-color: #f6f8fa; color: #24292f; }
.container { max-width: 1200px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.12); }
h1, h2 { text-align: center; color: #0969da; }
.charts { display: flex; flex-wrap: wrap; gap: 20px; justify-content: center; margin-top: 30px; }
.chart-container { flex: 1 1 45%; min-width: 400px; padding: 15px; border: 1px solid #d0d7de; border-radius: 6px; }
canvas { width: 100% !important; height: 300px !important; }
.footer { text-align: center; margin-top: 40px; font-size: 0.9em; color: #57606a; }
</style>
</head>
<body>
<div class="container">
<h1>Android Maps Utils Clustering Benchmarks</h1>
<p style="text-align: center;">Performance comparison between the original Java implementation (<code>main</code>) and the Kotlin rewrite (<code>feat/rewrite-android-maps-utils</code>).</p>

<div class="charts">
<!-- GridBasedAlgorithm Chart -->
<div class="chart-container">
<h2>GridBasedAlgorithm</h2>
<canvas id="gridChart"></canvas>
</div>

<!-- NonHierarchicalDistanceBasedAlgorithm Chart -->
<div class="chart-container">
<h2>NonHierarchicalDistanceBasedAlgorithm</h2>
<canvas id="distanceChart"></canvas>
</div>

<!-- CentroidNonHierarchicalDistanceBasedAlgorithm Chart -->
<div class="chart-container">
<h2>CentroidNonHierarchicalDistanceBased</h2>
<canvas id="centroidChart"></canvas>
</div>

<!-- ContinuousZoomEuclideanCentroidAlgorithm Chart -->
<div class="chart-container">
<h2>ContinuousZoomEuclideanCentroid</h2>
<canvas id="continuousChart"></canvas>
</div>

<!-- PreCachingAlgorithmDecorator Chart -->
<div class="chart-container">
<h2>PreCachingAlgorithmDecorator</h2>
<canvas id="preCachingChart"></canvas>
</div>
</div>
<div class="footer">
Note: Measurements are in milliseconds (ms) averaged across 10 iterations. Lower is better. Tests conducted with 50,000 random items.
</div>
</div>

<script>
const chartOptions = {
responsive: true,
maintainAspectRatio: false,
scales: { y: { beginAtZero: true, title: { display: true, text: 'Time (ms)' } } },
plugins: { legend: { position: 'top' } }
};

const labels = ['addItems', 'zoom 4.0', 'zoom 8.0', 'zoom 12.0', 'zoom 16.0'];

const colors = {
java: { bg: 'rgba(255, 99, 132, 0.7)', border: 'rgb(255, 99, 132)' },
kotlin: { bg: 'rgba(54, 162, 235, 0.7)', border: 'rgb(54, 162, 235)' }
};

function createChart(ctxId, dataJava, dataKotlin) {
new Chart(document.getElementById(ctxId).getContext('2d'), {
type: 'bar',
data: {
labels: labels,
datasets: [
{ label: 'Java (main)', data: dataJava, backgroundColor: colors.java.bg, borderColor: colors.java.border, borderWidth: 1 },
{ label: 'Kotlin (rewrite)', data: dataKotlin, backgroundColor: colors.kotlin.bg, borderColor: colors.kotlin.border, borderWidth: 1 }
]
},
options: chartOptions
});
}

createChart('gridChart', [6.28, 29.93, 206.30, 269.87, 228.93], [6.37, 29.77, 189.84, 250.74, 221.97]);
createChart('distanceChart', [22.60, 50.19, 70.89, 63.84, 62.89], [20.62, 48.33, 62.50, 55.42, 53.83]);
createChart('centroidChart', [32.28, 81.76, 133.18, 82.79, 90.66], [43.37, 77.41, 128.38, 105.66, 72.02]);
createChart('continuousChart', [21.99, 82.72, 105.88, 92.08, 100.84], [20.78, 76.80, 96.01, 89.77, 87.47]);
createChart('preCachingChart', [48.36, 84.21, 74.76, 75.32, 62.51], [47.58, 118.35, 72.57, 66.87, 69.80]);
</script>
</body>
</html>
Loading