|
| 1 | +from python.lib.testcase import WebServerAutoTestCase |
| 2 | + |
| 3 | + |
| 4 | +class TestMemoryUsage(WebServerAutoTestCase): |
| 5 | + STRING_ALIGNMENT = 4096 |
| 6 | + MAX_ALLOCATION_SIZE = 2**20 # 1 MB |
| 7 | + |
| 8 | + def _template(self, test_case: str, expected_usage: int, expected_usage_after_cleaning: int, expected_peak_usage: int): |
| 9 | + response = self.web_server.http_post(f"/{test_case}") |
| 10 | + self.assertEqual(response.status_code, 200) |
| 11 | + self.assertEqual( |
| 12 | + response.json(), |
| 13 | + { |
| 14 | + "usage": expected_usage, |
| 15 | + "usage_after_cleaning": expected_usage_after_cleaning, |
| 16 | + "peak_usage": expected_peak_usage, |
| 17 | + }, |
| 18 | + ) |
| 19 | + |
| 20 | + def test_memory_usage(self): |
| 21 | + self._template( |
| 22 | + "test_memory_usage", |
| 23 | + expected_usage=self.MAX_ALLOCATION_SIZE, |
| 24 | + expected_usage_after_cleaning=0, |
| 25 | + expected_peak_usage=self.MAX_ALLOCATION_SIZE + # last allocation size |
| 26 | + self.MAX_ALLOCATION_SIZE // 2 # prev allocation size |
| 27 | + ) |
| 28 | + |
| 29 | + def test_total_memory_usage(self): |
| 30 | + self._template( |
| 31 | + "test_total_memory_usage", |
| 32 | + expected_usage=2 * self.MAX_ALLOCATION_SIZE - 2 * self.STRING_ALIGNMENT, # 8192 + 16384 + ... + 2**20 |
| 33 | + expected_usage_after_cleaning=self.MAX_ALLOCATION_SIZE - 2 * self.STRING_ALIGNMENT, # 8192 + 16384 + ... + 2**19 |
| 34 | + expected_peak_usage=2 * self.MAX_ALLOCATION_SIZE - 2 * self.STRING_ALIGNMENT, # 8192 + 16384 + ... + 2**20 |
| 35 | + ) |
0 commit comments