|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PivotPHP\Core\Tests\Json; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use PivotPHP\Core\Http\Response; |
| 9 | +use PivotPHP\Core\Json\Pool\JsonBufferPool; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test that pooling thresholds are centralized and consistent |
| 13 | + */ |
| 14 | +class JsonPoolingThresholdsTest extends TestCase |
| 15 | +{ |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + JsonBufferPool::clearPools(); |
| 19 | + JsonBufferPool::resetConfiguration(); |
| 20 | + } |
| 21 | + |
| 22 | + protected function tearDown(): void |
| 23 | + { |
| 24 | + JsonBufferPool::clearPools(); |
| 25 | + JsonBufferPool::resetConfiguration(); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Test that pooling constants are properly defined |
| 30 | + */ |
| 31 | + public function testPoolingConstantsExist(): void |
| 32 | + { |
| 33 | + $this->assertTrue(defined('PivotPHP\Core\Json\Pool\JsonBufferPool::POOLING_ARRAY_THRESHOLD')); |
| 34 | + $this->assertTrue(defined('PivotPHP\Core\Json\Pool\JsonBufferPool::POOLING_OBJECT_THRESHOLD')); |
| 35 | + $this->assertTrue(defined('PivotPHP\Core\Json\Pool\JsonBufferPool::POOLING_STRING_THRESHOLD')); |
| 36 | + |
| 37 | + // Verify values are reasonable |
| 38 | + $this->assertEquals(10, JsonBufferPool::POOLING_ARRAY_THRESHOLD); |
| 39 | + $this->assertEquals(5, JsonBufferPool::POOLING_OBJECT_THRESHOLD); |
| 40 | + $this->assertEquals(1024, JsonBufferPool::POOLING_STRING_THRESHOLD); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test that Response uses centralized thresholds |
| 45 | + */ |
| 46 | + public function testResponseUsesPoolingThresholds(): void |
| 47 | + { |
| 48 | + $response = new Response(); |
| 49 | + $response->setTestMode(true); |
| 50 | + |
| 51 | + // Test array threshold - just below threshold should not pool |
| 52 | + $smallArray = array_fill(0, JsonBufferPool::POOLING_ARRAY_THRESHOLD - 1, 'item'); |
| 53 | + $response->json($smallArray); |
| 54 | + |
| 55 | + $stats = JsonBufferPool::getStatistics(); |
| 56 | + $this->assertEquals(0, $stats['total_operations'], 'Small arrays should not use pooling'); |
| 57 | + |
| 58 | + // Reset |
| 59 | + JsonBufferPool::clearPools(); |
| 60 | + $response = new Response(); |
| 61 | + $response->setTestMode(true); |
| 62 | + |
| 63 | + // Test array threshold - at threshold should pool |
| 64 | + $mediumArray = array_fill(0, JsonBufferPool::POOLING_ARRAY_THRESHOLD, 'item'); |
| 65 | + $response->json($mediumArray); |
| 66 | + |
| 67 | + $stats = JsonBufferPool::getStatistics(); |
| 68 | + $this->assertEquals(1, $stats['total_operations'], 'Arrays at threshold should use pooling'); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Test object pooling threshold consistency |
| 73 | + */ |
| 74 | + public function testObjectPoolingThreshold(): void |
| 75 | + { |
| 76 | + $response = new Response(); |
| 77 | + $response->setTestMode(true); |
| 78 | + |
| 79 | + // Create object just below threshold |
| 80 | + $smallObject = new \stdClass(); |
| 81 | + for ($i = 0; $i < JsonBufferPool::POOLING_OBJECT_THRESHOLD - 1; $i++) { |
| 82 | + $smallObject->{"prop{$i}"} = "value{$i}"; |
| 83 | + } |
| 84 | + |
| 85 | + $response->json($smallObject); |
| 86 | + $stats = JsonBufferPool::getStatistics(); |
| 87 | + $this->assertEquals(0, $stats['total_operations'], 'Small objects should not use pooling'); |
| 88 | + |
| 89 | + // Reset |
| 90 | + JsonBufferPool::clearPools(); |
| 91 | + $response = new Response(); |
| 92 | + $response->setTestMode(true); |
| 93 | + |
| 94 | + // Create object at threshold |
| 95 | + $mediumObject = new \stdClass(); |
| 96 | + for ($i = 0; $i < JsonBufferPool::POOLING_OBJECT_THRESHOLD; $i++) { |
| 97 | + $mediumObject->{"prop{$i}"} = "value{$i}"; |
| 98 | + } |
| 99 | + |
| 100 | + $response->json($mediumObject); |
| 101 | + $stats = JsonBufferPool::getStatistics(); |
| 102 | + $this->assertEquals(1, $stats['total_operations'], 'Objects at threshold should use pooling'); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test string pooling threshold consistency |
| 107 | + */ |
| 108 | + public function testStringPoolingThreshold(): void |
| 109 | + { |
| 110 | + $response = new Response(); |
| 111 | + $response->setTestMode(true); |
| 112 | + |
| 113 | + // String just under threshold |
| 114 | + $shortString = str_repeat('x', JsonBufferPool::POOLING_STRING_THRESHOLD); |
| 115 | + $response->json($shortString); |
| 116 | + |
| 117 | + $stats = JsonBufferPool::getStatistics(); |
| 118 | + $this->assertEquals(0, $stats['total_operations'], 'Short strings should not use pooling'); |
| 119 | + |
| 120 | + // Reset |
| 121 | + JsonBufferPool::clearPools(); |
| 122 | + $response = new Response(); |
| 123 | + $response->setTestMode(true); |
| 124 | + |
| 125 | + // String over threshold |
| 126 | + $longString = str_repeat('x', JsonBufferPool::POOLING_STRING_THRESHOLD + 1); |
| 127 | + $response->json($longString); |
| 128 | + |
| 129 | + $stats = JsonBufferPool::getStatistics(); |
| 130 | + $this->assertEquals(1, $stats['total_operations'], 'Long strings should use pooling'); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Test that thresholds are reasonable for performance |
| 135 | + */ |
| 136 | + public function testThresholdsAreReasonable(): void |
| 137 | + { |
| 138 | + // Array threshold should be high enough to avoid pooling small arrays |
| 139 | + $this->assertGreaterThanOrEqual(5, JsonBufferPool::POOLING_ARRAY_THRESHOLD); |
| 140 | + $this->assertLessThanOrEqual(50, JsonBufferPool::POOLING_ARRAY_THRESHOLD); |
| 141 | + |
| 142 | + // Object threshold should be reasonable for common objects |
| 143 | + $this->assertGreaterThanOrEqual(3, JsonBufferPool::POOLING_OBJECT_THRESHOLD); |
| 144 | + $this->assertLessThanOrEqual(20, JsonBufferPool::POOLING_OBJECT_THRESHOLD); |
| 145 | + |
| 146 | + // String threshold should be reasonable (around 1KB) |
| 147 | + $this->assertGreaterThanOrEqual(512, JsonBufferPool::POOLING_STRING_THRESHOLD); |
| 148 | + $this->assertLessThanOrEqual(4096, JsonBufferPool::POOLING_STRING_THRESHOLD); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Test consistency between direct pooling and Response pooling |
| 153 | + */ |
| 154 | + public function testConsistencyBetweenDirectAndResponsePooling(): void |
| 155 | + { |
| 156 | + $testData = array_fill(0, JsonBufferPool::POOLING_ARRAY_THRESHOLD, 'test'); |
| 157 | + |
| 158 | + // Direct pooling |
| 159 | + JsonBufferPool::clearPools(); |
| 160 | + $directResult = JsonBufferPool::encodeWithPool($testData); |
| 161 | + $directStats = JsonBufferPool::getStatistics(); |
| 162 | + |
| 163 | + // Response pooling |
| 164 | + JsonBufferPool::clearPools(); |
| 165 | + $response = new Response(); |
| 166 | + $response->setTestMode(true); |
| 167 | + $response->json($testData); |
| 168 | + $responseResult = $response->getBodyAsString(); |
| 169 | + $responseStats = JsonBufferPool::getStatistics(); |
| 170 | + |
| 171 | + // Results should be identical |
| 172 | + $this->assertEquals($directResult, $responseResult); |
| 173 | + |
| 174 | + // Both should have used pooling |
| 175 | + $this->assertEquals(1, $directStats['total_operations']); |
| 176 | + $this->assertEquals(1, $responseStats['total_operations']); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Test that updating centralized constants affects both components |
| 181 | + */ |
| 182 | + public function testCentralizedConstantsAffectBothComponents(): void |
| 183 | + { |
| 184 | + // This test verifies that the constants are truly centralized |
| 185 | + // by checking that Response uses the same values as JsonBufferPool |
| 186 | + |
| 187 | + $reflection = new \ReflectionClass('PivotPHP\Core\Http\Response'); |
| 188 | + $shouldUsePoolingMethod = $reflection->getMethod('shouldUseJsonPooling'); |
| 189 | + $shouldUsePoolingMethod->setAccessible(true); |
| 190 | + |
| 191 | + $response = new Response(); |
| 192 | + |
| 193 | + // Test array threshold boundary |
| 194 | + $arrayAtThreshold = array_fill(0, JsonBufferPool::POOLING_ARRAY_THRESHOLD, 'item'); |
| 195 | + $arrayBelowThreshold = array_fill(0, JsonBufferPool::POOLING_ARRAY_THRESHOLD - 1, 'item'); |
| 196 | + |
| 197 | + $this->assertTrue($shouldUsePoolingMethod->invoke($response, $arrayAtThreshold)); |
| 198 | + $this->assertFalse($shouldUsePoolingMethod->invoke($response, $arrayBelowThreshold)); |
| 199 | + |
| 200 | + // Test object threshold boundary |
| 201 | + $objectAtThreshold = new \stdClass(); |
| 202 | + for ($i = 0; $i < JsonBufferPool::POOLING_OBJECT_THRESHOLD; $i++) { |
| 203 | + $objectAtThreshold->{"prop{$i}"} = "value{$i}"; |
| 204 | + } |
| 205 | + |
| 206 | + $objectBelowThreshold = new \stdClass(); |
| 207 | + for ($i = 0; $i < JsonBufferPool::POOLING_OBJECT_THRESHOLD - 1; $i++) { |
| 208 | + $objectBelowThreshold->{"prop{$i}"} = "value{$i}"; |
| 209 | + } |
| 210 | + |
| 211 | + $this->assertTrue($shouldUsePoolingMethod->invoke($response, $objectAtThreshold)); |
| 212 | + $this->assertFalse($shouldUsePoolingMethod->invoke($response, $objectBelowThreshold)); |
| 213 | + |
| 214 | + // Test string threshold boundary |
| 215 | + $stringAtThreshold = str_repeat('x', JsonBufferPool::POOLING_STRING_THRESHOLD + 1); |
| 216 | + $stringBelowThreshold = str_repeat('x', JsonBufferPool::POOLING_STRING_THRESHOLD); |
| 217 | + |
| 218 | + $this->assertTrue($shouldUsePoolingMethod->invoke($response, $stringAtThreshold)); |
| 219 | + $this->assertFalse($shouldUsePoolingMethod->invoke($response, $stringBelowThreshold)); |
| 220 | + } |
| 221 | +} |
0 commit comments