5 use Friendica\Test\MockedTest;
6 use Friendica\Util\Profiler;
7 use Mockery\MockInterface;
8 use Psr\Log\LoggerInterface;
10 class ProfilerTest extends MockedTest
13 * @var LoggerInterface|MockInterface
17 protected function setUp()
21 $this->logger = \Mockery::mock('Psr\Log\LoggerInterface');
25 * Test the Profiler setup
27 public function testSetUp()
29 $profiler = new Profiler(true, true);
33 * A dataset for different profiling settings
36 public function dataPerformance()
40 'timestamp' => time(),
42 'functions' => ['test', 'it'],
45 'timestamp' => time(),
46 'name' => 'database_write',
47 'functions' => ['test', 'it2'],
50 'timestamp' => time(),
52 'functions' => ['test', 'it3'],
55 'timestamp' => time(),
56 'name' => 'cache_write',
57 'functions' => ['test', 'it4'],
60 'timestamp' => time(),
62 'functions' => ['test', 'it5'],
65 'timestamp' => time(),
70 'timestamp' => time(),
71 'name' => 'rendering',
72 'functions' => ['test', 'it7'],
75 'timestamp' => time(),
77 'functions' => ['test', 'it8'],
80 'timestamp' => time(),
82 'functions' => ['test'],
84 // This one isn't set during reset
86 'timestamp' => time(),
88 'functions' => ['test'],
94 * Test the Profiler savetimestamp
95 * @dataProvider dataPerformance
97 public function testSaveTimestamp($timestamp, $name, array $functions)
99 $profiler = new Profiler(true, true);
101 foreach ($functions as $function) {
102 $profiler->saveTimestamp($timestamp, $name, $function);
105 $this->assertGreaterThanOrEqual(0, $profiler->get($name));
109 * Test the Profiler reset
110 * @dataProvider dataPerformance
112 public function testReset($timestamp, $name, array $functions)
114 $profiler = new Profiler(true, true);
116 $profiler->saveTimestamp($timestamp, $name);
119 $this->assertEquals(0, $profiler->get($name));
122 public function dataBig()
128 'timestamp' => time(),
129 'name' => 'database',
130 'functions' => ['test', 'it'],
132 'database_write' => [
133 'timestamp' => time(),
134 'name' => 'database_write',
135 'functions' => ['test', 'it2'],
138 'timestamp' => time(),
140 'functions' => ['test', 'it3'],
143 'timestamp' => time(),
144 'name' => 'cache_write',
145 'functions' => ['test', 'it4'],
148 'timestamp' => time(),
150 'functions' => ['test', 'it5'],
158 * Test the output of the Profiler
159 * @dataProvider dataBig
161 public function testSaveLog($data)
164 ->shouldReceive('info')
165 ->with('test', \Mockery::any())
168 ->shouldReceive('info')
171 $profiler = new Profiler(true, true);
173 foreach ($data as $perf => $items) {
174 foreach ($items['functions'] as $function) {
175 $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
179 $profiler->saveLog($this->logger, 'test');