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

Optimize span constructor #1274

Merged
merged 16 commits into from
Jan 13, 2022
7 changes: 4 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ jobs:
dependencies:
- lowest
- highest
include:
- php: '8.1'
dependencies: lowest

steps:
- name: Checkout
Expand Down Expand Up @@ -75,3 +72,7 @@ jobs:
uses: codecov/codecov-action@v1
with:
file: build/coverage-report.xml

- name: Check benchmarks
run: vendor/bin/phpbench run --revs=1 --iterations=1
ste93cry marked this conversation as resolved.
Show resolved Hide resolved
if: ${{ matrix.dependencies == 'highest' && matrix.php == '8.1' }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package.xml
/vendor
.idea
.php_cs.cache
.phpbench
.phpunit.result.cache
docs/_build
tests/clover.xml
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Optimize `Span` constructor and add benchmarks (#1274)

## 3.3.5 (2021-12-27)

- Bump the minimum required version of the `jean85/pretty-package-versions` package (#1267)
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"monolog/monolog": "^1.3|^2.0",
"nikic/php-parser": "^4.10.3",
"php-http/mock-client": "^1.3",
"phpbench/phpbench": "^1.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
Expand Down Expand Up @@ -86,7 +87,11 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"phpstan/extension-installer": true
}
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
},
"prefer-stable": true,
"extra": {
Expand Down
6 changes: 6 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.retry_threshold": 2,
"runner.path": "tests/Benchmark"
}
22 changes: 7 additions & 15 deletions src/Tracing/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,17 @@ class Span
*/
public function __construct(?SpanContext $context = null)
{
$this->traceId = TraceId::generate();
$this->spanId = SpanId::generate();
$this->startTimestamp = microtime(true);

if (null === $context) {
return;
}
$this->traceId = TraceId::generate();
$this->spanId = SpanId::generate();
$this->startTimestamp = microtime(true);

if (null !== $context->getTraceId()) {
$this->traceId = $context->getTraceId();
}

if (null !== $context->getSpanId()) {
$this->spanId = $context->getSpanId();
return;
}

if (null !== $context->getStartTimestamp()) {
$this->startTimestamp = $context->getStartTimestamp();
}
$this->traceId = $context->getTraceId() ?? TraceId::generate();
$this->spanId = $context->getSpanId() ?? SpanId::generate();
$this->startTimestamp = $context->getStartTimestamp() ?? microtime(true);

Jean85 marked this conversation as resolved.
Show resolved Hide resolved
$this->parentSpanId = $context->getParentSpanId();
$this->description = $context->getDescription();
Expand Down
56 changes: 56 additions & 0 deletions tests/Benchmark/SpanBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Sentry\Tests\Benchmark;

use PhpBench\Benchmark\Metadata\Annotations\Iterations;
use PhpBench\Benchmark\Metadata\Annotations\Revs;
use Sentry\Tracing\Span;
use Sentry\Tracing\TransactionContext;

class SpanBench
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @var TransactionContext
*/
private $context;
/**
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
* @var TransactionContext
*/
private $contextWithTimestamp;

public function __construct()
{
$this->context = TransactionContext::fromSentryTrace('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0');
$this->contextWithTimestamp = TransactionContext::fromSentryTrace('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0');
$this->contextWithTimestamp->setStartTimestamp(microtime(true));
}

/**
* @Revs(100000)
* @Iterations(10)
*/
public function benchConstructor(): void
{
$span = new Span();
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @Revs(100000)
* @Iterations(10)
*/
public function benchConstructorWithInjectedContext(): void
{
$span = new Span($this->context);
}

/**
* @Revs(100000)
* @Iterations(10)
*/
public function benchConstructorWithInjectedContextAndStartTimestamp(): void
{
$span = new Span($this->contextWithTimestamp);
}
}