From 24106a7826b1989e6b793023f1518940a056b69f Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Thu, 2 Apr 2026 16:50:18 +0300 Subject: [PATCH 1/8] add memory usage tests --- tests/python/tests/memory_usage/__init__.py | 0 .../php/data/component-config.yaml | 7 +++ tests/python/tests/memory_usage/php/index.php | 49 +++++++++++++++++++ .../tests/memory_usage/test_memory_usage.py | 37 ++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 tests/python/tests/memory_usage/__init__.py create mode 100644 tests/python/tests/memory_usage/php/data/component-config.yaml create mode 100644 tests/python/tests/memory_usage/php/index.php create mode 100644 tests/python/tests/memory_usage/test_memory_usage.py diff --git a/tests/python/tests/memory_usage/__init__.py b/tests/python/tests/memory_usage/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/python/tests/memory_usage/php/data/component-config.yaml b/tests/python/tests/memory_usage/php/data/component-config.yaml new file mode 100644 index 0000000000..5ec7d22c4e --- /dev/null +++ b/tests/python/tests/memory_usage/php/data/component-config.yaml @@ -0,0 +1,7 @@ +entry: script +components: + script: + image: KPHP + scope: Request + args: {} + links: {} diff --git a/tests/python/tests/memory_usage/php/index.php b/tests/python/tests/memory_usage/php/index.php new file mode 100644 index 0000000000..5753df5954 --- /dev/null +++ b/tests/python/tests/memory_usage/php/index.php @@ -0,0 +1,49 @@ + Date: Mon, 6 Apr 2026 21:32:22 +0300 Subject: [PATCH 2/8] return json & renames --- .../{memory_usage => memory}/__init__.py | 0 .../php/data/component-config.yaml | 0 tests/python/tests/memory/php/index.php | 65 +++++++++++++++++++ .../test_memory_stats.py} | 11 ++-- tests/python/tests/memory_usage/php/index.php | 49 -------------- 5 files changed, 71 insertions(+), 54 deletions(-) rename tests/python/tests/{memory_usage => memory}/__init__.py (100%) rename tests/python/tests/{memory_usage => memory}/php/data/component-config.yaml (100%) create mode 100644 tests/python/tests/memory/php/index.php rename tests/python/tests/{memory_usage/test_memory_usage.py => memory/test_memory_stats.py} (83%) delete mode 100644 tests/python/tests/memory_usage/php/index.php diff --git a/tests/python/tests/memory_usage/__init__.py b/tests/python/tests/memory/__init__.py similarity index 100% rename from tests/python/tests/memory_usage/__init__.py rename to tests/python/tests/memory/__init__.py diff --git a/tests/python/tests/memory_usage/php/data/component-config.yaml b/tests/python/tests/memory/php/data/component-config.yaml similarity index 100% rename from tests/python/tests/memory_usage/php/data/component-config.yaml rename to tests/python/tests/memory/php/data/component-config.yaml diff --git a/tests/python/tests/memory/php/index.php b/tests/python/tests/memory/php/index.php new file mode 100644 index 0000000000..484ff45b21 --- /dev/null +++ b/tests/python/tests/memory/php/index.php @@ -0,0 +1,65 @@ + Date: Mon, 6 Apr 2026 21:36:11 +0300 Subject: [PATCH 3/8] pass args with keys --- tests/python/tests/memory/test_memory_stats.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/python/tests/memory/test_memory_stats.py b/tests/python/tests/memory/test_memory_stats.py index b400584663..4ec081b9db 100644 --- a/tests/python/tests/memory/test_memory_stats.py +++ b/tests/python/tests/memory/test_memory_stats.py @@ -17,22 +17,24 @@ def _template(self, test_case: str, expected_usage: int, expected_usage_after_un ) def test_memory_usage(self): - self._template("test_memory_usage", 2**20, 0, 2**19 + 2**20) + self._template( + "test_memory_usage", expected_usage=2**20, expected_usage_after_unset=0, expected_peak_usage=2**19 + 2**20 + ) @pytest.mark.k2_skip def test_total_memory_usage_kphp(self): self._template( "test_total_memory_usage", - 48 + sum(2**n for n in range(12, 21)), - 48 + sum(2**n for n in range(12, 20)), - 48 + sum(2**n for n in range(12, 21)), + expected_usage=48 + sum(2**n for n in range(12, 21)), + expected_usage_after_unset=48 + sum(2**n for n in range(12, 20)), + expected_peak_usage=48 + sum(2**n for n in range(12, 21)), ) @pytest.mark.kphp_skip def test_total_memory_usage_k2(self): self._template( "test_total_memory_usage", - 96 + sum(2**n for n in range(12, 21)), - 96 + sum(2**n for n in range(12, 20)), - 96 + sum(2**n for n in range(12, 21)), + expected_usage=96 + sum(2**n for n in range(12, 21)), + expected_usage_after_unset=96 + sum(2**n for n in range(12, 20)), + expected_peak_usage=96 + sum(2**n for n in range(12, 21)), ) From 509cf38054d23a01bbd6d8383f64fee4ec2ed71c Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Mon, 6 Apr 2026 23:53:11 +0300 Subject: [PATCH 4/8] rewrite tests --- tests/python/tests/memory/php/index.php | 37 +++++++++--------- .../python/tests/memory/test_memory_stats.py | 39 ++++++++++--------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/tests/python/tests/memory/php/index.php b/tests/python/tests/memory/php/index.php index 484ff45b21..3e12471d40 100644 --- a/tests/python/tests/memory/php/index.php +++ b/tests/python/tests/memory/php/index.php @@ -2,52 +2,53 @@ $res = []; -function memory_work() { - $a = "a"; +$a = ""; - $n = 1e6; - for ($i = 1; $i < $n; ++$i) { +function memory_work($n) { + global $a; + + for ($i = 1; $i <= $n; ++$i) { $a .= "a"; } - - return $a; } function test_memory_usage() { - global $res; + global $res, $a; $base_usage = memory_get_usage(false); - $a = memory_work(); + memory_work(1e6); $usage = memory_get_usage(false) - $base_usage; - - unset($a); - $usage_after_unset = memory_get_usage(false) - $base_usage; + $a = ""; + + $usage_after_cleaning = memory_get_usage(false) - $base_usage; $peak_usage = memory_get_peak_usage(false) - $base_usage; $res["usage"] = $usage; - $res["usage_after_unset"] = $usage_after_unset; + $res["usage_after_cleaning"] = $usage_after_cleaning; $res["peak_usage"] = $peak_usage; } function test_total_memory_usage() { - global $res; + global $res, $a; + + memory_work(2048); # memory usage for small pieces depends on runtime $base_usage = memory_get_total_usage(); - $a = memory_work(); + memory_work(1e6); $usage = memory_get_total_usage() - $base_usage; - - unset($a); - $usage_after_unset = memory_get_total_usage() - $base_usage; + $a = ""; + + $usage_after_cleaning = memory_get_total_usage() - $base_usage; $peak_usage = memory_get_peak_usage(true) - $base_usage; $res["usage"] = $usage; - $res["usage_after_unset"] = $usage_after_unset; + $res["usage_after_cleaning"] = $usage_after_cleaning; $res["peak_usage"] = $peak_usage; } diff --git a/tests/python/tests/memory/test_memory_stats.py b/tests/python/tests/memory/test_memory_stats.py index 4ec081b9db..916939edcb 100644 --- a/tests/python/tests/memory/test_memory_stats.py +++ b/tests/python/tests/memory/test_memory_stats.py @@ -1,40 +1,41 @@ -import pytest - from python.lib.testcase import WebServerAutoTestCase class TestMemoryUsage(WebServerAutoTestCase): - def _template(self, test_case: str, expected_usage: int, expected_usage_after_unset: int, expected_peak_usage: int): + LOG_PAGE_SIZE = 12 # log_2(4096) + LOG_EXPECTED_LAST_ALLOCATION_SIZE = 20 # ceil(log_2(1e6)) + + def _template(self, test_case: str, expected_usage: int, expected_usage_after_cleaning: int, expected_peak_usage: int): response = self.web_server.http_post(f"/{test_case}") self.assertEqual(response.status_code, 200) self.assertEqual( response.json(), { "usage": expected_usage, - "usage_after_unset": expected_usage_after_unset, + "usage_after_cleaning": expected_usage_after_cleaning, "peak_usage": expected_peak_usage, }, ) def test_memory_usage(self): self._template( - "test_memory_usage", expected_usage=2**20, expected_usage_after_unset=0, expected_peak_usage=2**19 + 2**20 - ) - - @pytest.mark.k2_skip - def test_total_memory_usage_kphp(self): - self._template( - "test_total_memory_usage", - expected_usage=48 + sum(2**n for n in range(12, 21)), - expected_usage_after_unset=48 + sum(2**n for n in range(12, 20)), - expected_peak_usage=48 + sum(2**n for n in range(12, 21)), + "test_memory_usage", + expected_usage=2**self.LOG_EXPECTED_LAST_ALLOCATION_SIZE, + expected_usage_after_cleaning=0, + expected_peak_usage=2**self.LOG_EXPECTED_LAST_ALLOCATION_SIZE + # last allocation size + 2**(self.LOG_EXPECTED_LAST_ALLOCATION_SIZE - 1) # prev allocation size ) - @pytest.mark.kphp_skip - def test_total_memory_usage_k2(self): + def test_total_memory_usage(self): self._template( "test_total_memory_usage", - expected_usage=96 + sum(2**n for n in range(12, 21)), - expected_usage_after_unset=96 + sum(2**n for n in range(12, 20)), - expected_peak_usage=96 + sum(2**n for n in range(12, 21)), + expected_usage=sum( + 2**n for n in range(self.LOG_PAGE_SIZE + 1, self.LOG_EXPECTED_LAST_ALLOCATION_SIZE + 1) + ), + expected_usage_after_cleaning=sum( + 2**n for n in range(self.LOG_PAGE_SIZE + 1, self.LOG_EXPECTED_LAST_ALLOCATION_SIZE) + ), + expected_peak_usage=sum( + 2**n for n in range(self.LOG_PAGE_SIZE + 1, self.LOG_EXPECTED_LAST_ALLOCATION_SIZE + 1) + ), ) From aa5efb16c18e4a68b5337fe91df20d5556695aa9 Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Tue, 7 Apr 2026 14:42:13 +0300 Subject: [PATCH 5/8] fix names --- .../python/tests/memory/test_memory_stats.py | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tests/python/tests/memory/test_memory_stats.py b/tests/python/tests/memory/test_memory_stats.py index 916939edcb..dac9c275e6 100644 --- a/tests/python/tests/memory/test_memory_stats.py +++ b/tests/python/tests/memory/test_memory_stats.py @@ -2,8 +2,8 @@ class TestMemoryUsage(WebServerAutoTestCase): - LOG_PAGE_SIZE = 12 # log_2(4096) - LOG_EXPECTED_LAST_ALLOCATION_SIZE = 20 # ceil(log_2(1e6)) + PAGE_SIZE = 4096 + EXPECTED_LAST_ALLOCATION_SIZE = 2**20 def _template(self, test_case: str, expected_usage: int, expected_usage_after_cleaning: int, expected_peak_usage: int): response = self.web_server.http_post(f"/{test_case}") @@ -20,22 +20,16 @@ def _template(self, test_case: str, expected_usage: int, expected_usage_after_cl def test_memory_usage(self): self._template( "test_memory_usage", - expected_usage=2**self.LOG_EXPECTED_LAST_ALLOCATION_SIZE, + expected_usage=self.EXPECTED_LAST_ALLOCATION_SIZE, expected_usage_after_cleaning=0, - expected_peak_usage=2**self.LOG_EXPECTED_LAST_ALLOCATION_SIZE + # last allocation size - 2**(self.LOG_EXPECTED_LAST_ALLOCATION_SIZE - 1) # prev allocation size + expected_peak_usage=self.EXPECTED_LAST_ALLOCATION_SIZE + # last allocation size + self.EXPECTED_LAST_ALLOCATION_SIZE // 2 # prev allocation size ) def test_total_memory_usage(self): self._template( "test_total_memory_usage", - expected_usage=sum( - 2**n for n in range(self.LOG_PAGE_SIZE + 1, self.LOG_EXPECTED_LAST_ALLOCATION_SIZE + 1) - ), - expected_usage_after_cleaning=sum( - 2**n for n in range(self.LOG_PAGE_SIZE + 1, self.LOG_EXPECTED_LAST_ALLOCATION_SIZE) - ), - expected_peak_usage=sum( - 2**n for n in range(self.LOG_PAGE_SIZE + 1, self.LOG_EXPECTED_LAST_ALLOCATION_SIZE + 1) - ), + expected_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), + expected_usage_after_cleaning=self.EXPECTED_LAST_ALLOCATION_SIZE - (self.PAGE_SIZE << 1), + expected_peak_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), ) From aa8d470a1278ba70a9dd8fb8718029d05b1d0d04 Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Tue, 7 Apr 2026 15:20:09 +0300 Subject: [PATCH 6/8] add comment --- tests/python/tests/memory/test_memory_stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/tests/memory/test_memory_stats.py b/tests/python/tests/memory/test_memory_stats.py index dac9c275e6..5358b8c76c 100644 --- a/tests/python/tests/memory/test_memory_stats.py +++ b/tests/python/tests/memory/test_memory_stats.py @@ -3,7 +3,7 @@ class TestMemoryUsage(WebServerAutoTestCase): PAGE_SIZE = 4096 - EXPECTED_LAST_ALLOCATION_SIZE = 2**20 + EXPECTED_LAST_ALLOCATION_SIZE = 2**20 # 1 MB def _template(self, test_case: str, expected_usage: int, expected_usage_after_cleaning: int, expected_peak_usage: int): response = self.web_server.http_post(f"/{test_case}") From 348e879c2e72a5ddffd004e2f6661aa7dcf06014 Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Tue, 7 Apr 2026 15:32:14 +0300 Subject: [PATCH 7/8] add more comments --- tests/python/tests/memory/test_memory_stats.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/python/tests/memory/test_memory_stats.py b/tests/python/tests/memory/test_memory_stats.py index 5358b8c76c..e3fec0fabc 100644 --- a/tests/python/tests/memory/test_memory_stats.py +++ b/tests/python/tests/memory/test_memory_stats.py @@ -29,7 +29,7 @@ def test_memory_usage(self): def test_total_memory_usage(self): self._template( "test_total_memory_usage", - expected_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), - expected_usage_after_cleaning=self.EXPECTED_LAST_ALLOCATION_SIZE - (self.PAGE_SIZE << 1), - expected_peak_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), + expected_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), # 8192 + 16384 + ... + 2**20 + expected_usage_after_cleaning=self.EXPECTED_LAST_ALLOCATION_SIZE - (self.PAGE_SIZE << 1), # 8192 + 16384 + ... + 2**19 + expected_peak_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), # 8192 + 16384 + ... + 2**20 ) From 2a3aac5e21bf5afe69eb0d8c503391bd7d8953f7 Mon Sep 17 00:00:00 2001 From: Karim Shamazov Date: Tue, 7 Apr 2026 18:56:28 +0300 Subject: [PATCH 8/8] MORE COMMENTS FOR THE GOD OF COMMENTS --- tests/python/tests/memory/php/index.php | 6 +++--- tests/python/tests/memory/test_memory_stats.py | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/python/tests/memory/php/index.php b/tests/python/tests/memory/php/index.php index 3e12471d40..7af36c6aaa 100644 --- a/tests/python/tests/memory/php/index.php +++ b/tests/python/tests/memory/php/index.php @@ -17,7 +17,7 @@ function test_memory_usage() { $base_usage = memory_get_usage(false); - memory_work(1e6); + memory_work(1e6); # 1 MB allocation expected $usage = memory_get_usage(false) - $base_usage; @@ -34,11 +34,11 @@ function test_memory_usage() { function test_total_memory_usage() { global $res, $a; - memory_work(2048); # memory usage for small pieces depends on runtime + memory_work(2048); # memory usage for small pieces depends on runtime $base_usage = memory_get_total_usage(); - memory_work(1e6); + memory_work(1e6); # 1 MB allocation expected $usage = memory_get_total_usage() - $base_usage; diff --git a/tests/python/tests/memory/test_memory_stats.py b/tests/python/tests/memory/test_memory_stats.py index e3fec0fabc..f2ae422a1c 100644 --- a/tests/python/tests/memory/test_memory_stats.py +++ b/tests/python/tests/memory/test_memory_stats.py @@ -2,8 +2,8 @@ class TestMemoryUsage(WebServerAutoTestCase): - PAGE_SIZE = 4096 - EXPECTED_LAST_ALLOCATION_SIZE = 2**20 # 1 MB + STRING_ALIGNMENT = 4096 + MAX_ALLOCATION_SIZE = 2**20 # 1 MB def _template(self, test_case: str, expected_usage: int, expected_usage_after_cleaning: int, expected_peak_usage: int): response = self.web_server.http_post(f"/{test_case}") @@ -20,16 +20,16 @@ def _template(self, test_case: str, expected_usage: int, expected_usage_after_cl def test_memory_usage(self): self._template( "test_memory_usage", - expected_usage=self.EXPECTED_LAST_ALLOCATION_SIZE, + expected_usage=self.MAX_ALLOCATION_SIZE, expected_usage_after_cleaning=0, - expected_peak_usage=self.EXPECTED_LAST_ALLOCATION_SIZE + # last allocation size - self.EXPECTED_LAST_ALLOCATION_SIZE // 2 # prev allocation size + expected_peak_usage=self.MAX_ALLOCATION_SIZE + # last allocation size + self.MAX_ALLOCATION_SIZE // 2 # prev allocation size ) def test_total_memory_usage(self): self._template( "test_total_memory_usage", - expected_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), # 8192 + 16384 + ... + 2**20 - expected_usage_after_cleaning=self.EXPECTED_LAST_ALLOCATION_SIZE - (self.PAGE_SIZE << 1), # 8192 + 16384 + ... + 2**19 - expected_peak_usage=(self.EXPECTED_LAST_ALLOCATION_SIZE << 1) - (self.PAGE_SIZE << 1), # 8192 + 16384 + ... + 2**20 + expected_usage=2 * self.MAX_ALLOCATION_SIZE - 2 * self.STRING_ALIGNMENT, # 8192 + 16384 + ... + 2**20 + expected_usage_after_cleaning=self.MAX_ALLOCATION_SIZE - 2 * self.STRING_ALIGNMENT, # 8192 + 16384 + ... + 2**19 + expected_peak_usage=2 * self.MAX_ALLOCATION_SIZE - 2 * self.STRING_ALIGNMENT, # 8192 + 16384 + ... + 2**20 )