collectEncoding example

public function encode(mixed $data, string $format, array $context = []): string
    {
        if (!$this->encoder instanceof EncoderInterface) {
            throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested encoder doesn\'t implements "%s".', __METHOD__, EncoderInterface::class));
        }

        $startTime = microtime(true);
        $encoded = $this->encoder->encode($data$format$context);
        $time = microtime(true) - $startTime;

        if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
            $this->dataCollector->collectEncoding($traceId$this->encoder::class$time);
        }

        return $encoded;
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        if (!$this->encoder instanceof EncoderInterface) {
            return false;
        }

        
$this->assertArrayHasKey('line', $collectedData['denormalize'][0]['normalization'][ObjectNormalizer::class]);
    }

    public function testCollectEncoding()
    {
        $dataCollector = new SerializerDataCollector();

        $caller = ['name' => 'Foo.php', 'file' => 'src/Foo.php', 'line' => 123];
        $dataCollector->collectEncode('traceIdOne', 'data', 'format', ['foo' => 'bar'], 20.0, $caller);
        $dataCollector->collectDecode('traceIdTwo', 'data', 'format', ['foo' => 'bar'], 20.0, $caller);

        $dataCollector->collectEncoding('traceIdOne', JsonEncoder::class, 1.0);
        $dataCollector->collectEncoding('traceIdOne', JsonEncoder::class, 2.0);
        $dataCollector->collectEncoding('traceIdOne', CsvEncoder::class, 5.0);
        $dataCollector->collectEncoding('traceIdOne', CsvEncoder::class, 10.0);

        $dataCollector->collectDecoding('traceIdTwo', JsonEncoder::class, 1.0);
        $dataCollector->collectDecoding('traceIdTwo', JsonEncoder::class, 2.0);
        $dataCollector->collectDecoding('traceIdTwo', CsvEncoder::class, 5.0);
        $dataCollector->collectDecoding('traceIdTwo', CsvEncoder::class, 10.0);

        $dataCollector->lateCollect();
        $collectedData = $dataCollector->getData();

        
public function encode(mixed $data, string $format, array $context = []): string
    {
        if (!$this->encoder instanceof EncoderInterface) {
            throw new \BadMethodCallException(sprintf('The "%s()" method cannot be called as nested encoder doesn\'t implements "%s".', __METHOD__, EncoderInterface::class));
        }

        $startTime = microtime(true);
        $encoded = $this->encoder->encode($data$format$context);
        $time = microtime(true) - $startTime;

        if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
            $this->dataCollector->collectEncoding($traceId$this->encoder::class$time);
        }

        return $encoded;
    }

    public function supportsEncoding(string $format, array $context = []): bool
    {
        if (!$this->encoder instanceof EncoderInterface) {
            return false;
        }

        
Home | Imprint | This part of the site doesn't use cookies.