Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit cf104c7

Browse files
committed
Merge pull request #42 from kbond/opcache-check
[RFC] added OpCacheMemory check
2 parents a2cb464 + 6825024 commit cf104c7

File tree

6 files changed

+261
-88
lines changed

6 files changed

+261
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use InvalidArgumentException;
9+
use ZendDiagnostics\Result\Failure;
10+
use ZendDiagnostics\Result\Skip;
11+
use ZendDiagnostics\Result\Success;
12+
use ZendDiagnostics\Result\Warning;
13+
14+
/**
15+
* Abstract class for handling different memory checks
16+
*/
17+
abstract class AbstractMemoryCheck extends AbstractCheck implements CheckInterface
18+
{
19+
/**
20+
* Percentage that will cause a warning.
21+
*
22+
* @var int
23+
*/
24+
protected $warningThreshold;
25+
26+
/**
27+
* Percentage that will cause a fail.
28+
*
29+
* @var int
30+
*/
31+
protected $criticalThreshold;
32+
33+
/**
34+
* @param int $warningThreshold A number between 0 and 100
35+
* @param int $criticalThreshold A number between 0 and 100
36+
* @throws InvalidArgumentException
37+
*/
38+
public function __construct($warningThreshold, $criticalThreshold)
39+
{
40+
if (!is_numeric($warningThreshold)) {
41+
throw new InvalidArgumentException('Invalid warningThreshold argument - expecting an integer');
42+
}
43+
44+
if (!is_numeric($criticalThreshold)) {
45+
throw new InvalidArgumentException('Invalid criticalThreshold argument - expecting an integer');
46+
}
47+
48+
if ($warningThreshold > 100 || $warningThreshold < 0) {
49+
throw new InvalidArgumentException('Invalid warningThreshold argument - expecting an integer between 1 and 100');
50+
}
51+
52+
if ($criticalThreshold > 100 || $criticalThreshold < 0) {
53+
throw new InvalidArgumentException('Invalid criticalThreshold argument - expecting an integer between 1 and 100');
54+
}
55+
56+
$this->warningThreshold = (int)$warningThreshold;
57+
$this->criticalThreshold = (int)$criticalThreshold;
58+
}
59+
60+
/**
61+
* Perform the check
62+
*
63+
* @see \ZendDiagnostics\Check\CheckInterface::check() *
64+
* @return Failure|Skip|Success|Warning
65+
*/
66+
public function check()
67+
{
68+
$percentUsed = ($this->getUsedMemory() / $this->getTotalMemory()) * 100;
69+
$message = sprintf('%.0f%% of available %s memory used.', $percentUsed, $this->formatBytes($this->getTotalMemory()));
70+
71+
if ($percentUsed > $this->criticalThreshold) {
72+
return new Failure($message);
73+
}
74+
75+
if ($percentUsed > $this->warningThreshold) {
76+
return new Warning($message);
77+
}
78+
79+
return new Success($message);
80+
}
81+
82+
/**
83+
* Returns the total memory in bytes
84+
*
85+
* @return int
86+
*/
87+
abstract protected function getTotalMemory();
88+
89+
/**
90+
* Returns the used memory in bytes
91+
*
92+
* @return int
93+
*/
94+
abstract protected function getUsedMemory();
95+
96+
/**
97+
* @param int $bytes
98+
* @return string
99+
*/
100+
private function formatBytes($bytes)
101+
{
102+
$size = 'B';
103+
104+
foreach (array('B','KB','MB','GB') as $size) {
105+
if ($bytes < 1024) {
106+
break;
107+
}
108+
109+
$bytes /= 1024;
110+
}
111+
112+
return sprintf("%.0f %s", $bytes, $size);
113+
}
114+
}

src/ZendDiagnostics/Check/ApcMemory.php

+20-68
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace ZendDiagnostics\Check;
77

