Skip to content

Commit

Permalink
tests: how frequently does the cache get hit?
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Sep 27, 2024
1 parent 1c8c08c commit 7dd2403
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lumicks/pylake/tests/test_file/test_caching.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import pytest

from lumicks import pylake
from lumicks.pylake.channel import _get_array


def test_global_cache_continuous(h5_file):
# Load the file (never storing the file handle)
f1x1 = pylake.File.from_h5py(h5_file).force1x
f1x2 = pylake.File.from_h5py(h5_file).force1x

# These should point to the same data
assert id(f1x1.data) == id(f1x2.data)
_get_array.cache_clear()

# Load the file (never storing the file handle)
f1x1 = pylake.File.from_h5py(h5_file)["Force HF/Force 1x"]
f1x2 = pylake.File.from_h5py(h5_file).force1x
assert _get_array.cache_info().hits == 0 # No cache used yet (lazy loading)

# These should point to the same data
assert id(f1x1.data) == id(f1x2.data)
assert _get_array.cache_info().hits == 1

with pytest.raises(ValueError, match="assignment destination is read-only"):
f1x1.data[5:100] = 3
Expand All @@ -26,12 +24,17 @@ def test_global_cache_continuous(h5_file):


def test_global_cache_timeseries(h5_file):
_get_array.cache_clear()

f1x1 = pylake.File.from_h5py(h5_file).downsampled_force1x
f1x2 = pylake.File.from_h5py(h5_file).downsampled_force1x
assert _get_array.cache_info().hits == 0 # No cache used yet (lazy loading)

# These should point to the same data
assert id(f1x1.data) == id(f1x2.data)
assert _get_array.cache_info().hits == 1
assert id(f1x1.timestamps) == id(f1x2.timestamps)
assert _get_array.cache_info().hits == 2

with pytest.raises(ValueError, match="assignment destination is read-only"):
f1x1.data[5:100] = 3
Expand All @@ -42,11 +45,15 @@ def test_global_cache_timeseries(h5_file):

def test_global_cache_timetags(h5_file):
if pylake.File.from_h5py(h5_file).format_version == 2:
_get_array.cache_clear()
tags1 = pylake.File.from_h5py(h5_file)["Photon Time Tags"]["Red"]
assert _get_array.cache_info().hits == 3 # start and stop pull the data every time
tags2 = pylake.File.from_h5py(h5_file)["Photon Time Tags"]["Red"]
assert _get_array.cache_info().hits == 6

# These should point to the same data
assert id(tags1.data) == id(tags2.data)
assert _get_array.cache_info().hits == 1

with pytest.raises(ValueError, match="assignment destination is read-only"):
tags1.data[5:100] = 3

0 comments on commit 7dd2403

Please sign in to comment.