File: WRStatsReaderTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (317 lines) | stat: -rw-r--r-- 7,768 bytes parent folder | download
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php

namespace Wikimedia\Tests\WRStats;

use PHPUnit\Framework\TestCase;
use Wikimedia\WRStats\ArrayStatsStore;
use Wikimedia\WRStats\GlobalEntityKey;
use Wikimedia\WRStats\RatePromise;
use Wikimedia\WRStats\TimeRange;
use Wikimedia\WRStats\WRStatsReader;
use Wikimedia\WRStats\WRStatsWriter;

/**
 * @covers \Wikimedia\WRStats\WRStatsReader
 * @covers \Wikimedia\WRStats\SequenceSpec
 * @covers \Wikimedia\WRStats\TimeRange
 * @covers \Wikimedia\WRStats\RatePromise
 * @covers \Wikimedia\WRStats\ArrayStatsStore
 */
class WRStatsReaderTest extends TestCase {
	/** @var ArrayStatsStore|null */
	private $store;

	public function testLatest() {
		$reader = $this->createReader( [] );
		$latest = $reader->latest( 60 );
		$this->assertInstanceOf( TimeRange::class, $latest );
		$this->assertSame( 1940, $latest->start );
		$this->assertSame( 2000, $latest->end );
		$this->assertSame( 60, $latest->getDuration() );
	}

	private function getStore() {
		if ( $this->store === null ) {
			$this->store = new ArrayStatsStore;
		}
		return $this->store;
	}

	private function createReader( $specs ) {
		$reader = new WRStatsReader(
			$this->getStore(),
			$specs,
			'prefix'
		);
		$reader->setCurrentTime( 2000 );
		return $reader;
	}

	private function createWriter( $specs ) {
		$writer = new WRStatsWriter(
			$this->getStore(),
			$specs,
			'prefix'
		);
		$writer->setCurrentTime( 1000 );
		return $writer;
	}

	public function testGetRate() {
		$specs = [
			'test' => [
				'sequences' => [ [
					'timeStep' => 10,
					'expiry' => 1000,
				] ]
			]
		];
		$writer = $this->createWriter( $specs );
		$writer->incr( 'test' );
		$writer->flush();
		$reader = $this->createReader( $specs );
		$reader->setCurrentTime( 1000.001 );
		$rate = $reader->getRate( 'test', null, $reader->latest( 10 ) );
		$this->assertInstanceOf( RatePromise::class, $rate );
		$this->assertSame( 1, $rate->total() );
	}

	public static function provideInterpolationFlatnessMagic() {
		return [
			[ 0.5 ],
			[ 1 ],
			[ 1.1 ],
			[ 2 ],
			[ 3 ],
		];
	}

	/**
	 * If the event rate is constant then the range offset w.r.t. the start of
	 * the window doesn't matter, the interpolated rate will be constant.
	 * @dataProvider provideInterpolationFlatnessMagic
	 */
	public function testInterpolationFlatnessMagic( $rangeDuration ) {
		$specs = [
			'test' => [
				'sequences' => [ [
					'timeStep' => 1,
					'expiry' => 1000,
				] ],
				'resolution' => 0.001
			]
		];
		$writer = $this->createWriter( $specs );
		for ( $t = 0; $t < 10; $t++ ) {
			$writer->setCurrentTime( 1000 + $t );
			$writer->incr( 'test' );

		}
		$writer->flush();

		$reader = $this->createReader( $specs );
		for ( $timeDeciSeconds = 10000; $timeDeciSeconds <= 10011; $timeDeciSeconds++ ) {
			$range = $reader->timeRange(
				$timeDeciSeconds / 10,
				$timeDeciSeconds / 10 + $rangeDuration
			);
			$rate = $reader->getRate( 'test', null, $range );
			$this->assertEqualsWithDelta(
				$rangeDuration,
				$rate->total(),
				1e-9,
				"Flatness at t = {$range->start}"
			);
		}
	}

	public static function provideInterpolationLinearity() {
		return [
			[ 1000.0, 1.0 ],
			[ 1000.1, 1.1 ],
			[ 1000.9, 1.9 ],
			[ 1001.0, 2.0 ]
		];
	}

	/**
	 * Test interpolation involving two unequal buckets
	 * @dataProvider provideInterpolationLinearity
	 */
	public function testInterpolationLinearity( $start, $expected ) {
		$specs = [
			'test' => [
				'sequences' => [ [
					'timeStep' => 1,
					'expiry' => 1000,
				] ],
				'resolution' => 0.001
			]
		];
		$writer = $this->createWriter( $specs );
		$writer->setCurrentTime( 1000 );
		$writer->incr( 'test' );
		$writer->setCurrentTime( 1001 );
		$writer->incr( 'test', null, 2 );
		$writer->flush();

		$reader = $this->createReader( $specs );
		$range = $reader->timeRange( $start, $start + 1 );
		$rate = $reader->getRate( 'test', null, $range );
		$this->assertEqualsWithDelta( $expected, $rate->total(), 1e-9 );
	}

