Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/unit/FilterProfilerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter\Tests\Unit;
4
5 use MediaWiki\Config\ServiceOptions;
6 use MediaWiki\Extension\AbuseFilter\FilterProfiler;
7 use MediaWiki\Title\Title;
8 use MediaWikiUnitTestCase;
9 use Psr\Log\LoggerInterface;
10 use Psr\Log\NullLogger;
11 use TestLogger;
12 use Wikimedia\ObjectCache\HashBagOStuff;
13 use Wikimedia\Stats\IBufferingStatsdDataFactory;
14 use Wikimedia\WRStats\BagOStuffStatsStore;
15 use Wikimedia\WRStats\WRStatsFactory;
16
17 /**
18 * @covers \MediaWiki\Extension\AbuseFilter\FilterProfiler
19 */
20 class FilterProfilerTest extends MediaWikiUnitTestCase {
21
22 private const NULL_FILTER_PROFILE = [
23 'count' => 0,
24 'matches' => 0,
25 'total-time' => 0.0,
26 'total-cond' => 0,
27 ];
28
29 private const NULL_GROUP_PROFILE = [
30 'total' => 0,
31 'overflow' => 0,
32 'total-time' => 0.0,
33 'total-cond' => 0,
34 'matches' => 0,
35 ];
36
37 private function getFilterProfiler( ?LoggerInterface $logger = null ): FilterProfiler {
38 $options = [
39 'AbuseFilterConditionLimit' => 1000,
40 'AbuseFilterSlowFilterRuntimeLimit' => 500,
41 ];
42 return new FilterProfiler(
43 new WRStatsFactory( new BagOStuffStatsStore( new HashBagOStuff() ) ),
44 new ServiceOptions( FilterProfiler::CONSTRUCTOR_OPTIONS, $options ),
45 'wiki',
46 $this->createMock( IBufferingStatsdDataFactory::class ),
47 $logger ?: new NullLogger()
48 );
49 }
50
51 public function testGetFilterProfile_noData() {
52 $profiler = $this->getFilterProfiler();
53 $this->assertSame( self::NULL_FILTER_PROFILE, $profiler->getFilterProfile( 1 ) );
54 }
55
56 public function testGetFilterProfile() {
57 $profiler = $this->getFilterProfiler();
58 $profiler->recordPerFilterProfiling(
59 $this->createMock( Title::class ),
60 [
61 '1' => [
62 'time' => 12.3,
63 'conds' => 5,
64 'result' => false
65 ],
66 ]
67 );
68 $this->assertSame(
69 [
70 'count' => 1,
71 'matches' => 0,
72 'total-time' => 12.3,
73 'total-cond' => 5
74 ],
75 $profiler->getFilterProfile( 1 )
76 );
77 }
78
79 public function testRecordPerFilterProfiling_mergesResults() {
80 $profiler = $this->getFilterProfiler();
81 $profiler->recordPerFilterProfiling(
82 $this->createMock( Title::class ),
83 [
84 '1' => [
85 'time' => 12.5,
86 'conds' => 5,
87 'result' => false
88 ],
89 ]
90 );
91 $profiler->recordPerFilterProfiling(
92 $this->createMock( Title::class ),
93 [
94 '1' => [
95 'time' => 34.5,
96 'conds' => 3,
97 'result' => true
98 ],
99 ]
100 );
101 $this->assertSame(
102 [
103 'count' => 2,
104 'matches' => 1,
105 'total-time' => 47.0,
106 'total-cond' => 8
107 ],
108 $profiler->getFilterProfile( 1 )
109 );
110 }
111
112 public function testRecordPerFilterProfiling_reportsSlowFilter() {
113 $logger = new TestLogger();
114 $logger->setCollect( true );
115 $title = $this->createMock( Title::class );
116 $title->method( 'getPrefixedText' )->willReturn( 'title' );
117
118 $profiler = $this->getFilterProfiler( $logger );
119 $profiler->recordPerFilterProfiling(
120 $title,
121 [
122 '1' => [
123 'time' => 501,
124 'conds' => 20,
125 'result' => false
126 ],
127 ]
128 );
129
130 $found = false;
131 foreach ( $logger->getBuffer() as [ , $entry ] ) {
132 $check = preg_match(
133 "/^Edit filter .+ on .+ is taking longer than expected$/",
134 $entry
135 );
136 if ( $check ) {
137 $found = true;
138 break;
139 }
140 }
141 $this->assertTrue(
142 $found,
143 "Test that FilterProfiler logs the slow filter."
144 );
145 }
146
147 public function testResetFilterProfile() {
148 $profiler = $this->getFilterProfiler();
149 $profiler->recordPerFilterProfiling(
150 $this->createMock( Title::class ),
151 [
152 '1' => [
153 'time' => 12.5,
154 'conds' => 5,
155 'result' => false
156 ],
157 '2' => [
158 'time' => 34.5,
159 'conds' => 3,
160 'result' => true
161 ],
162 ]
163 );
164 $profiler->resetFilterProfile( 1 );
165 $this->assertSame( self::NULL_FILTER_PROFILE, $profiler->getFilterProfile( 1 ) );
166 $this->assertNotSame( self::NULL_FILTER_PROFILE, $profiler->getFilterProfile( 2 ) );
167 }
168
169 public function testGetGroupProfile_noData() {
170 $profiler = $this->getFilterProfiler();
171 $this->assertSame( self::NULL_GROUP_PROFILE, $profiler->getGroupProfile( 'default' ) );
172 }
173
174 /**
175 * @param int $condsUsed
176 * @param float $time
177 * @param bool $matches
178 * @param array $expected
179 * @dataProvider provideRecordStats
180 */
181 public function testRecordStats( int $condsUsed, float $time, bool $matches, array $expected ) {
182 $profiler = $this->getFilterProfiler();
183 $group = 'default';
184 $profiler->recordStats( $group, $condsUsed, $time, $matches );
185 $this->assertSame( $expected, $profiler->getGroupProfile( $group ) );
186 }
187
188 public static function provideRecordStats(): array {
189 return [
190 'No overflow' => [
191 100,
192 333.3,
193 true,
194 [
195 'total' => 1,
196 'overflow' => 0,
197 'total-time' => 333.3,
198 'total-cond' => 100,
199 'matches' => 1
200 ]
201 ],
202 'Overflow' => [
203 10000,
204 20,
205 true,
206 [
207 'total' => 1,
208 'overflow' => 1,
209 'total-time' => 20.0,
210 'total-cond' => 10000,
211 'matches' => 1
212 ]
213 ]
214 ];
215 }
216
217 public function testRecordStats_mergesResults() {
218 $profiler = $this->getFilterProfiler();
219 $profiler->recordStats( 'default', 100, 256.5, true );
220 $profiler->recordStats( 'default', 200, 512.5, false );
221 $this->assertSame(
222 [
223 'total' => 2,
224 'overflow' => 0,
225 'total-time' => 769.0,
226 'total-cond' => 300,
227 'matches' => 1
228 ],
229 $profiler->getGroupProfile( 'default' )
230 );
231 }
232
233 }