-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionPerformanceTest.php
More file actions
297 lines (250 loc) · 11.9 KB
/
CollectionPerformanceTest.php
File metadata and controls
297 lines (250 loc) · 11.9 KB
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
<?php
declare(strict_types=1);
namespace Test\TinyBlocks\Collection;
use Generator;
use PHPUnit\Framework\TestCase;
use Test\TinyBlocks\Collection\Models\Amount;
use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\Order;
use TinyBlocks\Currency\Currency;
final class CollectionPerformanceTest extends TestCase
{
public function testArrayVsCollectionWithSortProduceSameResults(): void
{
/** @Given a large dataset with 10 thousand elements */
$elements = range(1, 10_000);
/** @When performing operations with an array */
$array = array_map(
static fn(int $value): Amount => new Amount(value: $value, currency: Currency::USD),
$elements
);
usort($array, static fn(Amount $first, Amount $second): int => $first->value <=> $second->value);
/** @And performing the same operations with Collection */
$collection = Collection::createFrom(elements: $elements)
->map(static fn(int $value): Amount => new Amount(value: $value, currency: Currency::USD))
->sort(
order: Order::ASCENDING_VALUE,
predicate: static fn(Amount $first, Amount $second): int => $first->value <=> $second->value
);
/** @Then assert that both approaches produce the same results */
self::assertEquals($array[0]->value, $collection->first()->value);
self::assertEquals($array[array_key_last($array)]->value, $collection->last()->value);
}
public function testCollectionIsEfficientForFirstElementRetrieval(): void
{
/** @Given a large dataset with 10 million elements as a generator */
$createGenerator = static fn(): Generator => (static function (): Generator {
for ($index = 1; $index <= 10_000_000; $index++) {
yield $index;
}
})();
/** @When retrieving the first element matching a condition near the beginning */
$this->forceGarbageCollection();
$startTime = hrtime(true);
$startMemory = memory_get_usage(true);
/** @And filter for elements greater than 100 and get the first one (match at position 101) */
$firstElement = Collection::createFrom(elements: $createGenerator())
->filter(static fn(int $value): bool => $value > 100)
->first();
/** @And end the time and memory measurement */
$executionTimeInMs = (hrtime(true) - $startTime) / 1_000_000;
$memoryUsageInMB = (memory_get_usage(true) - $startMemory) / 1024 / 1024;
/** @Then assert that the first matching element is found */
self::assertSame(101, $firstElement);
/** @And assert that execution time is minimal due to early termination */
self::assertLessThan(
50.0,
$executionTimeInMs,
sprintf('Execution time %.2fms exceeded 50ms limit', $executionTimeInMs)
);
/** @And assert that memory usage is minimal due to not materializing all elements */
self::assertLessThan(
2.0,
$memoryUsageInMB,
sprintf('Memory usage %.2fMB exceeded 2MB limit', $memoryUsageInMB)
);
}
public function testLazyOperationsDoNotMaterializeEntireCollection(): void
{
/** @Given a generator that would use massive memory if fully materialized */
$createLargeGenerator = static fn(): Generator => (static function (): Generator {
for ($index = 1; $index <= 10_000_000; $index++) {
yield $index;
}
})();
/** @When applying multiple transformations and getting only the first element */
$this->forceGarbageCollection();
$startMemory = memory_get_usage(true);
/** @And chain filter and map operations */
$result = Collection::createFrom(elements: $createLargeGenerator())
->filter(static fn(int $value): bool => $value % 2 === 0)
->map(static fn(int $value): int => $value * 10)
->filter(static fn(int $value): bool => $value > 100)
->first();
/** @And measure memory after operation */
$memoryUsageInMB = (memory_get_usage(true) - $startMemory) / 1024 / 1024;
/** @Then assert that the correct result is returned */
self::assertSame(120, $result);
/** @And assert that memory usage is minimal (not 10 million integers in memory) */
self::assertLessThan(
10.0,
$memoryUsageInMB,
sprintf(
'Memory usage %.2fMB is too high - collection may be materializing unnecessarily',
$memoryUsageInMB
)
);
}
public function testCollectionFindByIsEfficientWithEarlyTermination(): void
{
/** @Given a large dataset with 1 million elements as a generator */
$createGenerator = static fn(): Generator => (static function (): Generator {
for ($index = 1; $index <= 1_000_000; $index++) {
yield new Amount(value: $index, currency: Currency::USD);
}
})();
/** @When finding the first element with value 100 using Collection */
$this->forceGarbageCollection();
$startTime = hrtime(true);
$startMemory = memory_get_usage(true);
/** @And use findBy to locate the element */
$foundElement = Collection::createFrom(elements: $createGenerator())
->findBy(static fn(Amount $amount): bool => $amount->value == 100.0);
/** @And end the time and memory measurement */
$executionTimeInMs = (hrtime(true) - $startTime) / 1_000_000;
$memoryUsageInMB = (memory_get_usage(true) - $startMemory) / 1024 / 1024;
/** @Then assert that the correct element is found */
self::assertSame(100.0, $foundElement->value);
/** @And assert that execution time is minimal due to early termination */
self::assertLessThan(
100.0,
$executionTimeInMs,
sprintf('Execution time %.2fms exceeded 100ms limit', $executionTimeInMs)
);
/** @And assert that memory usage is minimal */
self::assertLessThan(
2.0,
$memoryUsageInMB,
sprintf('Memory usage %.2fMB exceeded 2MB limit', $memoryUsageInMB)
);
}
public function testChainedOperationsPerformanceAndMemoryWithCollection(): void
{
/** @Given a large collection of Amount objects containing 100 thousand elements */
$collection = Collection::createFrom(
elements: (static function (): Generator {
foreach (range(1, 100_000) as $value) {
yield new Amount(value: $value, currency: Currency::USD);
}
})()
);
/** @And start measuring time and memory usage */
$this->forceGarbageCollection();
$startTime = hrtime(true);
$startMemory = memory_get_usage(true);
/**
* @When performing the following chained operations:
* filtering to retain the first 50,000 elements,
* mapping to double each Amount's value,
* filtering to retain the first 45,000 elements,
* further filtering to retain the first 35,000 elements,
* mapping to double each Amount's value again,
* filtering to retain the first 30,000 elements,
* further filtering to retain the first 10,000 elements,
* mapping to convert the currency from USD to BRL and adjusting the value by a factor of 5.5,
* sorting the collection in descending order by value.
*/
$result = $collection
->filter(static fn(Amount $amount, int $index): bool => $index < 50_000)
->map(static fn(Amount $amount): Amount => new Amount(
value: $amount->value * 2,
currency: $amount->currency
))
->filter(static fn(Amount $amount, int $index): bool => $index < 45_000)
->filter(static fn(Amount $amount, int $index): bool => $index < 35_000)
->map(static fn(Amount $amount): Amount => new Amount(
value: $amount->value * 2,
currency: $amount->currency
))
->filter(static fn(Amount $amount, int $index): bool => $index < 30_000)
->filter(static fn(Amount $amount, int $index): bool => $index < 10_000)
->map(static fn(Amount $amount): Amount => new Amount(
value: $amount->value * 5.5,
currency: Currency::BRL
))
->sort(order: Order::DESCENDING_VALUE);
/** @And force full evaluation by getting first element */
$firstElement = $result->first();
/** @And end measuring time and memory usage */
$executionTimeInSeconds = (hrtime(true) - $startTime) / 1_000_000_000;
$memoryUsageInMB = (memory_get_usage(true) - $startMemory) / 1024 / 1024;
/** @Then verify the value of the first element in the sorted collection */
self::assertSame(220_000.0, $firstElement->value);
/** @And verify that the total duration of the chained operations is within limits */
self::assertLessThan(
15.0,
$executionTimeInSeconds,
sprintf('Execution time %.2fs exceeded 15s limit', $executionTimeInSeconds)
);
/** @And verify that memory usage is within acceptable limits */
self::assertLessThan(
50.0,
$memoryUsageInMB,
sprintf('Memory usage %.2fMB exceeded 50MB limit', $memoryUsageInMB)
);
}
public function testCollectionUsesLessMemoryThanArrayForFirstElementRetrieval(): void
{
/** @Given a large dataset with 1 million elements */
$totalElements = 1_000_000;
$targetValue = 1000;
/** @When finding the first matching element with an array */
$this->forceGarbageCollection();
$startArrayMemory = memory_get_usage(true);
/** @And create array and find first element greater than target */
$array = range(1, $totalElements);
$arrayResult = null;
foreach ($array as $value) {
if ($value > $targetValue) {
$arrayResult = $value;
break;
}
}
/** @And measure memory usage for the array operations */
$arrayMemoryUsageInBytes = memory_get_usage(true) - $startArrayMemory;
/** @And clean up array memory before Collection test */
unset($array);
$this->forceGarbageCollection();
/** @When finding the first matching element with Collection using a generator */
$startCollectionMemory = memory_get_usage(true);
/** @And create collection from generator and find first element greater than target */
$generator = (static function () use ($totalElements): Generator {
for ($index = 1; $index <= $totalElements; $index++) {
yield $index;
}
})();
$collectionResult = Collection::createFrom(elements: $generator)
->filter(static fn(int $value): bool => $value > $targetValue)
->first();
/** @And measure memory usage for the Collection operations */
$collectionMemoryUsageInBytes = memory_get_usage(true) - $startCollectionMemory;
/** @Then assert that both approaches produce the same result */
self::assertSame($arrayResult, $collectionResult);
/** @And assert that Collection uses less memory than array */
self::assertLessThan(
$arrayMemoryUsageInBytes,
$collectionMemoryUsageInBytes,
sprintf(
'Collection (%d bytes) should use less memory than array (%d bytes)',
$collectionMemoryUsageInBytes,
$arrayMemoryUsageInBytes
)
);
}
private function forceGarbageCollection(): void
{
gc_enable();
gc_collect_cycles();
gc_mem_caches();
}
}