packages/ztd-query-core/bench/ShadowStoreBench.php

1<?php
2
3declare(strict_types=1);
4
5namespace Bench;
6
7use PhpBench\Attributes as Bench;
8use ZtdQuery\Shadow\ShadowStore;
9
10/**
11 * Measures primary-key row changes and snapshot restoration at fixed table sizes.
12 */
13final class ShadowStoreBench
14{
15    private ShadowStore $store;
16
17    /**
18     * @var list<array{id: int, name: string}>
19     */
20    private array $updates;
21
22    /**
23     * @param array{rows: int} $params
24     */
25    public function setUp(array $params): void
26    {
27        $this->store = new ShadowStore();
28        $rows = [];
29        for ($id = 1; $id <= $params['rows']; $id++) {
30            $rows[] = ['id' => $id, 'name' => 'before'];
31        }
32        $this->store->set('users', $rows);
33        $this->updates = [['id' => $params['rows'], 'name' => 'after']];
34    }
35
36    /**
37     * @return array<string, array{rows: int}>
38     */
39    public function rowCounts(): array
40    {
41        return ['small' => ['rows' => 100], 'large' => ['rows' => 1000]];
42    }
43
44    /**
45     * Applies a row change and restores the original table for the next revolution.
46     */
47    #[Bench\BeforeMethods('setUp')]
48    #[Bench\ParamProviders('rowCounts')]
49    #[Bench\Revs(1000)]
50    public function benchUpdateAndRestore(): void
51    {
52        $snapshot = $this->store->snapshot();
53        $this->store->update('users', $this->updates, ['id']);
54        $this->store->restore($snapshot);
55    }
56
57    /**
58     * Applies a row change and restores the original table for the next revolution.
59     */
60    #[Bench\BeforeMethods('setUp')]
61    #[Bench\ParamProviders('rowCounts')]
62    #[Bench\Revs(1000)]
63    public function benchDeleteAndRestore(): void
64    {
65        $snapshot = $this->store->snapshot();
66        $this->store->delete('users', $this->updates, ['id']);
67        $this->store->restore($snapshot);
68    }
69}
70