Skip to content

Commit 2e8d02c

Browse files
committed
refactor: alterar constantes de estimativa de tamanho para público no JsonBufferPool
1 parent 58e0e84 commit 2e8d02c

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

src/Json/Pool/JsonBufferPool.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,29 @@
1616
class JsonBufferPool
1717
{
1818
// JSON Size Estimation Constants
19-
private const STRING_OVERHEAD = 20; // Quotes + escaping overhead
20-
private const EMPTY_ARRAY_SIZE = 2; // []
21-
private const SMALL_ARRAY_SIZE = 512; // Small array estimate (< 10 items)
22-
private const MEDIUM_ARRAY_SIZE = 2048; // Medium array estimate (< 100 items)
23-
private const LARGE_ARRAY_SIZE = 8192; // Large array estimate (< 1000 items)
24-
private const XLARGE_ARRAY_SIZE = 32768; // XLarge array estimate (>= 1000 items)
19+
public const STRING_OVERHEAD = 20; // Quotes + escaping overhead
20+
public const EMPTY_ARRAY_SIZE = 2; // []
21+
public const SMALL_ARRAY_SIZE = 512; // Small array estimate (< 10 items)
22+
public const MEDIUM_ARRAY_SIZE = 2048; // Medium array estimate (< 100 items)
23+
public const LARGE_ARRAY_SIZE = 8192; // Large array estimate (< 1000 items)
24+
public const XLARGE_ARRAY_SIZE = 32768; // XLarge array estimate (>= 1000 items)
2525

2626
// Array size thresholds
2727
public const SMALL_ARRAY_THRESHOLD = 10; // Threshold for small array
2828
public const MEDIUM_ARRAY_THRESHOLD = 100; // Threshold for medium array
2929
public const LARGE_ARRAY_THRESHOLD = 1000; // Threshold for large array
3030

3131
// Object size estimation constants
32-
private const OBJECT_PROPERTY_OVERHEAD = 50; // Bytes per object property
33-
private const OBJECT_BASE_SIZE = 100; // Base size for objects
32+
public const OBJECT_PROPERTY_OVERHEAD = 50; // Bytes per object property
33+
public const OBJECT_BASE_SIZE = 100; // Base size for objects
3434

3535
// Primitive type size constants
36-
private const BOOLEAN_OR_NULL_SIZE = 10; // Size for boolean/null values
37-
private const NUMERIC_SIZE = 20; // Size for numeric values
38-
private const DEFAULT_ESTIMATE = 100; // Default fallback estimate
36+
public const BOOLEAN_OR_NULL_SIZE = 10; // Size for boolean/null values
37+
public const NUMERIC_SIZE = 20; // Size for numeric values
38+
public const DEFAULT_ESTIMATE = 100; // Default fallback estimate
3939

4040
// Buffer capacity constants
41-
private const MIN_LARGE_BUFFER_SIZE = 65536; // Minimum size for very large buffers (64KB)
41+
public const MIN_LARGE_BUFFER_SIZE = 65536; // Minimum size for very large buffers (64KB)
4242
private const BUFFER_SIZE_MULTIPLIER = 2; // Multiplier for buffer size calculation
4343

4444
// Pooling decision thresholds (for determining when to use pooled encoding)

tests/Json/Pool/JsonSizeEstimationTest.php

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public function testStringEstimation(): void
3939
$longEstimate = $this->callEstimateJsonSize($longString);
4040

4141
// Should be string length + STRING_OVERHEAD (20)
42-
$this->assertEquals(strlen($shortString) + 20, $shortEstimate);
43-
$this->assertEquals(strlen($longString) + 20, $longEstimate);
42+
$this->assertEquals(strlen($shortString) + JsonBufferPool::STRING_OVERHEAD, $shortEstimate);
43+
$this->assertEquals(strlen($longString) + JsonBufferPool::STRING_OVERHEAD, $longEstimate);
4444

4545
// Longer strings should have larger estimates
4646
$this->assertGreaterThan($shortEstimate, $longEstimate);
@@ -64,7 +64,7 @@ public function testArrayEstimationThresholds(): void
6464
$xlargeEstimate = $this->callEstimateJsonSize($xlargeArray);
6565

6666
// Empty array should be smallest (2 bytes for [])
67-
$this->assertEquals(2, $emptyEstimate);
67+
$this->assertEquals(JsonBufferPool::EMPTY_ARRAY_SIZE, $emptyEstimate);
6868

6969
// Each category should be larger than the previous
7070
$this->assertGreaterThan($emptyEstimate, $smallEstimate);
@@ -73,10 +73,10 @@ public function testArrayEstimationThresholds(): void
7373
$this->assertGreaterThan($largeEstimate, $xlargeEstimate);
7474

7575
// Verify expected sizes based on constants
76-
$this->assertEquals(512, $smallEstimate); // SMALL_ARRAY_SIZE
77-
$this->assertEquals(2048, $mediumEstimate); // MEDIUM_ARRAY_SIZE
78-
$this->assertEquals(8192, $largeEstimate); // LARGE_ARRAY_SIZE
79-
$this->assertEquals(32768, $xlargeEstimate); // XLARGE_ARRAY_SIZE
76+
$this->assertEquals(JsonBufferPool::SMALL_ARRAY_SIZE, $smallEstimate);
77+
$this->assertEquals(JsonBufferPool::MEDIUM_ARRAY_SIZE, $mediumEstimate);
78+
$this->assertEquals(JsonBufferPool::LARGE_ARRAY_SIZE, $largeEstimate);
79+
$this->assertEquals(JsonBufferPool::XLARGE_ARRAY_SIZE, $xlargeEstimate);
8080
}
8181

