Repository navigation
[wip] Optimize test durations #8634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Jens Hedegaard Nielsen (jenshnielsen)
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
jenshnielsen:qcodes_faster_tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Loading data from a ``DataSet`` is now faster. Array and complex valued data is read | ||
| several times faster since the header of the stored arrays is only parsed once per | ||
| parameter rather than once per stored value. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import io | ||
| import math | ||
| from typing import TYPE_CHECKING, Any, Literal | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from qcodes.dataset.sqlite.database import ( | ||
| _adapt_array, | ||
| _adapt_complex, | ||
| _convert_array, | ||
| _convert_complex, | ||
| _convert_numeric, | ||
| _parse_npy_header, | ||
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| import numpy.typing as npt | ||
| from pytest_mock import MockerFixture | ||
|
|
||
|
|
||
| def _serialize( | ||
| array: npt.NDArray, version: tuple[int, int] | None, allow_pickle: bool = False | ||
| ) -> bytes: | ||
| out = io.BytesIO() | ||
| if version is None: | ||
| np.save(out, array, allow_pickle=allow_pickle) | ||
| else: | ||
| np.lib.format.write_array( | ||
| out, array, version=version, allow_pickle=allow_pickle | ||
| ) | ||
| return out.getvalue() | ||
|
|
||
|
|
||
| def _read_with_numpy(serialized: bytes) -> npt.NDArray: | ||
| return np.lib.format.read_array(io.BytesIO(serialized), allow_pickle=False) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("version", [(1, 0), (2, 0), (3, 0), None]) | ||
| @pytest.mark.parametrize("order", ["C", "F"]) | ||
| @pytest.mark.parametrize("shape", [(), (0,), (1,), (7,), (3, 4), (2, 3, 4), (0, 3)]) | ||
| @pytest.mark.parametrize( | ||
| "dtype", | ||
| [ | ||
| "f8", | ||
| ">f8", | ||
| "f4", | ||
| "i1", | ||
| ">i4", | ||
| "u8", | ||
| "c8", | ||
| "c16", | ||
| "?", | ||
| "<U5", | ||
| ">U3", | ||
| "S4", | ||
| "M8[ns]", | ||
| ], | ||
| ) | ||
| def test_convert_array_matches_numpy( | ||
| mocker: MockerFixture, | ||
| dtype: Any, | ||
| shape: tuple[int, ...], | ||
| order: Literal["C", "F"], | ||
| version: tuple[int, int] | None, | ||
| ) -> None: | ||
| array = np.asarray(np.zeros(shape, dtype=np.dtype(dtype)), order=order) | ||
| array.flat = np.arange(array.size) % 100 | ||
| serialized = _serialize(array, version) | ||
| expected = _read_with_numpy(serialized) | ||
|
|
||
| read_array_spy = mocker.spy(np.lib.format, "read_array") | ||
| result = _convert_array(serialized) | ||
|
|
||
| read_array_spy.assert_not_called() | ||
| assert result.dtype == expected.dtype | ||
| assert result.shape == expected.shape | ||
| assert result.flags.c_contiguous == expected.flags.c_contiguous | ||
| assert result.flags.f_contiguous == expected.flags.f_contiguous | ||
| assert result.flags.writeable | ||
| assert result.tobytes() == expected.tobytes() | ||
|
|
||
|
|
||
| def test_convert_array_round_trips_adapted_array() -> None: | ||
| array = np.random.default_rng().random((3, 5)) | ||
| result = _convert_array(bytes(_adapt_array(array))) | ||
| np.testing.assert_array_equal(result, array) | ||
| # the returned array must not share memory with the immutable input bytes | ||
| result[0, 0] = 1.0 | ||
|
|
||
|
|
||
| def test_convert_array_non_ascii_header_falls_back_to_numpy( | ||
| mocker: MockerFixture, | ||
| ) -> None: | ||
| serialized = _serialize(np.zeros(2, dtype=[("\u00e5", "f8")]), (3, 0)) | ||
| read_array_spy = mocker.spy(np.lib.format, "read_array") | ||
|
|
||
| result = _convert_array(serialized) | ||
|
|
||
| read_array_spy.assert_called_once() | ||
| assert result.dtype == _read_with_numpy(serialized).dtype | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "dtype", | ||
| [ | ||
| [("a", "f8"), ("b", "i4")], | ||
| [("a", "f8"), ("b", [("c", "i4"), ("d", "f4")])], | ||
| ], | ||
| ) | ||
| def test_convert_array_structured_dtype_is_not_shared( | ||
| mocker: MockerFixture, dtype: Any | ||
| ) -> None: | ||
| serialized = _serialize(np.zeros(2, dtype=dtype), (3, 0)) | ||
| expected = _read_with_numpy(serialized) | ||
| read_array_spy = mocker.spy(np.lib.format, "read_array") | ||
|
|
||
| result = _convert_array(serialized) | ||
| read_array_spy.assert_called_once() | ||
| assert result.dtype == expected.dtype | ||
| # renaming the fields of one result must not affect later reads | ||
| assert result.dtype.names is not None | ||
| result.dtype.names = tuple(f"renamed_{name}" for name in result.dtype.names) | ||
| nested_dtype = result.dtype[-1] | ||
| if nested_dtype.names is not None: | ||
| nested_dtype.names = tuple(f"renamed_{name}" for name in nested_dtype.names) | ||
|
|
||
| assert _convert_array(serialized).dtype == expected.dtype | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("header_size", "available_header_bytes"), | ||
| [(10_001, 10_001), (5_000, 100), (2**31, 100)], | ||
| ids=["too_large", "truncated", "too_large_and_truncated"], | ||
| ) | ||
| def test_convert_array_invalid_header_size_is_not_cached( | ||
| header_size: int, available_header_bytes: int | ||
| ) -> None: | ||
| serialized = ( | ||
| b"\x93NUMPY\x03\x00" | ||
| + header_size.to_bytes(4, "little") | ||
| + b" " * available_header_bytes | ||
| ) | ||
| _parse_npy_header.cache_clear() | ||
|
|
||
| with pytest.raises(ValueError): | ||
| _convert_array(serialized) | ||
|
|
||
| assert _parse_npy_header.cache_info().currsize == 0 | ||
|
|
||
|
|
||
| def test_convert_array_object_array_raises() -> None: | ||
| serialized = _serialize(np.array([1, "a"], dtype=object), (3, 0), True) | ||
| with pytest.raises(ValueError, match="Object arrays cannot be loaded"): | ||
| _convert_array(serialized) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("serialized", [b"", b"not an array", b"\x93NUMP"]) | ||
| def test_convert_array_invalid_input_raises(serialized: bytes) -> None: | ||
| with pytest.raises(ValueError): | ||
| _convert_array(serialized) | ||
|
|
||
|
|
||
| def test_convert_array_truncated_data_raises() -> None: | ||
| serialized = bytes(_adapt_array(np.arange(10.0)))[:-8] | ||
| with pytest.raises(ValueError, match="array data"): | ||
| _convert_array(serialized) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "value", | ||
| [1 + 2j, np.complex64(3 - 1j), np.complex128(-0.5 + 0.25j), complex("nan+infj")], | ||
| ) | ||
| def test_convert_complex(value: complex | np.complexfloating) -> None: | ||
| serialized = bytes(_adapt_complex(value)) | ||
| expected = np.load(io.BytesIO(serialized))[0] | ||
|
|
||
| result = _convert_complex(serialized) | ||
|
|
||
| assert type(result) is type(expected) | ||
| np.testing.assert_array_equal(result, expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("value", "expected"), | ||
| [ | ||
| (b"1.5", 1.5), | ||
| (b"-2.25e-3", -2.25e-3), | ||
| (b"3", 3), | ||
| (b"3.0", 3), | ||
| (b"-0.0", 0), | ||
| (b"1e20", 10**20), | ||
| (b"inf", math.inf), | ||
| (b"-Infinity", -math.inf), | ||
| (b"some text", "some text"), | ||
| ], | ||
| ) | ||
| def test_convert_numeric(value: bytes, expected: float | str) -> None: | ||
| result = _convert_numeric(value) | ||
| assert type(result) is type(expected) | ||
| assert result == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("value", [b"nan", b"NaN", b"-nan"]) | ||
| def test_convert_numeric_nan(value: bytes) -> None: | ||
| result = _convert_numeric(value) | ||
| assert isinstance(result, float) | ||
| assert math.isnan(result) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.