-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_hls_benchmark.py
280 lines (238 loc) · 7.96 KB
/
test_hls_benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""Benchmarks for HLS tile requests"""
import asyncio
from datetime import datetime, timedelta
from typing import Any, Dict, List, NamedTuple, Tuple
import httpx
import morecantile
import pytest
ENDPOINT = "https://dev-titiler-cmr.delta-backend.com"
TMS = morecantile.tms.get("WebMercatorQuad")
TEST_LNG, TEST_LAT = -92.1, 46.8
TILES_HEIGHT = 5
TILES_WIDTH = 5
class CollectionConfig(NamedTuple):
"""Configuration for a collection"""
collection_id: str
concept_id: str
base_date: datetime
# band configurations
rgb_bands: List[str]
ndvi_bands: Tuple[str, str] # (red_band, nir_band)
single_band: str
LANDSAT = CollectionConfig(
collection_id="HLSL30",
concept_id="C2021957657-LPCLOUD",
base_date=datetime(2023, 2, 24, 0, 0, 1),
rgb_bands=["B04", "B03", "B02"],
ndvi_bands=("B04", "B05"),
single_band="B04",
)
SENTINEL = CollectionConfig(
collection_id="HLSS30",
concept_id="C2021957295-LPCLOUD",
base_date=datetime(2023, 2, 13, 0, 0, 1),
rgb_bands=["B04", "B03", "B02"],
ndvi_bands=("B04", "B8A"),
single_band="B04",
)
COLLECTIONS = {
"HLSL30": LANDSAT,
"HLSS30": SENTINEL,
}
# test parameters
ZOOM_LEVELS = [6, 7, 8, 9, 10]
INTERVAL_DAYS = [1, 7]
N_BANDS = [1, 2, 3]
def get_surrounding_tiles(
x: int, y: int, zoom: int, width: int = TILES_WIDTH, height: int = TILES_HEIGHT
) -> List[Tuple[int, int]]:
"""Get a list of surrounding tiles for a viewport"""
tiles = []
offset_x = width // 2
offset_y = height // 2
for y_pos in range(y - offset_y, y + offset_y + 1):
for x_pos in range(x - offset_x, x + offset_x + 1):
# Ensure x, y are valid for the zoom level
max_tile = 2**zoom - 1
x_valid = max(0, min(x_pos, max_tile))
y_valid = max(0, min(y_pos, max_tile))
tiles.append((x_valid, y_valid))
return tiles
def get_band_params(
collection_config: CollectionConfig, n_bands: int
) -> Dict[str, Any]:
"""Get band-specific parameters based on collection and band count"""
params: Dict[str, Any] = {
"backend": "rasterio",
"bands_regex": "B[0-9][0-9]",
}
if n_bands == 3:
# RGB visualization
params["color_formula"] = "Gamma RGB 3.5 Saturation 1.7 Sigmoidal RGB 15 0.35"
params["bands"] = collection_config.rgb_bands
elif n_bands == 2:
# NDVI visualization
red_band, nir_band = collection_config.ndvi_bands
params["bands"] = [red_band, nir_band]
params["expression"] = f"({nir_band}-{red_band})/({nir_band}+{red_band})"
params["colormap_name"] = "greens"
params["rescale"] = "-1,1"
elif n_bands == 1:
# Single band visualization
params["bands"] = [collection_config.single_band]
params["colormap_name"] = "viridis"
params["rescale"] = "0,5000"
return params
async def fetch_tile(
client: httpx.AsyncClient,
endpoint: str,
z: int,
x: int,
y: int,
collection_config: CollectionConfig,
interval_days: int,
n_bands: int,
) -> httpx.Response:
"""Fetch a single HLS tile"""
url = f"{endpoint}/tiles/WebMercatorQuad/{z}/{x}/{y}.png"
start_date = collection_config.base_date
end_date = start_date + timedelta(days=interval_days)
datetime_range = f"{start_date.isoformat()}/{end_date.isoformat()}"
params: Dict[str, Any] = {
"concept_id": collection_config.concept_id,
"datetime": datetime_range,
}
params.update(get_band_params(collection_config, n_bands))
start_time = datetime.now()
try:
response = await client.get(url, params=params, timeout=30.0)
response.raise_for_status()
elapsed = (datetime.now() - start_time).total_seconds()
response.elapsed = timedelta(seconds=elapsed)
return response
except Exception:
# Create a mock response for exceptions
mock_response = httpx.Response(500, request=httpx.Request("GET", url))
mock_response.elapsed = datetime.now() - start_time
return mock_response
async def fetch_viewport_tiles(
endpoint: str,
collection_config: CollectionConfig,
zoom: int,
lng: float,
lat: float,
interval_days: int,
n_bands: int,
) -> List[Dict]:
"""Fetch all tiles for a viewport and return detailed metrics"""
tile = TMS.tile(lng=lng, lat=lat, zoom=zoom)
tiles = get_surrounding_tiles(tile.x, tile.y, zoom)
results = []
async with httpx.AsyncClient() as client:
tasks = [
fetch_tile(
client,
endpoint,
zoom,
x,
y,
collection_config,
interval_days,
n_bands,
)
for x, y in tiles
]
responses = await asyncio.gather(*tasks)
for (x, y), response in zip(tiles, responses):
# Capture detailed metrics for each tile
results.append(
{
"x": x,
"y": y,
"status_code": response.status_code,
"response_time": response.elapsed.total_seconds(),
"response_size": len(response.content)
if hasattr(response, "content")
else 0,
"has_data": response.status_code == 200, # 204 means no data
"is_error": response.status_code >= 400,
}
)
return results
@pytest.fixture(scope="session", autouse=True)
def warm_up_api():
"""Perform a single warmup request to the API before all tests."""
asyncio.run(
fetch_viewport_tiles(
endpoint=ENDPOINT,
collection_config=LANDSAT,
zoom=8,
lng=TEST_LNG,
lat=TEST_LAT,
interval_days=1,
n_bands=3,
)
)
@pytest.mark.benchmark(
group="hls-tiles",
min_rounds=2,
warmup=False,
)
@pytest.mark.parametrize("collection_id", list(COLLECTIONS.keys()))
@pytest.mark.parametrize("zoom", ZOOM_LEVELS)
@pytest.mark.parametrize("interval_days", INTERVAL_DAYS)
@pytest.mark.parametrize("n_bands", N_BANDS)
def test_hls_tiles(
benchmark,
collection_id: str,
zoom: int,
interval_days: int,
n_bands: int,
):
"""Test HLS tile performance with various parameters"""
collection_config = COLLECTIONS[collection_id]
def tile_benchmark():
# Run the async function in a synchronous context
results = asyncio.run(
fetch_viewport_tiles(
endpoint=ENDPOINT,
collection_config=collection_config,
zoom=zoom,
lng=TEST_LNG,
lat=TEST_LAT,
interval_days=interval_days,
n_bands=n_bands,
)
)
return results
# Run the benchmark
results = benchmark(tile_benchmark)
# Calculate summary statistics
total_tiles = len(results)
success_count = sum(1 for r in results if r["has_data"])
no_data_count = sum(1 for r in results if r["status_code"] == 204)
error_count = sum(1 for r in results if r["is_error"])
avg_response_time = (
sum(r["response_time"] for r in results) / total_tiles if total_tiles else 0
)
avg_response_size = (
sum(r["response_size"] for r in results if r["has_data"]) / success_count
if success_count
else 0
)
# Add detailed metrics to the benchmark results
benchmark.extra_info.update(
{
"collection": collection_id,
"zoom": zoom,
"interval_days": interval_days,
"band_count": n_bands,
"total_tiles": total_tiles,
"success_count": success_count,
"no_data_count": no_data_count,
"error_count": error_count,
"success_rate": success_count / total_tiles if total_tiles else 0,
"avg_response_time": avg_response_time,
"avg_response_size": avg_response_size,
}
)