| 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\WRStatsWriter; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class WRStatsWriterTest extends TestCase { |
| 20 |
public static function provideIncrFlush() { |
| 21 |
$specs1 = [ |
| 22 |
'test' => [ |
| 23 |
'sequences' => [ [ |
| 24 |
'timeStep' => 100, |
| 25 |
'expiry' => 1000, |
| 26 |
] ], |
| 27 |
'resolution' => 1 |
| 28 |
] |
| 29 |
]; |
| 30 |
$specs2 = [ |
| 31 |
'int' => [ |
| 32 |
'sequences' => [ [ |
| 33 |
'timeStep' => 100, |
| 34 |
'expiry' => 1000, |
| 35 |
] ], |
| 36 |
'resolution' => 10 |
| 37 |
], |
| 38 |
'decimal' => [ |
| 39 |
'sequences' => [ [ |
| 40 |
'timeStep' => 200, |
| 41 |
'expiry' => 2000, |
| 42 |
] ], |
| 43 |
'resolution' => 0.1 |
| 44 |
] |
| 45 |
]; |
| 46 |
$specs3 = [ |
| 47 |
'test' => [ |
| 48 |
'sequences' => [ |
| 49 |
[ |
| 50 |
'timeStep' => 10, |
| 51 |
'expiry' => 100, |
| 52 |
], |
| 53 |
[ |
| 54 |
'timeStep' => 100, |
| 55 |
'expiry' => 1000 |
| 56 |
], |
| 57 |
[ |
| 58 |
'name' => 'L', |
| 59 |
'timeStep' => 1000, |
| 60 |
'expiry' => 10000 |
| 61 |
] |
| 62 |
] |
| 63 |
] |
| 64 |
]; |
| 65 |
return [ |
| 66 |
'no-op' => [ |
| 67 |
$specs1, |
| 68 |
false, |
| 69 |
[], |
| 70 |
[] |
| 71 |
], |
| 72 |
'single incr' => [ |
| 73 |
$specs1, |
| 74 |
false, |
| 75 |
[ [ 'test', 1, 1000 ] ], |
| 76 |
[ 'local:prefix:test::10' => [ 1, 1100 ] ] |
| 77 |
], |
| 78 |
'single incr global' => [ |
| 79 |
$specs1, |
| 80 |
true, |
| 81 |
[ [ 'test', 1, 1000 ] ], |
| 82 |
[ 'global:prefix:test::10' => [ 1, 1100 ] ] |
| 83 |
], |
| 84 |
'two incrs' => [ |
| 85 |
$specs1, |
| 86 |
false, |
| 87 |
[ |
| 88 |
[ 'test', 1, 1000 ], |
| 89 |
[ 'test', 1, 1000 ] |
| 90 |
], |
| 91 |
[ 'local:prefix:test::10' => [ 2, 1100 ] ] |
| 92 |
], |
| 93 |
'incr by amount >1' => [ |
| 94 |
$specs1, |
| 95 |
false, |
| 96 |
[ |
| 97 |
[ 'test', 5, 1000 ], |
| 98 |
[ 'test', 7, 1000 ] |
| 99 |
], |
| 100 |
[ 'local:prefix:test::10' => [ 12, 1100 ] ] |
| 101 |
], |
| 102 |
'incr in different time buckets' => [ |
| 103 |
$specs1, |
| 104 |
false, |
| 105 |
[ |
| 106 |
[ 'test', 1, 1000 ], |
| 107 |
[ 'test', 2, 1100 ], |
| 108 |
], |
| 109 |
[ |
| 110 |
'local:prefix:test::10' => [ 1, 1100 ], |
| 111 |
'local:prefix:test::11' => [ 2, 1100 ] |
| 112 |
] |
| 113 |
], |
| 114 |
'resolution > 1' => [ |
| 115 |
$specs2, |
| 116 |
false, |
| 117 |
[ |
| 118 |
[ 'int', 12.5, 1000 ], |
| 119 |
], |
| 120 |
[ |
| 121 |
'local:prefix:int::10' => [ 1, 1100 ], |
| 122 |
] |
| 123 |
], |
| 124 |
'resolution < 1' => [ |
| 125 |
$specs2, |
| 126 |
false, |
| 127 |
[ |
| 128 |
[ 'decimal', 1.33, 1000 ], |
| 129 |
], |
| 130 |
[ |
| 131 |
'local:prefix:decimal::5' => [ 13, 2200 ], |
| 132 |
] |
| 133 |
], |
| 134 |
'mixed expiry, resolution' => [ |
| 135 |
$specs2, |
| 136 |
false, |
| 137 |
[ |
| 138 |
[ 'int', 12.5, 1000 ], |
| 139 |
[ 'decimal', 1.33, 1000 ], |
| 140 |
], |
| 141 |
[ |
| 142 |
'local:prefix:int::10' => [ 1, 1100 ], |
| 143 |
'local:prefix:decimal::5' => [ 13, 2200 ], |
| 144 |
] |
| 145 |
], |
| 146 |
'multi-sequence' => [ |
| 147 |
$specs3, |
| 148 |
false, |
| 149 |
[ |
| 150 |
[ 'test', 1, 1000 ], |
| 151 |
[ 'test', 1, 1010 ] |
| 152 |
], |
| 153 |
[ |
| 154 |
'local:prefix:test::100' => [ 1, 110 ], |
| 155 |
'local:prefix:test::101' => [ 1, 110 ], |
| 156 |
'local:prefix:test:s1:10' => [ 2, 1100 ], |
| 157 |
'local:prefix:test:L:1' => [ 2, 11000 ], |
| 158 |
] |
| 159 |
] |
| 160 |
]; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public function testIncrFlush( $specs, $global, $incrOps, $expected ) { |
| 167 |
$store = new ArrayStatsStore; |
| 168 |
$writer = new WRStatsWriter( |
| 169 |
$store, |
| 170 |
$specs, |
| 171 |
'prefix' |
| 172 |
); |
| 173 |
$entity = $global ? new GlobalEntityKey : null; |
| 174 |
foreach ( $incrOps as [ $name, $value, $time ] ) { |
| 175 |
$writer->setCurrentTime( $time ); |
| 176 |
$writer->incr( $name, $entity, $value ); |
| 177 |
} |
| 178 |
$writer = null; |
| 179 |
$this->assertSame( $expected, $store->getData() ); |
| 180 |
} |
| 181 |
|
| 182 |
public function testResetAll() { |
| 183 |
$store = new ArrayStatsStore; |
| 184 |
$specs = [ |
| 185 |
'int' => [ |
| 186 |
'sequences' => [ [ |
| 187 |
'timeStep' => 100, |
| 188 |
'expiry' => 1000, |
| 189 |
] ], |
| 190 |
'resolution' => 10 |
| 191 |
], |
| 192 |
'decimal' => [ |
| 193 |
'sequences' => [ [ |
| 194 |
'timeStep' => 200, |
| 195 |
'expiry' => 2000, |
| 196 |
] ], |
| 197 |
'resolution' => 0.1 |
| 198 |
] |
| 199 |
]; |
| 200 |
$writer = new WRStatsWriter( |
| 201 |
$store, |
| 202 |
$specs, |
| 203 |
'prefix' |
| 204 |
); |
| 205 |
$writer->setCurrentTime( 1000 ); |
| 206 |
$writer->incr( 'int', null, 20 ); |
| 207 |
$writer->incr( 'decimal', null, 0.2 ); |
| 208 |
$writer->flush(); |
| 209 |
$this->assertArrayHasKey( 'local:prefix:int::10', $store->getData() ); |
| 210 |
$this->assertArrayHasKey( 'local:prefix:decimal::5', $store->getData() ); |
| 211 |
$writer->resetAll(); |
| 212 |
$this->assertSame( [], $store->getData() ); |
| 213 |
} |
| 214 |
} |