	public static function provideCurrentTimeAdjustment() {
		return [
			[ 1000.1, 1000.1, 1 ],
			[ 1000.2, 1000.1, 0.5 ],
			[ 1000.5, 1000.1, 0.2 ],
			[ 1001, 1000.1, 0.1 ],
			[ 1002, 1000.1, 0.1 ],
		];
	}

	/**
	 * Test the interpolation adjustment which occurs when the reader's current
	 * time falls within the last relevant bucket
	 *
	 * @dataProvider provideCurrentTimeAdjustment
	 */
	public function testCurrentTimeAdjustment( $currentTime, $rangeEnd, $expected ) {
		$specs = [
			'test' => [
				'sequences' => [ [
					'timeStep' => 1,
					'expiry' => 1000,
				] ],
				'resolution' => 0.001
			]
		];
		$writer = $this->createWriter( $specs );
		$writer->incr( 'test' );
		$writer->flush();

		$reader = $this->createReader( $specs );
		$reader->setCurrentTime( $currentTime );
		$range = $reader->timeRange( 999, $rangeEnd );
		$rate = $reader->getRate( 'test', null, $range );
		$this->assertEqualsWithDelta( $expected, $rate->total(), 1e-9 );
	}

	/**
	 * When the metric has multiple sequences, we select the one with the
	 * shortest expiry which contains the whole range
	 */
	public function testMultiSequence() {
		$specs = [
			'test' => [
				'sequences' => [
					[
						'timeStep' => 1,
						'expiry' => 10,
					],
					[
						'timeStep' => 10,
						'expiry' => 100,
					]
				],
				'resolution' => 0.001
			]
		];
		$store = $this->getStore();
		$store->incr(
			[
				'local:prefix:test::1999' => 1000,
				'local:prefix:test:s1:199' => 555000
			],
			1000
		);
		$reader = $this->createReader( $specs );
		$range = $reader->timeRange( 1990, 2000 );
		$rate = $reader->getRate( 'test', null, $range );
		$this->assertSame( 1.0, $rate->total() );

		$range = $reader->timeRange( 1900, 2000 );
		$rate = $reader->getRate( 'test', null, $range );
		$this->assertSame( 555.0, $rate->total() );
	}

	/**
	 * Test multiple entity keys
	 */
	public function testEntityKey() {
		$specs = [
			'test' => [
				'sequences' => [ [
					'timeStep' => 1,
					'expiry' => 1000,
				] ],
				'resolution' => 1
			]
		];
		$writer = $this->createWriter( $specs );
		$entity1 = new GlobalEntityKey( [ 'entity', 1 ] );
		$entity2 = new GlobalEntityKey( [ 'entity', 3 ] );
		$writer->incr( 'test', $entity1, 1 );
		$writer->incr( 'test', $entity2, 2 );
		$writer->flush();

		$reader = $this->createReader( $specs );
		$range = $reader->timeRange( 1000, 1001 );

		$rate0 = $reader->getRate( 'test', null, $range );
		$this->assertSame( 0, $rate0->total() );

		$rate1 = $reader->getRate( 'test', $entity1, $range );
		$this->assertSame( 1, $rate1->total() );

		$rate2 = $reader->getRate( 'test', $entity2, $range );
		$this->assertSame( 2, $rate2->total() );
	}

	/**
	 * Test the batch rate resolve functions total() and per*()
	 */
	public function testBatchResolve() {
		$specs = [
			'test' => [
				'sequences' => [ [
					'timeStep' => 10,
					'expiry' => 1000,
				] ],
				'resolution' => 0.001
			]
		];
		$writer = $this->createWriter( $specs );
		$writer->incr( 'test', null, 10 );
		$writer->flush();

		$reader = $this->createReader( $specs );
		$reader->setCurrentTime( 1010 );
		$rates = [
			1 => $reader->getRate( 'test', null, $reader->latest( 1 ) ),
			2 => $reader->getRate( 'test', null, $reader->latest( 2 ) ),
			3 => $reader->getRate( 'test', null, $reader->latest( 3 ) ),
		];
		$this->assertSame(
			[ 1 => 1.0, 2 => 2.0, 3 => 3.0 ],
			$reader->total( $rates )
		);
		$this->assertSame(
			[ 1 => 1.0, 2 => 1.0, 3 => 1.0 ],
			$reader->perSecond( $rates )
		);
		$this->assertSame(
			[ 1 => 60.0, 2 => 60.0, 3 => 60.0 ],
			$reader->perMinute( $rates )
		);
		$this->assertSame(
			[ 1 => 3600.0, 2 => 3600.0, 3 => 3600.0 ],
			$reader->perHour( $rates )
		);
		$this->assertSame(
			[ 1 => 86400.0, 2 => 86400.0, 3 => 86400.0 ],
			$reader->perDay( $rates )
		);
	}
}