File: profiler-sorting.phpt

package info (click to toggle)
php-luasandbox 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 620 kB
  • sloc: ansic: 3,700; xml: 2,152; php: 64; makefile: 13; sh: 3
file content (62 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
--TEST--
profiler sorting
--FILE--
<?php

// Note these tests have to busy-loop. Even if Lua had an "os.sleep", it'd just
// say "sleep" used all the time. And we can't directly loop on os.clock() here
// either, because that would say "clock" used most of the time.

$lua = <<<LUA
	function test1()
		for i = 0, 1e6 do end
	end

	function test2()
		for i = 0, 4e6 do end
	end

	function test3()
		for i = 0, 2e6 do end
	end

	function test()
		local t = os.clock() + 0.5
		while os.clock() < t do
			test1()
			test2()
			test3()
		end
	end
LUA;

$sandbox = new LuaSandbox;
$sandbox->loadString( $lua )->call();
$sandbox->enableProfiler( 0.01 );

$sandbox->callFunction( 'test' );

foreach( [
	'samples' => LuaSandbox::SAMPLES,
	'seconds' => LuaSandbox::SECONDS,
	'percent' => LuaSandbox::PERCENT
] as $name => $stat ) {
	$result = $sandbox->getProfilerFunctionReport( $stat );
	// "clone" and sort
	$sorted = array_combine( array_keys( $result ), array_values( $result ) );
	arsort( $sorted );
	if ( $result === $sorted ) {
		echo "$name: OK\n";
	} else {
		echo "$name: FAIL\n";
		var_export( [
			'result' => $result,
			'sorted' => $sorted,
		] );
	}
}

--EXPECTF--
samples: OK
seconds: OK
percent: OK