This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/4813' into develop
- Loading branch information
Showing
2 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Log\Filter; | ||
|
||
use Zend\Log\Exception; | ||
use Zend\Log\Filter\FilterInterface; | ||
|
||
class Sample implements FilterInterface | ||
{ | ||
/** | ||
* Sample rate [0-1]. | ||
* | ||
* @var float | ||
*/ | ||
protected $sampleRate; | ||
|
||
/** | ||
* Filters logging by sample rate. | ||
* | ||
* Sample rate must be a float number between 0 and 1 included. | ||
* If 0.5, only half of the values will be logged. | ||
* If 0.1 only 1 among 10 values will be logged. | ||
* | ||
* @param float|int $samplerate Sample rate [0-1]. | ||
* @return Priority | ||
* @throws Exception\InvalidArgumentException | ||
*/ | ||
public function __construct($sampleRate = 1) | ||
{ | ||
if (! is_numeric($sampleRate)) { | ||
throw new Exception\InvalidArgumentException(sprintf( | ||
'Sample rate must be numeric, received "%s"', | ||
gettype($sampleRate) | ||
)); | ||
} | ||
|
||
$this->sampleRate = (float) $sampleRate; | ||
} | ||
|
||
/** | ||
* Returns TRUE to accept the message, FALSE to block it. | ||
* | ||
* @param array $event event data | ||
* @return bool Accepted ? | ||
*/ | ||
public function filter(array $event) | ||
{ | ||
return (mt_rand() / mt_getrandmax()) <= $this->sampleRate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @package Zend_Log | ||
*/ | ||
|
||
namespace ZendTest\Log\Filter; | ||
|
||
use Zend\Log\Filter\Sample; | ||
|
||
/** | ||
* @category Zend | ||
* @package Zend_Log | ||
* @subpackage UnitTests | ||
* @group Zend_Log | ||
*/ | ||
class SampleTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testConstructorThrowsOnInvalidSampleRate() | ||
{ | ||
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException', 'must be numeric'); | ||
new Sample('bar'); | ||
} | ||
|
||
public function testSampleLimit0() | ||
{ | ||
// Should log nothing. | ||
$filter = new Sample(0); | ||
|
||
// Since sampling is a random process, let's test several times. | ||
$ret = false; | ||
for ($i = 0; $i < 100; $i ++) { | ||
if ($filter->filter(array())) { | ||
break; | ||
$ret = true; | ||
} | ||
} | ||
|
||
$this->assertFalse($ret); | ||
} | ||
|
||
public function testSampleLimit1() | ||
{ | ||
// Should log all events. | ||
$filter = new Sample(1); | ||
|
||
// Since sampling is a random process, let's test several times. | ||
$ret = true; | ||
for ($i = 0; $i < 100; $i ++) { | ||
if (! $filter->filter(array())) { | ||
break; | ||
$ret = false; | ||
} | ||
} | ||
|
||
$this->assertTrue($ret); | ||
} | ||
} |