8282
/**
@@ -93,15 +93,21 @@ public function testObjectEstimation(): void
9393
$largeEstimate = $this->callEstimateJsonSize($largeObject);
9494

9595
// Empty object should be base size (100)
96-
$this->assertEquals(100, $emptyEstimate);
96+
$this->assertEquals(JsonBufferPool::OBJECT_BASE_SIZE, $emptyEstimate);
9797

9898
// Objects with properties should be larger
9999
$this->assertGreaterThan($emptyEstimate, $smallEstimate);
100100
$this->assertGreaterThan($smallEstimate, $largeEstimate);
101101

102-
// Should follow formula: property_count * 50 + 100
103-
$this->assertEquals(2 * 50 + 100, $smallEstimate); // 2 properties
104-
$this->assertEquals(10 * 50 + 100, $largeEstimate); // 10 properties
102+
// Should follow formula: property_count * OBJECT_PROPERTY_OVERHEAD + OBJECT_BASE_SIZE
103+
$this->assertEquals(
104+
2 * JsonBufferPool::OBJECT_PROPERTY_OVERHEAD + JsonBufferPool::OBJECT_BASE_SIZE,
105+
$smallEstimate
106+
); // 2 properties
107+
$this->assertEquals(
108+
10 * JsonBufferPool::OBJECT_PROPERTY_OVERHEAD + JsonBufferPool::OBJECT_BASE_SIZE,
109+
$largeEstimate
110+
); // 10 properties
105111
}
106112

107113
/**
@@ -120,12 +126,12 @@ public function testPrimitiveEstimations(): void
120126
$floatEstimate = $this->callEstimateJsonSize($float);
121127

122128
// Boolean and null should be same size (10)
123-
$this->assertEquals(10, $booleanEstimate);
124-
$this->assertEquals(10, $nullEstimate);
129+
$this->assertEquals(JsonBufferPool::BOOLEAN_OR_NULL_SIZE, $booleanEstimate);
130+
$this->assertEquals(JsonBufferPool::BOOLEAN_OR_NULL_SIZE, $nullEstimate);
125131

126132
// Numeric values should be same size (20)
127-
$this->assertEquals(20, $integerEstimate);
128-
$this->assertEquals(20, $floatEstimate);
133+
$this->assertEquals(JsonBufferPool::NUMERIC_SIZE, $integerEstimate);
134+
$this->assertEquals(JsonBufferPool::NUMERIC_SIZE, $floatEstimate);
129135
}
130136

131137
/**
@@ -139,7 +145,7 @@ public function testDefaultEstimation(): void
139145
fclose($resource);
140146

141147
// Should return default estimate (100)
142-
$this->assertEquals(100, $estimate);
148+
$this->assertEquals(JsonBufferPool::DEFAULT_ESTIMATE, $estimate);
143149
}
144150

145151
/**
@@ -153,11 +159,11 @@ public function testOptimalCapacityCalculation(): void
153159
$smallCapacity = JsonBufferPool::getOptimalCapacity($smallData);
154160
$largeCapacity = JsonBufferPool::getOptimalCapacity($largeData);
155161

156-
// Small data should fit in standard categories
162+
// Small data should fit in standard categories (1024, 4096, 16384, 65536)
157163
$this->assertContains($smallCapacity, [1024, 4096, 16384, 65536]);
158164

159165
// Large data should get calculated capacity
160-
$this->assertGreaterThanOrEqual(65536, $largeCapacity);
166+
$this->assertGreaterThanOrEqual(JsonBufferPool::MIN_LARGE_BUFFER_SIZE, $largeCapacity);
161167
$this->assertGreaterThan($smallCapacity, $largeCapacity);
162168
}
163169

@@ -167,10 +173,10 @@ public function testOptimalCapacityCalculation(): void
167173
public function testConstantsAreReasonable(): void
168174
{
169175
// Test that size constants are in ascending order
170-
$this->assertLessThan(512, 2); // EMPTY < SMALL
171-
$this->assertLessThan(2048, 512); // SMALL < MEDIUM
172-
$this->assertLessThan(8192, 2048); // MEDIUM < LARGE
173-
$this->assertLessThan(32768, 8192); // LARGE < XLARGE
176+
$this->assertLessThan(JsonBufferPool::SMALL_ARRAY_SIZE, JsonBufferPool::EMPTY_ARRAY_SIZE); // EMPTY < SMALL
177+
$this->assertLessThan(JsonBufferPool::MEDIUM_ARRAY_SIZE, JsonBufferPool::SMALL_ARRAY_SIZE); // SMALL < MEDIUM
178+
$this->assertLessThan(JsonBufferPool::LARGE_ARRAY_SIZE, JsonBufferPool::MEDIUM_ARRAY_SIZE); // MEDIUM < LARGE
179+
$this->assertLessThan(JsonBufferPool::XLARGE_ARRAY_SIZE, JsonBufferPool::LARGE_ARRAY_SIZE); // LARGE < XLARGE
174180

175181
// Test threshold constants are in ascending order
176182
$this->assertLessThan(
@@ -183,9 +189,9 @@ public function testConstantsAreReasonable(): void
183189
); // MEDIUM < LARGE threshold
184190

185191
// Test overhead constants are reasonable
186-
$this->assertGreaterThan(0, 20); // STRING_OVERHEAD > 0
187-
$this->assertGreaterThan(0, 50); // OBJECT_PROPERTY_OVERHEAD > 0
188-
$this->assertGreaterThan(0, 100); // OBJECT_BASE_SIZE > 0
192+
$this->assertGreaterThan(0, JsonBufferPool::STRING_OVERHEAD); // STRING_OVERHEAD > 0
193+
$this->assertGreaterThan(0, JsonBufferPool::OBJECT_PROPERTY_OVERHEAD); // OBJECT_PROPERTY_OVERHEAD > 0
194+
$this->assertGreaterThan(0, JsonBufferPool::OBJECT_BASE_SIZE); // OBJECT_BASE_SIZE > 0
189195
}
190196

191197
/**

0 commit comments

Comments
 (0)