3 namespace Friendica\Test\src\Util;
5 use Friendica\Core\Config\Cache\ConfigCache;
6 use Friendica\Core\Config\IConfiguration;
7 use Friendica\Test\MockedTest;
8 use Friendica\Util\Profiler;
9 use Mockery\MockInterface;
10 use Psr\Log\LoggerInterface;
12 class ProfilerTest extends MockedTest
15 * @var LoggerInterface|MockInterface
19 protected function setUp()
23 $this->logger = \Mockery::mock(LoggerInterface::class);
27 * Test the Profiler setup
29 public function testSetUp()
31 $configCache = \Mockery::mock(ConfigCache::class);
32 $configCache->shouldReceive('get')
36 $profiler = new Profiler($configCache);
40 * A dataset for different profiling settings
43 public function dataPerformance()
47 'timestamp' => time(),
49 'functions' => ['test', 'it'],
52 'timestamp' => time(),
53 'name' => 'database_write',
54 'functions' => ['test', 'it2'],
57 'timestamp' => time(),
59 'functions' => ['test', 'it3'],
62 'timestamp' => time(),
63 'name' => 'cache_write',
64 'functions' => ['test', 'it4'],
67 'timestamp' => time(),
69 'functions' => ['test', 'it5'],
72 'timestamp' => time(),
77 'timestamp' => time(),
78 'name' => 'rendering',
79 'functions' => ['test', 'it7'],
82 'timestamp' => time(),
84 'functions' => ['test', 'it8'],
87 'timestamp' => time(),
89 'functions' => ['test'],
91 // This one isn't set during reset
93 'timestamp' => time(),
95 'functions' => ['test'],
101 * Test the Profiler savetimestamp
102 * @dataProvider dataPerformance
104 public function testSaveTimestamp($timestamp, $name, array $functions)
106 $configCache = \Mockery::mock(ConfigCache::class);
107 $configCache->shouldReceive('get')
112 $profiler = new Profiler($configCache);
114 foreach ($functions as $function) {
115 $profiler->saveTimestamp($timestamp, $name, $function);
118 $this->assertGreaterThanOrEqual(0, $profiler->get($name));
122 * Test the Profiler reset
123 * @dataProvider dataPerformance
125 public function testReset($timestamp, $name, array $functions)
127 $configCache = \Mockery::mock(ConfigCache::class);
128 $configCache->shouldReceive('get')
133 $profiler = new Profiler($configCache);
135 $profiler->saveTimestamp($timestamp, $name);
138 $this->assertEquals(0, $profiler->get($name));
141 public function dataBig()
147 'timestamp' => time(),
148 'name' => 'database',
149 'functions' => ['test', 'it'],
151 'database_write' => [
152 'timestamp' => time(),
153 'name' => 'database_write',
154 'functions' => ['test', 'it2'],
157 'timestamp' => time(),
159 'functions' => ['test', 'it3'],
162 'timestamp' => time(),
163 'name' => 'cache_write',
164 'functions' => ['test', 'it4'],
167 'timestamp' => time(),
169 'functions' => ['test', 'it5'],
177 * Test the output of the Profiler
178 * @dataProvider dataBig
180 public function testSaveLog($data)
183 ->shouldReceive('info')
184 ->with('test', \Mockery::any())
187 ->shouldReceive('info')
190 $configCache = \Mockery::mock(ConfigCache::class);
191 $configCache->shouldReceive('get')
196 $profiler = new Profiler($configCache);
198 foreach ($data as $perf => $items) {
199 foreach ($items['functions'] as $function) {
200 $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
204 $profiler->saveLog($this->logger, 'test');
206 $output = $profiler->getRendertimeString();
208 foreach ($data as $perf => $items) {
209 foreach ($items['functions'] as $function) {
210 // assert that the output contains the functions
211 $this->assertRegExp('/' . $function . ': \d+/', $output);
217 * Test different enable and disable states of the profiler
219 public function testEnableDisable()
221 $configCache = \Mockery::mock(ConfigCache::class);
222 $configCache->shouldReceive('get')
223 ->with('system', 'profiler')
226 $configCache->shouldReceive('get')
227 ->with('rendertime', 'callstack')
231 $profiler = new Profiler($configCache);
233 $this->assertFalse($profiler->isRendertime());
234 $this->assertEmpty($profiler->getRendertimeString());
236 $profiler->saveTimestamp(time(), 'network', 'test1');
238 $config = \Mockery::mock(IConfiguration::class);
239 $config->shouldReceive('get')
240 ->with('system', 'profiler')
243 $config->shouldReceive('get')
244 ->with('rendertime', 'callstack')
248 $profiler->update($config);
250 $this->assertFalse($profiler->isRendertime());
251 $this->assertEmpty($profiler->getRendertimeString());
253 $config->shouldReceive('get')
254 ->with('system', 'profiler')
257 $config->shouldReceive('get')
258 ->with('rendertime', 'callstack')
262 $profiler->update($config);
264 $profiler->saveTimestamp(time(), 'database', 'test2');
266 $this->assertTrue($profiler->isRendertime());
267 $output = $profiler->getRendertimeString();
268 $this->assertRegExp('/test1: \d+/', $output);
269 $this->assertRegExp('/test2: \d+/', $output);