Completed
Pull Request — master (#4)
by mw
22:32 queued 09:07
created

CallFuncMemorySniffer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A memoryTick() 0 5 2
A call() 0 21 2
A getMemoryUsed() 0 3 1
A getTimeUsed() 0 3 1
1
<?php
2
3
namespace Onoi\CallbackContainer;
4
5
use Closure;
6
7
/**
8
 * @see http://stackoverflow.com/questions/19973037/benchmark-memory-usage-in-php
9
 *
10
 * @license GNU GPL v2+
11
 * @since 2.0
12
 */
13
class CallFuncMemorySniffer {
14
15
	/**
16
	 * @var integer
17
	 */
18
	private static $max;
19
20
	/**
21
	 * @var integer
22
	 */
23
	private static $memory;
24
25
	/**
26
	 * @var integer
27
	 */
28
	private $time;
29
30
	/**
31
	 * @since 2.0
32
	 */
33
	public static function memoryTick() {
34
		self::$memory = memory_get_usage() - self::$memory;
35
		self::$max    = self::$memory > self::$max ? self::$memory : self::$max;
36
		self::$memory = memory_get_usage();
37
	}
38
39
	/**
40
	 * @since 2.0
41
	 *
42
	 * @param Closure $func
43
	 * @param array|null $args
44
	 *
45
	 * @return mixed
46
	 */
47
	public function call( Closure $func, $args = null ) {
48
49
		declare( ticks=1 );
50
51
		self::$memory = memory_get_usage();
52
		self::$max    = 0;
53
54
		register_tick_function(
55
			'call_user_func_array',
56
			array( '\Onoi\CallbackContainer\CallFuncMemorySniffer', 'memoryTick' ),
57
			array()
58
		);
59
60
		$this->time = microtime( true );
0 ignored issues
show
Documentation Bug introduced by
The property $time was declared of type integer, but microtime(true) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
61
		$result = is_array( $args ) ? call_user_func_array( $func, $args ): call_user_func( $func );
62
		$this->time = microtime( true ) - $this->time;
0 ignored issues
show
Documentation Bug introduced by
The property $time was declared of type integer, but microtime(true) - $this->time is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
63
64
		unregister_tick_function( 'call_user_func_array' );
65
66
		return $result;
67
	}
68
69
	/**
70
	 * @since 2.0
71
	 *
72
	 * @return integer
73
	 */
74
	public function getMemoryUsed() {
75
		return self::$max;
76
	}
77
78
	/**
79
	 * @since 2.0
80
	 *
81
	 * @return float
82
	 */
83
	public function getTimeUsed() {
84
		return $this->time;
85
	}
86
87
}
88