8-
use InvalidArgumentException;
98
use ZendDiagnostics\Result\Failure;
109
use ZendDiagnostics\Result\Skip;
1110
use ZendDiagnostics\Result\Success;
@@ -20,48 +19,14 @@
2019
* license: The PHP License, version 3.01
2120
* copyright: Copyright (c) 2006-2011 The PHP Group
2221
*/
23-
class ApcMemory extends AbstractCheck implements CheckInterface
22+
class ApcMemory extends AbstractMemoryCheck
2423
{
2524
/**
26-
* Percentage that will cause a warning.
25+
* APC information
2726
*
28-
* @var int
27+
* @var array
2928
*/
30-
protected $warningThreshold;
31-
32-
/**
33-
* Percentage that will cause a fail.
34-
*
35-
* @var int
36-
*/
37-
protected $criticalThreshold;
38-
39-
/**
40-
* @param int $warningThreshold A number between 0 and 100
41-
* @param int $criticalThreshold A number between 0 and 100
42-
* @throws InvalidArgumentException
43-
*/
44-
public function __construct($warningThreshold, $criticalThreshold)
45-
{
46-
if (!is_numeric($warningThreshold)) {
47-
throw new InvalidArgumentException('Invalid warningThreshold argument - expecting an integer');
48-
}
49-
50-
if (!is_numeric($criticalThreshold)) {
51-
throw new InvalidArgumentException('Invalid criticalThreshold argument - expecting an integer');
52-
}
53-
54-
if ($warningThreshold > 100 || $warningThreshold < 0) {
55-
throw new InvalidArgumentException('Invalid warningThreshold argument - expecting an integer between 1 and 100');
56-
}
57-
58-
if ($criticalThreshold > 100 || $criticalThreshold < 0) {
59-
throw new InvalidArgumentException('Invalid criticalThreshold argument - expecting an integer between 1 and 100');
60-
}
61-
62-
$this->warningThreshold = (int)$warningThreshold;
63-
$this->criticalThreshold = (int)$criticalThreshold;
64-
}
29+
private $apcInfo;
6530

6631
/**
6732
* Perform the check
@@ -83,43 +48,30 @@ public function check()
8348
return new Warning('APC extension is not available');
8449
}
8550

86-
if (!$info = apc_sma_info()) {
51+
if (!$this->apcInfo = apc_sma_info()) {
8752
return new Warning('Unable to retrieve APC memory status information.');
8853
}
8954

90-
$size = $info['num_seg'] * $info['seg_size'];
91-
$available = $info['avail_mem'];
92-
$used = $size - $available;
93-
$percentUsed = ($used / $size) * 100;
94-
$message = sprintf('%.0f%% of available %s memory used.', $percentUsed, $this->formatBytes($size));
95-
96-
if ($percentUsed > $this->criticalThreshold) {
97-
return new Failure($message);
98-
}
99-
100-
if ($percentUsed > $this->warningThreshold) {
101-
return new Warning($message);
102-
}
103-
104-
return new Success($message);
55+
return parent::check();
10556
}
10657

10758
/**
108-
* @param int $bytes
109-
* @return string
59+
* Returns the total memory in bytes
60+
*
61+
* @return int
11062
*/
111-
private function formatBytes($bytes)
63+
protected function getTotalMemory()
11264
{
113-
$size = 'B';
114-
115-
foreach (array('B','KB','MB','GB') as $size) {
116-
if ($bytes < 1024) {
117-
break;
118-
}
119-
120-
$bytes /= 1024;
121-
}
65+
return $this->apcInfo['num_seg'] * $this->apcInfo['seg_size'];
66+
}
12267

123-
return sprintf("%.0f %s", $bytes, $size);
68+
/**
69+
* Returns the used memory in bytes
70+
*
71+
* @return int
72+
*/
73+
protected function getUsedMemory()
74+
{
75+
return $this->getTotalMemory() - $this->apcInfo['avail_mem'];
12476
}
12577
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* @license http://framework.zend.com/license/new-bsd New BSD License
4+
*/
5+
6+
namespace ZendDiagnostics\Check;
7+
8+
use ZendDiagnostics\Result\Failure;
9+
use ZendDiagnostics\Result\Skip;
10+
use ZendDiagnostics\Result\Success;
11+
use ZendDiagnostics\Result\Warning;
12+
13+
/**
14+
* Checks to see if the OpCache memory usage is below warning/critical thresholds
15+
*/
16+
class OpCacheMemory extends AbstractMemoryCheck
17+
{
18+
/**
19+
* OpCache information
20+
*
21+
* @var array
22+
*/
23+
private $opCacheInfo;
24+
25+
/**
26+
* Perform the check
27+
*
28+
* @see \ZendDiagnostics\Check\CheckInterface::check() *
29+
* @return Failure|Skip|Success|Warning
30+
*/
31+
public function check()
32+
{
33+
if (!function_exists('opcache_get_status')) {
34+
return new Warning('Zend OPcache extension is not available');
35+
}
36+
37+
$this->opCacheInfo = opcache_get_status(false);
38+
39+
if (!is_array($this->opCacheInfo) || !array_key_exists('memory_usage', $this->opCacheInfo)) {
40+
return new Warning('Zend OPcache extension is not enabled in this environment');
41+
}
42+
43+
return parent::check();
44+
}
45+
46+
/**
47+
* Return a label describing this test instance.
48+
*
49+
* @return string
50+
*/
51+
public function getLabel()
52+
{
53+
return 'OPcache Memory';
54+
}
55+
56+
/**
57+
* Returns the total memory in bytes
58+
*
59+
* @return int
60+
*/
61+
protected function getTotalMemory()
62+
{
63+
return $this->opCacheInfo['memory_usage']['used_memory'] + $this->opCacheInfo['memory_usage']['free_memory'] + $this->opCacheInfo['memory_usage']['wasted_memory'];
64+
}
65+
66+
/**
67+
* Returns the used memory in bytes
68+
*
69+
* @return int
70+
*/
71+
protected function getUsedMemory()
72+
{
73+
return $this->opCacheInfo['memory_usage']['used_memory'];
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace ZendDiagnosticsTest;
4+
5+
/**
6+
* @author Kevin Bond <[email protected]>
7+
*/
8+
abstract class AbstractMemoryTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @dataProvider InvalidArgumentProvider
12+
*/
13+
public function testInvalidArguments($warningThreshold, $criticalThreshold)
14+
{
15+
$this->setExpectedException('InvalidArgumentException');
16+
$this->createCheck($warningThreshold, $criticalThreshold);
17+
}
18+
19+
public function InvalidArgumentProvider()
20+
{
21+
return array(
22+
array('Not an integer.', 'Not an integer.'),
23+
array(5, 'Not an integer.'),
24+
array('Not an integer.', 100),
25+
array(-10, 100),
26+
array(105, 100),
27+
array(10, -10),
28+
array(10, 105)
29+
);
30+
}
31+
32+
abstract protected function createCheck($warningThreshold, $criticalThreshold);
33+
}

tests/ZendDiagnosticsTest/ApcMemoryTest.php

+3-20
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,10 @@
77
/**
88
* @author Kevin Bond <[email protected]>
99
*/
10-
class ApcMemoryTest extends \PHPUnit_Framework_TestCase
10+
class ApcMemoryTest extends AbstractMemoryTest
1111
{
12-
/**
13-
* @dataProvider InvalidArgumentProvider
14-
*/
15-
public function testInvalidArguments($warningThreshold, $criticalThreshold)
12+
protected function createCheck($warningThreshold, $criticalThreshold)
1613
{
17-
$this->setExpectedException('InvalidArgumentException');
18-
new ApcMemory($warningThreshold, $criticalThreshold);
19-
}
20-
21-
public function InvalidArgumentProvider()
22-
{
23-
return array(
24-
array('Not an integer.', 'Not an integer.'),
25-
array(5, 'Not an integer.'),
26-
array('Not an integer.', 100),
27-
array(-10, 100),
28-
array(105, 100),
29-
array(10, -10),
30-
array(10, 105)
31-
);
14+
return new ApcMemory($warningThreshold, $criticalThreshold);
3215
}
3316
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace ZendDiagnosticsTest;
4+
5+
use ZendDiagnostics\Check\OpCacheMemory;
6+
7+
/**
8+
* @author Kevin Bond <[email protected]>
9+
*/
10+
class OpCacheMemoryTest extends AbstractMemoryTest
11+
{
12+
protected function createCheck($warningThreshold, $criticalThreshold)
13+
{
14+
return new OpCacheMemory($warningThreshold, $criticalThreshold);
15+
}
16+
}

0 commit comments

Comments
 (0)