Skip to content

Commit 99a583b

Browse files
committed
chore: benchmarks for Collection component
1 parent c0c4bb6 commit 99a583b

3 files changed

Lines changed: 327 additions & 0 deletions

File tree

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
<?php
2+
3+
namespace Psl\Tests\Benchmark\Collection;
4+
5+
use PhpBench\Attributes\Groups;
6+
use PhpBench\Attributes\ParamProviders;
7+
use Psl\Collection\MapInterface;
8+
use function Psl\Vec\map;
9+
10+
abstract class AbstractMapBench
11+
{
12+
/**
13+
* @param iterable<array-key, mixed> $data
14+
*/
15+
public function benchFromArray(): void
16+
{
17+
map($this->getDataProviders(), fn ($value) => $this->createFromArray(iterator_to_array($value)));
18+
}
19+
20+
public function benchFromIterable(): void
21+
{
22+
map($this->getDataProviders(), fn ($value) => $this->createFromIterable($value));
23+
}
24+
25+
public function benchmarkValues(): void
26+
{
27+
$map = $this->createFromIterable($this->createAssociativeIterable());
28+
$map->values();
29+
}
30+
31+
public function benchmarkKeys(): void
32+
{
33+
$map = $this->createFromIterable($this->createAssociativeIterable());
34+
35+
$map->keys();
36+
}
37+
38+
public function benchmarkFilter(): void
39+
{
40+
$map = $this->createFromIterable($this->createAssociativeIterable());
41+
42+
$map->filter(fn ($v) => $v === 'a');
43+
$map->filter(fn ($v) => $v === 'k');
44+
$map->filter(fn ($v) => $v === 'foo');
45+
}
46+
47+
public function benchmarkMap(): void
48+
{
49+
$map = $this->createFromIterable($this->createAssociativeIterable());
50+
51+
$map->map(fn ($v) => $v);
52+
}
53+
54+
public function benchmarkMapWithKey(): void
55+
{
56+
$map = $this->createFromIterable($this->createAssociativeIterable());
57+
58+
$map->mapWithKey(fn ($k, $v) => [$k, $v]);
59+
}
60+
61+
public function benchmarkFirst(): void
62+
{
63+
$map = $this->createFromIterable($this->createAssociativeIterable());
64+
65+
$map->first();
66+
}
67+
68+
public function benchmarkFirstKey(): void
69+
{
70+
$map = $this->createFromIterable($this->createAssociativeIterable());
71+
72+
$map->firstKey();
73+
}
74+
75+
public function benchmarkLast(): void
76+
{
77+
$map = $this->createFromIterable($this->createAssociativeIterable());
78+
79+
$map->last();
80+
}
81+
82+
public function benchmarkLastKey(): void
83+
{
84+
$map = $this->createFromIterable($this->createAssociativeIterable());
85+
86+
$map->lastKey();
87+
}
88+
89+
public function benchmarkLinearSearch(): void
90+
{
91+
$map = $this->createFromIterable($this->createAssociativeIterable());
92+
93+
$map->linearSearch('a');
94+
$map->linearSearch('k');
95+
$map->linearSearch('lol');
96+
}
97+
98+
public function benchmarkZip(): void
99+
{
100+
$map = $this->createFromIterable($this->createAssociativeIterable());
101+
102+
$map->zip([]);
103+
$map->zip(['z', 'y', 'x']);
104+
}
105+
106+
public function benchmarkTake(): void
107+
{
108+
$map = $this->createFromIterable($this->createAssociativeIterable());
109+
110+
$map->take(0);
111+
$map->take(1);
112+
$map->take(5);
113+
$map->take(1000);
114+
}
115+
116+
public function benchmarkTakeWhile(): void
117+
{
118+
$map = $this->createFromIterable($this->createAssociativeIterable());
119+
120+
$map->takeWhile(fn ($v) => 'a');
121+
$map->takeWhile(fn ($v) => 'k');
122+
$map->takeWhile(fn ($v) => 'rolf');
123+
$map->takeWhile(fn ($v) => true);
124+
$map->takeWhile(fn ($v) => false);
125+
}
126+
127+
public function benchmarkDrop(): void
128+
{
129+
$map = $this->createFromIterable($this->createAssociativeIterable());
130+
131+
$map->drop(0);
132+
$map->drop(1);
133+
$map->drop(8);
134+
$map->drop(1000);
135+
}
136+
137+
public function benchmarkDropWhile(): void
138+
{
139+
$map = $this->createFromIterable($this->createAssociativeIterable());
140+
141+
$map->dropWhile(fn ($v) => 'a');
142+
$map->dropWhile(fn ($v) => 'k');
143+
$map->dropWhile(fn ($v) => 'rolf');
144+
$map->dropWhile(fn ($v) => true);
145+
$map->dropWhile(fn ($v) => false);
146+
}
147+
148+
public function benchmarkSlice(): void
149+
{
150+
$map = $this->createFromIterable($this->createAssociativeIterable());
151+
152+
$map->slice(0, 10);
153+
$map->slice(5, 10);
154+
$map->slice(15, 10);
155+
$map->slice(1000, 100);
156+
}
157+
158+
public function benchmarkChunk(): void
159+
{
160+
$map = $this->createFromIterable($this->createAssociativeIterable());
161+
162+
$map->chunk(1);
163+
$map->chunk(2);
164+
$map->chunk(3);
165+
$map->chunk(10);
166+
$map->chunk(1000);
167+
}
168+
169+
/**
170+
* @return array<string, array<array-key, mixed>>
171+
*/
172+
public function getDataProviders(): array
173+
{
174+
return [
175+
'empty array' => [$this->createEmptyIterable()],
176+
'list' => [$this->createNumericIterable()],
177+
'associative' => [$this->createAssociativeIterable()],
178+
];
179+
}
180+
181+
public function createEmptyIterable(): iterable
182+
{
183+
yield from [];
184+
}
185+
186+
/**
187+
* @template Tk of array-key
188+
* @template Tv
189+
*
190+
* @param iterable<Tk, Tv> $items
191+
*
192+
* @return MapInterface<Tk, Tv>
193+
*/
194+
abstract protected function createFromIterable(iterable $items): MapInterface;
195+
196+
/**
197+
* @template Tk of array-key
198+
* @template Tv
199+
*
200+
* @param array<Tk, Tv> $items
201+
*
202+
* @return MapInterface<Tk, Tv>
203+
*/
204+
abstract protected function createFromArray(array $items): MapInterface;
205+
206+
/**
207+
* @return \Generator
208+
*/
209+
function createNumericIterable(): \Generator
210+
{
211+
yield 'a';
212+
yield 'b';
213+
yield 'c';
214+
yield 'd';
215+
yield 'e';
216+
yield 'f';
217+
yield 'g';
218+
yield 'h';
219+
yield 'i';
220+
yield 'j';
221+
yield 'k';
222+
}
223+
224+
/**
225+
* @return \Generator
226+
*/
227+
function createAssociativeIterable(): \Generator
228+
{
229+
yield 'a' => 'a';
230+
yield 'b' => 'b';
231+
yield 'c' => 'c';
232+
yield 'd' => 'd';
233+
yield 'e' => 'e';
234+
yield 'f' => 'f';
235+
yield 'g' => 'g';
236+
yield 'h' => 'h';
237+
yield 'i' => 'i';
238+
yield 'j' => 'j';
239+
yield 'k' => 'k';
240+
}
241+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Psl\Tests\Benchmark\Collection;
4+
5+
use PhpBench\Attributes\Groups;
6+
use Psl\Collection\MapInterface;
7+
use Psl\Collection\Map;
8+
use Psl\Tests\Benchmark\Collection\AbstractMapBench;
9+
10+
#[Groups(["collection"])]
11+
class MapBench extends AbstractMapBench
12+
{
13+
protected function createFromIterable(iterable $items): MapInterface
14+
{
15+
return Map::fromItems($items);
16+
}
17+
18+
protected function createFromArray(array $items): MapInterface
19+
{
20+
return Map::fromArray($items);
21+
}
22+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Psl\Tests\Benchmark\Collection;
4+
5+
use PhpBench\Attributes\Groups;
6+
use Psl\Collection\MutableMapInterface;
7+
use Psl\Collection\MutableMap;
8+
9+
#[Groups(["collection"])]
10+
class MutableMapBench extends AbstractMapBench
11+
{
12+
public function benchSet(): void
13+
{
14+
$map = $this->createFromIterable($this->createAssociativeIterable());
15+
16+
// TODO
17+
// $map->set(...);
18+
}
19+
public function benchSetAll(): void
20+
{
21+
$map = $this->createFromIterable($this->createAssociativeIterable());
22+
23+
// TODO
24+
// $map->setAll(...);
25+
}
26+
public function benchAdd(): void
27+
{
28+
$map = $this->createFromIterable($this->createAssociativeIterable());
29+
30+
// TODO
31+
// $map->add(...);
32+
}
33+
public function benchAddAll(): void
34+
{
35+
$map = $this->createFromIterable($this->createAssociativeIterable());
36+
37+
// TODO
38+
// $map->addAll(...);
39+
}
40+
public function benchRemove(): void
41+
{
42+
$map = $this->createFromIterable($this->createAssociativeIterable());
43+
44+
// TODO
45+
// $map->remove(...);
46+
}
47+
public function benchClear(): void
48+
{
49+
$map = $this->createFromIterable($this->createAssociativeIterable());
50+
51+
// TODO
52+
// $map->clear(...);
53+
}
54+
55+
protected function createFromIterable(iterable $items): MutableMapInterface
56+
{
57+
return MutableMap::fromItems($items);
58+
}
59+
60+
protected function createFromArray(array $items): MutableMapInterface
61+
{
62+
return MutableMap::fromArray($items);
63+
}
64+
}

0 commit comments

Comments
 (0)