Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
7 changes: 7 additions & 0 deletions tests/python/tests/memory/php/data/component-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
entry: script
components:
script:
image: KPHP
scope: Request
args: {}
links: {}
66 changes: 66 additions & 0 deletions tests/python/tests/memory/php/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

$res = [];

$a = "";

function memory_work($n) {
global $a;

for ($i = 1; $i <= $n; ++$i) {
$a .= "a";
}
}

function test_memory_usage() {
global $res, $a;

$base_usage = memory_get_usage(false);

memory_work(1e6); # 1 MB allocation expected

$usage = 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_cleaning"] = $usage_after_cleaning;
$res["peak_usage"] = $peak_usage;
}

function test_total_memory_usage() {
global $res, $a;

memory_work(2048); # memory usage for small pieces depends on runtime

$base_usage = memory_get_total_usage();

memory_work(1e6); # 1 MB allocation expected

$usage = 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_cleaning"] = $usage_after_cleaning;
$res["peak_usage"] = $peak_usage;
}

switch($_SERVER["PHP_SELF"]) {
case "/test_memory_usage":
test_memory_usage();
break;
case "/test_total_memory_usage":
test_total_memory_usage();
break;
default:
critical_error('unexpected "' . $_SERVER["PHP_SELF"] . '"');
}

echo json_encode($res);
35 changes: 35 additions & 0 deletions tests/python/tests/memory/test_memory_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from python.lib.testcase import WebServerAutoTestCase


class TestMemoryUsage(WebServerAutoTestCase):
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}")
self.assertEqual(response.status_code, 200)
self.assertEqual(
response.json(),
{
"usage": expected_usage,
"usage_after_cleaning": expected_usage_after_cleaning,
"peak_usage": expected_peak_usage,
},
)

def test_memory_usage(self):
self._template(
"test_memory_usage",
expected_usage=self.MAX_ALLOCATION_SIZE,
expected_usage_after_cleaning=0,
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=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
)
Loading