Skip to content

Commit

Permalink
Optimize the constructor of the Span class and add some benchmarks (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 authored Jan 13, 2022
1 parent 8fea3e3 commit 7ee1505
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 19 deletions.
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
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-fixer.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
1 change: 1 addition & 0 deletions 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": "^1.3",
"phpstan/phpstan-phpunit": "^1.0",
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"
}
23 changes: 7 additions & 16 deletions src/Tracing/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +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;
}

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

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

if (null !== $context->getStartTimestamp()) {
$this->startTimestamp = $context->getStartTimestamp();
return;
}

$this->traceId = $context->getTraceId() ?? TraceId::generate();
$this->spanId = $context->getSpanId() ?? SpanId::generate();
$this->startTimestamp = $context->getStartTimestamp() ?? microtime(true);
$this->parentSpanId = $context->getParentSpanId();
$this->description = $context->getDescription();
$this->op = $context->getOp();
Expand Down
57 changes: 57 additions & 0 deletions tests/Benchmark/SpanBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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;

final class SpanBench
{
/**
* @var TransactionContext
*/
private $context;

/**
* @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();
}

/**
* @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);
}
}

0 comments on commit 7ee1505

Please sign in to comment.