16
16
from earthkit .data .testing import earthkit_examples_file
17
17
18
18
19
+ class TestMetadataCache :
20
+ def __init__ (self ):
21
+ self .hits = 0
22
+ self .misses = 0
23
+ self .data = {}
24
+
25
+ def __contains__ (self , key ):
26
+ return key in self .data
27
+
28
+ def __getitem__ (self , key ):
29
+ self .hits += 1
30
+ return self .data [key ]
31
+
32
+ def __setitem__ (self , key , value ):
33
+ self .misses += 1
34
+ self .data [key ] = value
35
+
36
+ def __len__ (self ):
37
+ return len (self .data )
38
+
39
+
40
+ @pytest .fixture
41
+ def patch_metadata_cache (monkeypatch ):
42
+ from earthkit .data .readers .grib .index import GribResourceManager
43
+
44
+ def patched_make_metadata_cache (self ):
45
+ return TestMetadataCache ()
46
+
47
+ monkeypatch .setattr (GribResourceManager , "_make_metadata_cache" , patched_make_metadata_cache )
48
+
49
+
19
50
def _check_diag (diag , ref ):
20
51
for k , v in ref .items ():
21
52
assert diag [k ] == v , f"{ k } ={ diag [k ]} != { v } "
22
53
23
54
24
55
@pytest .mark .parametrize ("handle_cache_size" , [1 , 5 ])
25
- def test_grib_cache_basic (handle_cache_size ):
56
+ def test_grib_cache_basic (handle_cache_size , patch_metadata_cache ):
26
57
27
58
with settings .temporary (
28
59
{
@@ -99,7 +130,81 @@ def test_grib_cache_basic(handle_cache_size):
99
130
assert ds [0 ].handle == md ._handle
100
131
101
132
102
- def test_grib_cache_options_1 ():
133
+ def test_grib_cache_basic_non_patched ():
134
+ """This test is the same as test_grib_cache_basic but without the patch_metadata_cache fixture.
135
+ So metadata cache hits and misses are not counted."""
136
+ with settings .temporary (
137
+ {
138
+ "grib-field-policy" : "persistent" ,
139
+ "grib-handle-policy" : "cache" ,
140
+ "grib-handle-cache-size" : 1 ,
141
+ "use-grib-metadata-cache" : True ,
142
+ }
143
+ ):
144
+ ds = from_source ("file" , earthkit_examples_file ("tuv_pl.grib" ))
145
+ assert len (ds ) == 18
146
+
147
+ cache = ds ._manager
148
+ assert cache
149
+
150
+ # unique values
151
+ ref_vals = ds .unique_values ("paramId" , "levelist" , "levtype" , "valid_datetime" )
152
+
153
+ ref = {
154
+ "field_cache_size" : 18 ,
155
+ "field_create_count" : 18 ,
156
+ "handle_cache_size" : 1 ,
157
+ "handle_create_count" : 18 ,
158
+ "current_handle_count" : 0 ,
159
+ # "metadata_cache_hits": 0,
160
+ # "metadata_cache_misses": 18 * 6,
161
+ "metadata_cache_size" : 18 * 6 ,
162
+ }
163
+ _check_diag (ds ._diag (), ref )
164
+
165
+ for i , f in enumerate (ds ):
166
+ assert i in cache .field_cache , f"{ i } not in cache"
167
+ assert id (f ) == id (cache .field_cache [i ]), f"{ i } not the same object"
168
+
169
+ _check_diag (ds ._diag (), ref )
170
+
171
+ # unique values repeated
172
+ vals = ds .unique_values ("paramId" , "levelist" , "levtype" , "valid_datetime" )
173
+
174
+ assert vals == ref_vals
175
+
176
+ ref = {
177
+ "field_cache_size" : 18 ,
178
+ "field_create_count" : 18 ,
179
+ "handle_cache_size" : 1 ,
180
+ "handle_create_count" : 18 ,
181
+ "current_handle_count" : 0 ,
182
+ # "metadata_cache_hits": 18 * 4,
183
+ # "metadata_cache_misses": 18 * 6,
184
+ "metadata_cache_size" : 18 * 6 ,
185
+ }
186
+ _check_diag (ds ._diag (), ref )
187
+
188
+ # order by
189
+ ds .order_by (["levelist" , "valid_datetime" , "paramId" , "levtype" ])
190
+ ref = {
191
+ "field_cache_size" : 18 ,
192
+ "field_create_count" : 18 ,
193
+ "handle_cache_size" : 1 ,
194
+ "handle_create_count" : 18 ,
195
+ "current_handle_count" : 0 ,
196
+ # "metadata_cache_misses": 18 * 6,
197
+ "metadata_cache_size" : 18 * 6 ,
198
+ }
199
+ _check_diag (ds ._diag (), ref )
200
+
201
+ # metadata object is not decoupled from the field object
202
+ md = ds [0 ].metadata ()
203
+ assert hasattr (md , "_field" )
204
+ assert ds [0 ].handle == md ._handle
205
+
206
+
207
+ def test_grib_cache_options_1 (patch_metadata_cache ):
103
208
with settings .temporary (
104
209
{
105
210
"grib-field-policy" : "persistent" ,
@@ -179,7 +284,7 @@ def test_grib_cache_options_1():
179
284
_check_diag (ds ._diag (), ref )
180
285
181
286
182
- def test_grib_cache_options_2 ():
287
+ def test_grib_cache_options_2 (patch_metadata_cache ):
183
288
with settings .temporary (
184
289
{
185
290
"grib-field-policy" : "persistent" ,
@@ -261,7 +366,7 @@ def test_grib_cache_options_2():
261
366
_check_diag (ds ._diag (), ref )
262
367
263
368
264
- def test_grib_cache_options_3 ():
369
+ def test_grib_cache_options_3 (patch_metadata_cache ):
265
370
with settings .temporary (
266
371
{
267
372
"grib-field-policy" : "persistent" ,
@@ -341,7 +446,7 @@ def test_grib_cache_options_3():
341
446
_check_diag (ds ._diag (), ref )
342
447
343
448
344
- def test_grib_cache_options_4 ():
449
+ def test_grib_cache_options_4 (patch_metadata_cache ):
345
450
with settings .temporary (
346
451
{
347
452
"grib-field-policy" : "temporary" ,
@@ -420,6 +525,7 @@ def test_grib_cache_options_4():
420
525
_check_diag (
421
526
ds [0 ]._diag (), {"metadata_cache_hits" : 0 , "metadata_cache_misses" : 0 , "metadata_cache_size" : 0 }
422
527
)
528
+
423
529
ref ["field_create_count" ] += 2
424
530
ref ["handle_create_count" ] += 1
425
531
_check_diag (ds ._diag (), ref )
@@ -428,12 +534,13 @@ def test_grib_cache_options_4():
428
534
_check_diag (
429
535
ds [0 ]._diag (), {"metadata_cache_hits" : 0 , "metadata_cache_misses" : 0 , "metadata_cache_size" : 0 }
430
536
)
537
+
431
538
ref ["field_create_count" ] += 2
432
539
ref ["handle_create_count" ] += 1
433
540
_check_diag (ds ._diag (), ref )
434
541
435
542
436
- def test_grib_cache_options_5 ():
543
+ def test_grib_cache_options_5 (patch_metadata_cache ):
437
544
with settings .temporary (
438
545
{
439
546
"grib-field-policy" : "temporary" ,
@@ -529,7 +636,7 @@ def test_grib_cache_options_5():
529
636
_check_diag (ds ._diag (), ref )
530
637
531
638
532
- def test_grib_cache_options_6 ():
639
+ def test_grib_cache_options_6 (patch_metadata_cache ):
533
640
with settings .temporary (
534
641
{
535
642
"grib-field-policy" : "temporary" ,
0 commit comments