Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests and benchmark for baggage parsing #873

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/API/Baggage/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ final class Metadata implements MetadataInterface

public static function getEmpty(): Metadata
{
if (null === self::$instance) {
self::$instance = new self('');
}

return self::$instance;
return self::$instance ??= new self('');
}

private string $metadata;
Expand Down
3 changes: 0 additions & 3 deletions src/API/Baggage/Propagation/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ public function __construct(string $baggageHeader)
$this->baggageHeader = $baggageHeader;
}

/**
* @todo: Make this algorithm more robust/performant.
*/
public function parseInto(BaggageBuilderInterface $baggageBuilder): void
{
foreach (explode(',', $this->baggageHeader) as $baggageString) {
Expand Down
35 changes: 35 additions & 0 deletions tests/Benchmark/BaggageParsingBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

use OpenTelemetry\API\Baggage\BaggageBuilder;
use OpenTelemetry\API\Baggage\Propagation\Parser;

class BaggageParsingBench
{
/**
* @Revs(1000)
* @Iterations(10)
* @OutputTimeUnit("microseconds")
*/
public function benchParseInto(): void
{
$builder = new BaggageBuilder();
$header = 'value1=foo,value2=bar';
$parser = new Parser($header);
$parser->parseInto($builder);
}

/**
* @Revs(1000)
* @Iterations(10)
* @OutputTimeUnit("microseconds")
*/
public function benchParseWithMetadata(): void
{
$builder = new BaggageBuilder();
$header = 'value1=foo;metadata1;metadata2,value2=bar;meta=baz';
$parser = new Parser($header);
$parser->parseInto($builder);
}
}
104 changes: 104 additions & 0 deletions tests/Unit/API/Baggage/Propagation/ParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace OpenTelemetry\Example\Unit\API\Baggage\Propagation;

use OpenTelemetry\API\Baggage\BaggageBuilderInterface;
use OpenTelemetry\API\Baggage\Metadata;
use OpenTelemetry\API\Baggage\Propagation\Parser;
use PHPUnit\Framework\TestCase;

/**
* @covers \OpenTelemetry\API\Baggage\Propagation\Parser
*/
class ParserTest extends TestCase
{
/** @var BaggageBuilderInterface&\PHPUnit\Framework\MockObject\MockObject */
private BaggageBuilderInterface $builder;

public function setUp(): void
{
$this->builder = $this->createMock(BaggageBuilderInterface::class);
}

public function test_parse_into(): void
{
$header = 'foo_key=foo_value,bar_key=bar_value';
$parser = new Parser($header);

$this->builder
->expects($this->exactly(2))
->method('set')
->withConsecutive(
[$this->equalTo('foo_key'), $this->equalTo('foo_value'), $this->anything()],
[$this->equalTo('bar_key'), $this->equalTo('bar_value'), $this->anything()],
);

$parser->parseInto($this->builder);
}

public function test_parse_into_with_properties(): void
{
//@see https://www.w3.org/TR/baggage/#example
$header = 'key1=value1;property1;property2, key2 = value2, key3=value3; propertyKey=propertyValue';
brettmc marked this conversation as resolved.
Show resolved Hide resolved
$parser = new Parser($header);

$this->builder
->expects($this->exactly(3))
->method('set')
->withConsecutive(
[
$this->equalTo('key1'),
$this->equalTo('value1'),
$this->callback(function (Metadata $metadata) {
$this->assertSame('property1;property2', $metadata->getValue());

return true;
}),
],
[
$this->equalTo('key2'),
$this->equalTo('value2'),
$this->equalTo(null),
],
[
$this->equalTo('key3'),
$this->equalTo('value3'),
$this->callback(function (Metadata $metadata) {
$this->assertSame('propertyKey=propertyValue', $metadata->getValue());

return true;
}),
],
);

$parser->parseInto($this->builder);
}

/**
* @dataProvider invalidHeaderProvider
*/
public function test_parse_into_with_invalid_header(string $header): void
{
$parser = new Parser($header);

$this->builder
->expects($this->never())
->method('set');

$parser->parseInto($this->builder);
}

public function invalidHeaderProvider(): array
{
return [
'nothing' => [''],
'empty values' => [',,,,,'],
'no equals' => ['key1,key2'],
'empty key' => ['=value'],
'key with invalid char' => ['@foo=bar'],
'value with invalid char' => ['foo="bar"'],
];
}
}