This repository has been archived by the owner on Apr 8, 2018. It is now read-only.
/
PerformanceTest.php
77 lines (67 loc) · 2.37 KB
/
PerformanceTest.php
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace PetrKnap\Php\FileStorage\Test;
use League\Flysystem\Adapter\Local;
use PetrKnap\Php\FileStorage\FileSystem;
use PetrKnap\Php\Profiler\SimpleProfiler;
class PerformanceTest extends AbstractTestCase
{
/**
* @dataProvider performanceIsNotIntrusiveDataProvider
* @param FileSystem $fileSystem
* @param int $from
* @param int $to
*/
public function testPerformanceIsNotIntrusive(FileSystem $fileSystem, $from, $to)
{
$profilerWasEnabled = SimpleProfiler::start();
if (!$profilerWasEnabled) {
SimpleProfiler::enable();
}
#region Build storage
for ($i = $from; $i < $to; $i++) {
$file = "/file_{$i}.tmp";
#region Create file
SimpleProfiler::start();
$fileSystem->write($file, null);
$profile = SimpleProfiler::finish();
$this->assertLessThanOrEqual(5, $profile->absoluteDuration);
#endregion
#region Write content
SimpleProfiler::start();
$fileSystem->update($file, sha1($i, true));
$fileSystem->update($file, md5($i, true), ["append" => true]);
$profile = SimpleProfiler::finish();
$this->assertLessThanOrEqual(10, $profile->absoluteDuration);
#endregion
#region Read content
SimpleProfiler::start();
$fileSystem->read($file);
$profile = SimpleProfiler::finish();
$this->assertLessThanOrEqual(5, $profile->absoluteDuration);
#endregion
}
#endregion
#region Iterate all files
SimpleProfiler::start();
/** @noinspection PhpUnusedLocalVariableInspection */
foreach ($fileSystem->listContents() as $unused) ;
$profile = SimpleProfiler::finish();
$this->assertLessThanOrEqual(5 * $to, $profile->absoluteDuration);
#endregion
if (!$profilerWasEnabled) {
SimpleProfiler::disable();
}
SimpleProfiler::finish();
}
public function performanceIsNotIntrusiveDataProvider()
{
$iMax = 2048;
$step = 512;
$output = [];
$fileSystem = new FileSystem(new Local($this->getTemporaryDirectory()));
for ($i = 0; $i < $iMax; $i += $step) {
$output[] = [$fileSystem, $i, $i + $step];
}
return $output;
}
}