Skip to content

Commit 02f6a1d

Browse files
authored
add memory usage tests (#1590)
1 parent ceabd49 commit 02f6a1d

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

tests/python/tests/memory/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
entry: script
2+
components:
3+
script:
4+
image: KPHP
5+
scope: Request
6+
args: {}
7+
links: {}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
$res = [];
4+
5+
$a = "";
6+
7+
function memory_work($n) {
8+
global $a;
9+
10+
for ($i = 1; $i <= $n; ++$i) {
11+
$a .= "a";
12+
}
13+
}
14+
15+
function test_memory_usage() {
16+
global $res, $a;
17+
18+
$base_usage = memory_get_usage(false);
19+
20+
memory_work(1e6); # 1 MB allocation expected
21+
22+
$usage = memory_get_usage(false) - $base_usage;
23+
24+
$a = "";
25+
26+
$usage_after_cleaning = memory_get_usage(false) - $base_usage;
27+
$peak_usage = memory_get_peak_usage(false) - $base_usage;
28+
29+
$res["usage"] = $usage;
30+
$res["usage_after_cleaning"] = $usage_after_cleaning;
31+
$res["peak_usage"] = $peak_usage;
32+
}
33+
34+
function test_total_memory_usage() {
35+
global $res, $a;
36+
37+
memory_work(2048); # memory usage for small pieces depends on runtime
38+
39+
$base_usage = memory_get_total_usage();
40+
41+
memory_work(1e6); # 1 MB allocation expected
42+
43+
$usage = memory_get_total_usage() - $base_usage;
44+
45+
$a = "";
46+
47+
$usage_after_cleaning = memory_get_total_usage() - $base_usage;
48+
$peak_usage = memory_get_peak_usage(true) - $base_usage;
49+
50+
$res["usage"] = $usage;
51+
$res["usage_after_cleaning"] = $usage_after_cleaning;
52+
$res["peak_usage"] = $peak_usage;
53+
}
54+
55+
switch($_SERVER["PHP_SELF"]) {
56+
case "/test_memory_usage":
57+
test_memory_usage();
58+
break;
59+
case "/test_total_memory_usage":
60+
test_total_memory_usage();
61+
break;
62+
default:
63+
critical_error('unexpected "' . $_SERVER["PHP_SELF"] . '"');
64+
}
65+
66+
echo json_encode($res);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

Comments
 (0)