Skip to content

Commit f57cdf7

Browse files
committed
test(profiling): allocation_sampling_distance regression
1 parent b5c50d0 commit f57cdf7

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
[profiling] allocation sampling distance is configurable
3+
--DESCRIPTION--
4+
This code path had a regression, so it seems worth adding a test to ensure it
5+
cannot regress again.
6+
--SKIPIF--
7+
<?php
8+
if (!extension_loaded('datadog-profiling'))
9+
die("skip: test requires datadog-profiling");
10+
?>
11+
--INI--
12+
datadog.profiling.enabled=1
13+
datadog.profiling.allocation_enabled=1
14+
datadog.profiling.allocation_sampling_distance=1
15+
datadog.profiling.log_level=trace
16+
datadog.profiling.output_pprof=/tmp/profiling-data/pprof
17+
zend.assertions=1
18+
assert.exception=1
19+
--FILE--
20+
<?php
21+
// Goal: trigger a smallish allocation so we won't run afoul of the default
22+
// sampling distance, but also something unique-ishly sized so we can be
23+
// reasonably sure that the log corresponds to our inputs and not accidentally
24+
// something else.
25+
//
26+
// A zend_string costs 24 bytes on 64-bit architectures just for the struct,
27+
// plus we need the string data and null.
28+
// 24 + strlen($str) + 1
29+
// We want a number so that the total number of bytes is evenly divisible by
30+
// 16 to avoid worrying about rounding/padding that can apply. So let's target
31+
// 112 total bytes which is divisible by 16 and seems relatively unique-ish:
32+
// 112 = 24 + strlen($str) + 1
33+
// 112 - 24 - 1 = strlen($str)
34+
// 87 = strlen($str)
35+
// ... except that the engine does the rounding in the wrong place for
36+
// str_repeat, so it with our inputs it ends up doing this, with `f` being the
37+
// function which determines the rounded amount:
38+
// 1 * 87 + f(24 + 0 + 1)
39+
// 87 + f(25)
40+
// 87 + 32 = 119
41+
// So it over-allocates by 7 bytes, and the log will have 119 bytes.
42+
// Our regex allows for 112 (correct) and 119 (observed).
43+
$str = \str_repeat('a', 87);
44+
?>
45+
Done.
46+
--EXPECTREGEX--
47+
.* Memory allocation profiling initialized with a sampling distance of 1 bytes.*
48+
.* Sent stack sample of [0-9]* frames, .* labels, 11[2,9] bytes allocated, and 1 allocations to profiler.*
49+
.*Done\..*

0 commit comments

Comments
 (0)