| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests\WRStats; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Wikimedia\WRStats\ArrayStatsStore; |
| 7 |
use Wikimedia\WRStats\GlobalEntityKey; |
| 8 |
use Wikimedia\WRStats\RatePromise; |
| 9 |
use Wikimedia\WRStats\TimeRange; |
| 10 |
use Wikimedia\WRStats\WRStatsReader; |
| 11 |
use Wikimedia\WRStats\WRStatsWriter; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class WRStatsReaderTest extends TestCase { |
| 21 |
|
| 22 |
private $store; |
| 23 |
|
| 24 |
public function testLatest() { |
| 25 |
$reader = $this->createReader( [] ); |
| 26 |
$latest = $reader->latest( 60 ); |
| 27 |
$this->assertInstanceOf( TimeRange::class, $latest ); |
| 28 |
$this->assertSame( 1940, $latest->start ); |
| 29 |
$this->assertSame( 2000, $latest->end ); |
| 30 |
$this->assertSame( 60, $latest->getDuration() ); |
| 31 |
} |
| 32 |
|
| 33 |
private function getStore() { |
| 34 |
if ( $this->store === null ) { |
| 35 |
$this->store = new ArrayStatsStore; |
| 36 |
} |
| 37 |
return $this->store; |
| 38 |
} |
| 39 |
|
| 40 |
private function createReader( $specs ) { |
| 41 |
$reader = new WRStatsReader( |
| 42 |
$this->getStore(), |
| 43 |
$specs, |
| 44 |
'prefix' |
| 45 |
); |
| 46 |
$reader->setCurrentTime( 2000 ); |
| 47 |
return $reader; |
| 48 |
} |
| 49 |
|
| 50 |
private function createWriter( $specs ) { |
| 51 |
$writer = new WRStatsWriter( |
| 52 |
$this->getStore(), |
| 53 |
$specs, |
| 54 |
'prefix' |
| 55 |
); |
| 56 |
$writer->setCurrentTime( 1000 ); |
| 57 |
return $writer; |
| 58 |
} |
| 59 |
|
| 60 |
public function testGetRate() { |
| 61 |
$specs = [ |
| 62 |
'test' => [ |
| 63 |
'sequences' => [ [ |
| 64 |
'timeStep' => 10, |
| 65 |
'expiry' => 1000, |
| 66 |
] ] |
| 67 |
] |
| 68 |
]; |
| 69 |
$writer = $this->createWriter( $specs ); |
| 70 |
$writer->incr( 'test' ); |
| 71 |
$writer->flush(); |
| 72 |
$reader = $this->createReader( $specs ); |
| 73 |
$reader->setCurrentTime( 1000.001 ); |
| 74 |
$rate = $reader->getRate( 'test', null, $reader->latest( 10 ) ); |
| 75 |
$this->assertInstanceOf( RatePromise::class, $rate ); |
| 76 |
$this->assertSame( 1, $rate->total() ); |
| 77 |
} |
| 78 |
|
| 79 |
public static function provideInterpolationFlatnessMagic() { |
| 80 |
return [ |
| 81 |
[ 0.5 ], |
| 82 |
[ 1 ], |
| 83 |
[ 1.1 ], |
| 84 |
[ 2 ], |
| 85 |
[ 3 ], |
| 86 |
]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function testInterpolationFlatnessMagic( $rangeDuration ) { |
| 95 |
$specs = [ |
| 96 |
'test' => [ |
| 97 |
'sequences' => [ [ |
| 98 |
'timeStep' => 1, |
| 99 |
'expiry' => 1000, |
| 100 |
] ], |
| 101 |
'resolution' => 0.001 |
| 102 |
] |
| 103 |
]; |
| 104 |
$writer = $this->createWriter( $specs ); |
| 105 |
for ( $t = 0; $t < 10; $t++ ) { |
| 106 |
$writer->setCurrentTime( 1000 + $t ); |
| 107 |
$writer->incr( 'test' ); |
| 108 |
|
| 109 |
} |
| 110 |
$writer->flush(); |
| 111 |
|
| 112 |
$reader = $this->createReader( $specs ); |
| 113 |
for ( $timeDeciSeconds = 10000; $timeDeciSeconds <= 10011; $timeDeciSeconds++ ) { |
| 114 |
$range = $reader->timeRange( |
| 115 |
$timeDeciSeconds / 10, |
| 116 |
$timeDeciSeconds / 10 + $rangeDuration |
| 117 |
); |
| 118 |
$rate = $reader->getRate( 'test', null, $range ); |
| 119 |
$this->assertEqualsWithDelta( |
| 120 |
$rangeDuration, |
| 121 |
$rate->total(), |
| 122 |
1e-9, |
| 123 |
"Flatness at t = {$range->start}" |
| 124 |
); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
public static function provideInterpolationLinearity() { |
| 129 |
return [ |
| 130 |
[ 1000.0, 1.0 ], |
| 131 |
[ 1000.1, 1.1 ], |
| 132 |
[ 1000.9, 1.9 ], |
| 133 |
[ 1001.0, 2.0 ] |
| 134 |
]; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public function testInterpolationLinearity( $start, $expected ) { |
| 142 |
$specs = [ |
| 143 |
'test' => [ |
| 144 |
'sequences' => [ [ |
| 145 |
'timeStep' => 1, |
| 146 |
'expiry' => 1000, |
| 147 |
] ], |
| 148 |
'resolution' => 0.001 |
| 149 |
] |
| 150 |
]; |
| 151 |
$writer = $this->createWriter( $specs ); |
| 152 |
$writer->setCurrentTime( 1000 ); |
| 153 |
$writer->incr( 'test' ); |
| 154 |
$writer->setCurrentTime( 1001 ); |
| 155 |
$writer->incr( 'test', null, 2 ); |
| 156 |
$writer->flush(); |
| 157 |
|
| 158 |
$reader = $this->createReader( $specs ); |
| 159 |
$range = $reader->timeRange( $start, $start + 1 ); |
| 160 |
$rate = $reader->getRate( 'test', null, $range ); |
| 161 |
$this->assertEqualsWithDelta( $expected, $rate->total(), 1e-9 ); |
| 162 |
} |
| 163 |
|
| 164 |
public static function provideCurrentTimeAdjustment() { |
| 165 |
return [ |
| 166 |
[ 1000.1, 1000.1, 1 ], |
| 167 |
[ 1000.2, 1000.1, 0.5 ], |
| 168 |
[ 1000.5, 1000.1, 0.2 ], |
| 169 |
[ 1001, 1000.1, 0.1 ], |
| 170 |
[ 1002, 1000.1, 0.1 ], |
| 171 |
]; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public function testCurrentTimeAdjustment( $currentTime, $rangeEnd, $expected ) { |
| 181 |
$specs = [ |
| 182 |
'test' => [ |
| 183 |
'sequences' => [ [ |
| 184 |
'timeStep' => 1, |
| 185 |
'expiry' => 1000, |
| 186 |
] ], |
| 187 |
'resolution' => 0.001 |
| 188 |
] |
| 189 |
]; |
| 190 |
$writer = $this->createWriter( $specs ); |
| 191 |
$writer->incr( 'test' ); |
| 192 |
$writer->flush(); |
| 193 |
|
| 194 |
$reader = $this->createReader( $specs ); |
| 195 |
$reader->setCurrentTime( $currentTime ); |
| 196 |
$range = $reader->timeRange( 999, $rangeEnd ); |
| 197 |
$rate = $reader->getRate( 'test', null, $range ); |
| 198 |
$this->assertEqualsWithDelta( $expected, $rate->total(), 1e-9 ); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public function testMultiSequence() { |
| 206 |
$specs = [ |
| 207 |
'test' => [ |
| 208 |
'sequences' => [ |
| 209 |
[ |
| 210 |
'timeStep' => 1, |
| 211 |
'expiry' => 10, |
| 212 |
], |
| 213 |
[ |
| 214 |
'timeStep' => 10, |
| 215 |
'expiry' => 100, |
| 216 |
] |
| 217 |
], |
| 218 |
'resolution' => 0.001 |
| 219 |
] |
| 220 |
]; |
| 221 |
$store = $this->getStore(); |
| 222 |
$store->incr( |
| 223 |
[ |
| 224 |
'local:prefix:test::1999' => 1000, |
| 225 |
'local:prefix:test:s1:199' => 555000 |
| 226 |
], |
| 227 |
1000 |
| 228 |
); |
| 229 |
$reader = $this->createReader( $specs ); |
| 230 |
$range = $reader->timeRange( 1990, 2000 ); |
| 231 |
$rate = $reader->getRate( 'test', null, $range ); |
| 232 |
$this->assertSame( 1.0, $rate->total() ); |
| 233 |
|
| 234 |
$range = $reader->timeRange( 1900, 2000 ); |
| 235 |
$rate = $reader->getRate( 'test', null, $range ); |
| 236 |
$this->assertSame( 555.0, $rate->total() ); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public function testEntityKey() { |
| 243 |
$specs = [ |
| 244 |
'test' => [ |
| 245 |
'sequences' => [ [ |
| 246 |
'timeStep' => 1, |
| 247 |
'expiry' => 1000, |
| 248 |
] ], |
| 249 |
'resolution' => 1 |
| 250 |
] |
| 251 |
]; |
| 252 |
$writer = $this->createWriter( $specs ); |
| 253 |
$entity1 = new GlobalEntityKey( [ 'entity', 1 ] ); |
| 254 |
$entity2 = new GlobalEntityKey( [ 'entity', 3 ] ); |
| 255 |
$writer->incr( 'test', $entity1, 1 ); |
| 256 |
$writer->incr( 'test', $entity2, 2 ); |
| 257 |
$writer->flush(); |
| 258 |
|
| 259 |
$reader = $this->createReader( $specs ); |
| 260 |
$range = $reader->timeRange( 1000, 1001 ); |
| 261 |
|
| 262 |
$rate0 = $reader->getRate( 'test', null, $range ); |
| 263 |
$this->assertSame( 0, $rate0->total() ); |
| 264 |
|
| 265 |
$rate1 = $reader->getRate( 'test', $entity1, $range ); |
| 266 |
$this->assertSame( 1, $rate1->total() ); |
| 267 |
|
| 268 |
$rate2 = $reader->getRate( 'test', $entity2, $range ); |
| 269 |
$this->assertSame( 2, $rate2->total() ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function testBatchResolve() { |
| 276 |
$specs = [ |
| 277 |
'test' => [ |
| 278 |
'sequences' => [ [ |
| 279 |
'timeStep' => 10, |
| 280 |
'expiry' => 1000, |
| 281 |
] ], |
| 282 |
'resolution' => 0.001 |
| 283 |
] |
| 284 |
]; |
| 285 |
$writer = $this->createWriter( $specs ); |
| 286 |
$writer->incr( 'test', null, 10 ); |
| 287 |
$writer->flush(); |
| 288 |
|
| 289 |
$reader = $this->createReader( $specs ); |
| 290 |
$reader->setCurrentTime( 1010 ); |
| 291 |
$rates = [ |
| 292 |
1 => $reader->getRate( 'test', null, $reader->latest( 1 ) ), |
| 293 |
2 => $reader->getRate( 'test', null, $reader->latest( 2 ) ), |
| 294 |
3 => $reader->getRate( 'test', null, $reader->latest( 3 ) ), |
| 295 |
]; |
| 296 |
$this->assertSame( |
| 297 |
[ 1 => 1.0, 2 => 2.0, 3 => 3.0 ], |
| 298 |
$reader->total( $rates ) |
| 299 |
); |
| 300 |
$this->assertSame( |
| 301 |
[ 1 => 1.0, 2 => 1.0, 3 => 1.0 ], |
| 302 |
$reader->perSecond( $rates ) |
| 303 |
); |
| 304 |
$this->assertSame( |
| 305 |
[ 1 => 60.0, 2 => 60.0, 3 => 60.0 ], |
| 306 |
$reader->perMinute( $rates ) |
| 307 |
); |
| 308 |
$this->assertSame( |
| 309 |
[ 1 => 3600.0, 2 => 3600.0, 3 => 3600.0 ], |
| 310 |
$reader->perHour( $rates ) |
| 311 |
); |
| 312 |
$this->assertSame( |
| 313 |
[ 1 => 86400.0, 2 => 86400.0, 3 => 86400.0 ], |
| 314 |
$reader->perDay( $rates ) |
| 315 |
); |
| 316 |
} |
| 317 |
} |