-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathtest_memory_benchmark.py
More file actions
289 lines (232 loc) · 11.2 KB
/
test_memory_benchmark.py
File metadata and controls
289 lines (232 loc) · 11.2 KB
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
281
282
283
284
285
286
287
288
289
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
"""Memory benchmarks for manifest cache efficiency.
These benchmarks reproduce the manifest cache memory issue described in:
https://github.com/apache/iceberg-python/issues/2325
The issue: When caching manifest lists as tuples, overlapping ManifestFile objects
are duplicated across cache entries, causing O(N²) memory growth instead of O(N).
Run with: uv run pytest tests/benchmark/test_memory_benchmark.py -v -s -m benchmark
"""
import gc
import tracemalloc
from datetime import datetime, timezone
import pyarrow as pa
import pytest
from pyiceberg import manifest as manifest_module
from pyiceberg.catalog.memory import InMemoryCatalog
from pyiceberg.manifest import clear_manifest_cache
def generate_test_dataframe() -> pa.Table:
"""Generate a PyArrow table for testing, similar to the issue's example."""
n_rows = 100 # Smaller for faster tests, increase for more realistic benchmarks
return pa.table(
{
"event_type": ["playback"] * n_rows,
"event_origin": ["origin1"] * n_rows,
"event_send_at": [datetime.now(timezone.utc)] * n_rows,
"event_saved_at": [datetime.now(timezone.utc)] * n_rows,
"id": list(range(n_rows)),
"reference_id": [f"ref-{i}" for i in range(n_rows)],
}
)
@pytest.fixture
def memory_catalog(tmp_path_factory: pytest.TempPathFactory) -> InMemoryCatalog:
"""Create an in-memory catalog for memory testing."""
warehouse_path = str(tmp_path_factory.mktemp("warehouse"))
catalog = InMemoryCatalog("memory_test", warehouse=f"file://{warehouse_path}")
catalog.create_namespace("default")
return catalog
@pytest.fixture(autouse=True)
def clear_caches() -> None:
"""Clear caches before each test."""
clear_manifest_cache()
gc.collect()
@pytest.mark.benchmark
def test_manifest_cache_memory_growth(memory_catalog: InMemoryCatalog) -> None:
"""Benchmark memory growth of manifest cache during repeated appends.
This test reproduces the issue from GitHub #2325 where each append creates
a new manifest list entry in the cache, causing memory to grow.
With the old caching strategy (tuple per manifest list), memory grew as O(N²).
With the new strategy (individual ManifestFile objects), memory grows as O(N).
"""
df = generate_test_dataframe()
table = memory_catalog.create_table("default.memory_test", schema=df.schema)
tracemalloc.start()
num_iterations = 50
memory_samples: list[tuple[int, int, int]] = [] # (iteration, current_memory, cache_size)
print("\n--- Manifest Cache Memory Growth Benchmark ---")
print(f"Running {num_iterations} append operations...")
for i in range(num_iterations):
table.append(df)
# Sample memory at intervals
if (i + 1) % 10 == 0:
current, _ = tracemalloc.get_traced_memory()
cache_size = len(manifest_module._manifest_cache)
memory_samples.append((i + 1, current, cache_size))
print(f" Iteration {i + 1}: Memory={current / 1024:.1f} KB, Cache entries={cache_size}")
tracemalloc.stop()
# Analyze memory growth
if len(memory_samples) >= 2:
first_memory = memory_samples[0][1]
last_memory = memory_samples[-1][1]
memory_growth = last_memory - first_memory
growth_per_iteration = memory_growth / (memory_samples[-1][0] - memory_samples[0][0])
print("\nResults:")
print(f" Initial memory: {first_memory / 1024:.1f} KB")
print(f" Final memory: {last_memory / 1024:.1f} KB")
print(f" Total growth: {memory_growth / 1024:.1f} KB")
print(f" Growth per iteration: {growth_per_iteration:.1f} bytes")
print(f" Final cache size: {memory_samples[-1][2]} entries")
# With efficient caching, growth should be roughly linear (O(N))
# rather than quadratic (O(N²)) as it was before
# Memory growth includes ManifestFile objects, metadata, and other overhead
# We expect about 5-10 KB per iteration for typical workloads
# The key improvement is that growth is O(N) not O(N²)
# Threshold of 15KB/iteration based on observed behavior - O(N²) would show ~50KB+/iteration
max_memory_growth_per_iteration_bytes = 15000
assert growth_per_iteration < max_memory_growth_per_iteration_bytes, (
f"Memory growth per iteration ({growth_per_iteration:.0f} bytes) is too high. "
"This may indicate the O(N²) cache inefficiency is present."
)
@pytest.mark.benchmark
def test_memory_after_gc_with_cache_cleared(memory_catalog: InMemoryCatalog) -> None:
"""Test that clearing the cache allows memory to be reclaimed.
This test verifies that when we clear the manifest cache, the associated
memory can be garbage collected.
"""
df = generate_test_dataframe()
table = memory_catalog.create_table("default.gc_test", schema=df.schema)
tracemalloc.start()
print("\n--- Memory After GC Benchmark ---")
# Phase 1: Fill the cache
print("Phase 1: Filling cache with 20 appends...")
for _ in range(20):
table.append(df)
gc.collect()
before_clear_memory, _ = tracemalloc.get_traced_memory()
cache_size_before = len(manifest_module._manifest_cache)
print(f" Memory before clear: {before_clear_memory / 1024:.1f} KB")
print(f" Cache size: {cache_size_before}")
# Phase 2: Clear cache and GC
print("\nPhase 2: Clearing cache and running GC...")
clear_manifest_cache()
gc.collect()
gc.collect() # Multiple GC passes for thorough cleanup
after_clear_memory, _ = tracemalloc.get_traced_memory()
print(f" Memory after clear: {after_clear_memory / 1024:.1f} KB")
print(f" Memory reclaimed: {(before_clear_memory - after_clear_memory) / 1024:.1f} KB")
tracemalloc.stop()
memory_reclaimed = before_clear_memory - after_clear_memory
print("\nResults:")
print(f" Memory reclaimed by clearing cache: {memory_reclaimed / 1024:.1f} KB")
# Verify that clearing the cache actually freed some memory
# Note: This may be flaky in some environments due to GC behavior
assert memory_reclaimed >= 0, "Memory should not increase after clearing cache"
@pytest.mark.benchmark
def test_manifest_cache_deduplication_efficiency() -> None:
"""Benchmark the efficiency of the per-ManifestFile caching strategy.
This test verifies that when multiple manifest lists share the same
ManifestFile objects, they are properly deduplicated in the cache.
"""
from tempfile import TemporaryDirectory
from pyiceberg.io.pyarrow import PyArrowFileIO
from pyiceberg.manifest import (
DataFile,
DataFileContent,
FileFormat,
ManifestEntry,
ManifestEntryStatus,
_manifests,
clear_manifest_cache,
write_manifest,
write_manifest_list,
)
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC
from pyiceberg.schema import Schema
from pyiceberg.typedef import Record
from pyiceberg.types import IntegerType, NestedField
io = PyArrowFileIO()
print("\n--- Manifest Cache Deduplication Benchmark ---")
with TemporaryDirectory() as tmp_dir:
schema = Schema(NestedField(field_id=1, name="id", field_type=IntegerType(), required=True))
spec = UNPARTITIONED_PARTITION_SPEC
# Create N manifest files
num_manifests = 20
manifest_files = []
print(f"Creating {num_manifests} manifest files...")
for i in range(num_manifests):
manifest_path = f"{tmp_dir}/manifest_{i}.avro"
with write_manifest(
format_version=2,
spec=spec,
schema=schema,
output_file=io.new_output(manifest_path),
snapshot_id=i + 1,
avro_compression="null",
) as writer:
data_file = DataFile.from_args(
content=DataFileContent.DATA,
file_path=f"{tmp_dir}/data_{i}.parquet",
file_format=FileFormat.PARQUET,
partition=Record(),
record_count=100,
file_size_in_bytes=1000,
)
writer.add_entry(
ManifestEntry.from_args(
status=ManifestEntryStatus.ADDED,
snapshot_id=i + 1,
data_file=data_file,
)
)
manifest_files.append(writer.to_manifest_file())
# Create multiple manifest lists with overlapping manifest files
# List i contains manifest files 0 through i
num_lists = 10
print(f"Creating {num_lists} manifest lists with overlapping manifests...")
clear_manifest_cache()
for i in range(num_lists):
list_path = f"{tmp_dir}/manifest-list_{i}.avro"
manifests_to_include = manifest_files[: i + 1]
with write_manifest_list(
format_version=2,
output_file=io.new_output(list_path),
snapshot_id=i + 1,
parent_snapshot_id=i if i > 0 else None,
sequence_number=i + 1,
avro_compression="null",
) as list_writer:
list_writer.add_manifests(manifests_to_include)
# Read the manifest list using _manifests (this populates the cache)
_manifests(io, list_path)
# Analyze cache efficiency
cache_entries = len(manifest_module._manifest_cache)
# List i contains manifests 0..i, so only the first num_lists manifests are actually used
manifests_actually_used = num_lists
print("\nResults:")
print(f" Manifest lists created: {num_lists}")
print(f" Manifest files created: {num_manifests}")
print(f" Manifest files actually used: {manifests_actually_used}")
print(f" Cache entries: {cache_entries}")
# With efficient per-ManifestFile caching, we should have exactly
# manifests_actually_used entries (one per unique manifest path)
print(f"\n Expected cache entries (efficient): {manifests_actually_used}")
print(f" Actual cache entries: {cache_entries}")
# The cache should be efficient - one entry per unique manifest path
assert cache_entries == manifests_actually_used, (
f"Cache has {cache_entries} entries, expected exactly {manifests_actually_used}. "
"The cache may not be deduplicating properly."
)