| 1 | from pytest_codspeed import BenchmarkFixture |
| 2 | |
| 3 | from ulid_transform import ( |
| 4 | bytes_to_ulid, |
| 5 | bytes_to_ulid_or_none, |
| 6 | ulid_at_time, |
| 7 | ulid_at_time_bytes, |
| 8 | ulid_hex, |
| 9 | ulid_now, |
| 10 | ulid_now_bytes, |
| 11 | ulid_to_bytes, |
| 12 | ulid_to_bytes_or_none, |
| 13 | ulid_to_timestamp, |
| 14 | ) |
| 15 | |
| 16 | ITERATIONS = 10000 |
| 17 | |
| 18 | _SAMPLE_ULID_STR = "01GTCKZT7K26YEVVW6AMQ3J0VT" |
| 19 | _SAMPLE_ULID_BYTES = b"\x01\x86\x99?\xe8\xf3\x11\xbc\xed\xef\x86U.9\x03z" |
| 20 | |
| 21 | |
| 22 | def test_ulid_now(benchmark: BenchmarkFixture) -> None: |
| 23 | _ulid_now = ulid_now |
| 24 | |
| 25 | @benchmark |
| 26 | def _(): |
| 27 | for _ in range(ITERATIONS): |
| 28 | _ulid_now() |
| 29 | |
| 30 | |
| 31 | def test_ulid_now_bytes(benchmark: BenchmarkFixture) -> None: |
| 32 | _ulid_now_bytes = ulid_now_bytes |
| 33 | |
| 34 | @benchmark |
| 35 | def _(): |
| 36 | for _ in range(ITERATIONS): |
| 37 | _ulid_now_bytes() |
| 38 | |
| 39 | |
| 40 | def test_ulid_hex(benchmark: BenchmarkFixture) -> None: |
| 41 | _ulid_hex = ulid_hex |
| 42 | |
| 43 | @benchmark |
| 44 | def _(): |
| 45 | for _ in range(ITERATIONS): |
| 46 | _ulid_hex() |
| 47 | |
| 48 | |
| 49 | def test_ulid_at_time(benchmark: BenchmarkFixture) -> None: |
| 50 | _ulid_at_time = ulid_at_time |
| 51 | |
| 52 | @benchmark |
| 53 | def _(): |
| 54 | for _ in range(ITERATIONS): |
| 55 | _ulid_at_time(1) |
| 56 | |
| 57 | |
| 58 | def test_ulid_at_time_bytes(benchmark: BenchmarkFixture) -> None: |
| 59 | _ulid_at_time_bytes = ulid_at_time_bytes |
| 60 | |
| 61 | @benchmark |
| 62 | def _(): |
| 63 | for _ in range(ITERATIONS): |
| 64 | _ulid_at_time_bytes(1) |
| 65 | |
| 66 | |
| 67 | def test_ulid_to_bytes(benchmark: BenchmarkFixture) -> None: |
| 68 | _ulid_to_bytes = ulid_to_bytes |
| 69 | sample = _SAMPLE_ULID_STR |
| 70 | |
| 71 | @benchmark |
| 72 | def _(): |
| 73 | for _ in range(ITERATIONS): |
| 74 | _ulid_to_bytes(sample) |
| 75 | |
| 76 | |
| 77 | def test_ulid_to_bytes_or_none_valid(benchmark: BenchmarkFixture) -> None: |
| 78 | _ulid_to_bytes_or_none = ulid_to_bytes_or_none |
| 79 | sample = _SAMPLE_ULID_STR |
| 80 | |
| 81 | @benchmark |
| 82 | def _(): |
| 83 | for _ in range(ITERATIONS): |
| 84 | _ulid_to_bytes_or_none(sample) |
| 85 | |
| 86 | |
| 87 | def test_ulid_to_bytes_or_none_invalid(benchmark: BenchmarkFixture) -> None: |
| 88 | _ulid_to_bytes_or_none = ulid_to_bytes_or_none |
| 89 | sample = "not a valid ulid" |
| 90 | |
| 91 | @benchmark |
| 92 | def _(): |
| 93 | for _ in range(ITERATIONS): |
| 94 | _ulid_to_bytes_or_none(sample) |
| 95 | |
| 96 | |
| 97 | def test_bytes_to_ulid(benchmark: BenchmarkFixture) -> None: |
| 98 | _bytes_to_ulid = bytes_to_ulid |
| 99 | sample = _SAMPLE_ULID_BYTES |
| 100 | |
| 101 | @benchmark |
| 102 | def _(): |
| 103 | for _ in range(ITERATIONS): |
| 104 | _bytes_to_ulid(sample) |
| 105 | |
| 106 | |
| 107 | def test_bytes_to_ulid_or_none_valid(benchmark: BenchmarkFixture) -> None: |
| 108 | _bytes_to_ulid_or_none = bytes_to_ulid_or_none |
| 109 | sample = _SAMPLE_ULID_BYTES |
| 110 | |
| 111 | @benchmark |
| 112 | def _(): |
| 113 | for _ in range(ITERATIONS): |
| 114 | _bytes_to_ulid_or_none(sample) |
| 115 | |
| 116 | |
| 117 | def test_bytes_to_ulid_or_none_invalid(benchmark: BenchmarkFixture) -> None: |
| 118 | _bytes_to_ulid_or_none = bytes_to_ulid_or_none |
| 119 | sample = b"too short" |
| 120 | |
| 121 | @benchmark |
| 122 | def _(): |
| 123 | for _ in range(ITERATIONS): |
| 124 | _bytes_to_ulid_or_none(sample) |
| 125 | |
| 126 | |
| 127 | def test_ulid_to_timestamp_str(benchmark: BenchmarkFixture) -> None: |
| 128 | _ulid_to_timestamp = ulid_to_timestamp |
| 129 | sample = _SAMPLE_ULID_STR |
| 130 | |
| 131 | @benchmark |
| 132 | def _(): |
| 133 | for _ in range(ITERATIONS): |
| 134 | _ulid_to_timestamp(sample) |
| 135 | |
| 136 | |
| 137 | def test_ulid_to_timestamp_bytes(benchmark: BenchmarkFixture) -> None: |
| 138 | _ulid_to_timestamp = ulid_to_timestamp |
| 139 | sample = _SAMPLE_ULID_BYTES |
| 140 | |
| 141 | @benchmark |
| 142 | def _(): |
| 143 | for _ in range(ITERATIONS): |
| 144 | _ulid_to_timestamp(sample) |