PT
apps/cryo-studio / assets / demo / shop_worker.php · commit 76aa68d
<?php// A long-running worker that handles batches of orders. Run with ext-cryo://   php -d extension=cryo -d cryo.track_alloc=stack shop_worker.php// It takes one snapshot after each batch (Cryo Studio compares them) and samples its memory into shop.cryom.class Query    { public function __construct(public string $sql) {} }class Order    { public array $lines = []; public function __construct(public int $id) {} }class Price    { public function __construct(public int $productId, public float $amount) {} }class Customer { public function __construct(public int $id, public string $name) {} }class SqlLogger{    public static array $log = [];    public static function log(string $sql): void    {        $query = new Query($sql);    // created here        self::$log[] = $query;       // kept: never cleared    }}class PriceCache{    private array $items = [];    public function get(int $productId): Price    {        if (isset($this->items[$productId])) {            return $this->items[$productId];        }        $price = new Price($productId, $productId * 1.5);        $this->items[$productId] = $price;        if (count($this->items) > 60) {            unset($this->items[array_key_first($this->items)]); // bounded        }        return $price;    }}class Connection{    public ?Connection $peer = null;   // a cycle    public static function pair(): void    {        $a = new Connection();        $b = new Connection();        $a->peer = $b;        $b->peer = $a;     // left to the GC    }}class EventBus{    /** @var list<Closure> */    public static array $listeners = [];    public static function on(Closure $listener): void    {        self::$listeners[] = $listener;    }}$customers = [];$audit = [];$prices = new PriceCache();function handleBatch(int $batch, PriceCache $prices, array &$customers, array &$audit): void{    for ($i = 0; $i < 80; $i++) {        $id = $batch * 80 + $i;        $order = new Order($id);         // freed        $order->lines[] = $prices->get(($id * 7) % 200);        SqlLogger::log("INSERT INTO orders (id) VALUES ($id)");        $audit[] = sprintf('%s shipped', 'order'); // copies        if ($i % 8 === 0) {            $customers[$id] = new Customer($id, "c$id"); // half expire        }        usleep(4000);                    // the work    }    foreach (array_keys($customers) as $k => $id) {        if ($k % 2 === 0) {            unset($customers[$id]);        }    }    Connection::pair();}gc_disable();                        // cycles stay: orphansfor ($k = 0; $k < 12; $k++) {    EventBus::on(fn (Order $o) => $o->id + $k);   // listeners}cryo_metrics_sample(50, __DIR__ . '/shop.cryom');for ($batch = 0; $batch < 5; $batch++) {    handleBatch($batch, $prices, $customers, $audit);    cryo_snapshot(__DIR__ . "/shop-$batch.cryo", ['gc' => false]);    cryo_metrics_mark("snapshot shop-$batch");}cryo_metrics_sample(0, __DIR__ . '/shop.cryom');echo "5 snapshots and shop.cryom in ", __DIR__, "\n";