Skip to content

Commit 7ee1505

Browse files
authored
Optimize the constructor of the Span class and add some benchmarks (#1274)
1 parent 8fea3e3 commit 7ee1505

File tree

7 files changed

+78
-19
lines changed

7 files changed

+78
-19
lines changed

.github/workflows/ci.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ jobs:
2626
dependencies:
2727
- lowest
2828
- highest
29-
include:
30-
- php: '8.1'
31-
dependencies: lowest
3229

3330
steps:
3431
- name: Checkout
@@ -75,3 +72,7 @@ jobs:
7572
uses: codecov/codecov-action@v1
7673
with:
7774
file: build/coverage-report.xml
75+
76+
- name: Check benchmarks
77+
run: vendor/bin/phpbench run --revs=1 --iterations=1
78+
if: ${{ matrix.dependencies == 'highest' && matrix.php == '8.1' }}

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package.xml
33
/vendor
44
.idea
55
.php-cs-fixer.cache
6+
.phpbench
67
.phpunit.result.cache
78
docs/_build
89
tests/clover.xml

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Optimize `Span` constructor and add benchmarks (#1274)
6+
57
## 3.3.5 (2021-12-27)
68

79
- Bump the minimum required version of the `jean85/pretty-package-versions` package (#1267)

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"monolog/monolog": "^1.3|^2.0",
4545
"nikic/php-parser": "^4.10.3",
4646
"php-http/mock-client": "^1.3",
47+
"phpbench/phpbench": "^1.0",
4748
"phpstan/extension-installer": "^1.0",
4849
"phpstan/phpstan": "^1.3",
4950
"phpstan/phpstan-phpunit": "^1.0",

phpbench.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
3+
"runner.bootstrap": "vendor/autoload.php",
4+
"runner.retry_threshold": 2,
5+
"runner.path": "tests/Benchmark"
6+
}

src/Tracing/Span.php

+7-16
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,17 @@ class Span
8080
*/
8181
public function __construct(?SpanContext $context = null)
8282
{
83-
$this->traceId = TraceId::generate();
84-
$this->spanId = SpanId::generate();
85-
$this->startTimestamp = microtime(true);
86-
8783
if (null === $context) {
88-
return;
89-
}
90-
91-
if (null !== $context->getTraceId()) {
92-
$this->traceId = $context->getTraceId();
93-
}
94-
95-
if (null !== $context->getSpanId()) {
96-
$this->spanId = $context->getSpanId();
97-
}
84+
$this->traceId = TraceId::generate();
85+
$this->spanId = SpanId::generate();
86+
$this->startTimestamp = microtime(true);
9887

99-
if (null !== $context->getStartTimestamp()) {
100-
$this->startTimestamp = $context->getStartTimestamp();
88+
return;
10189
}
10290

91+
$this->traceId = $context->getTraceId() ?? TraceId::generate();
92+
$this->spanId = $context->getSpanId() ?? SpanId::generate();
93+
$this->startTimestamp = $context->getStartTimestamp() ?? microtime(true);
10394
$this->parentSpanId = $context->getParentSpanId();
10495
$this->description = $context->getDescription();
10596
$this->op = $context->getOp();

tests/Benchmark/SpanBench.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\Tests\Benchmark;
6+
7+
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
8+
use PhpBench\Benchmark\Metadata\Annotations\Revs;
9+
use Sentry\Tracing\Span;
10+
use Sentry\Tracing\TransactionContext;
11+
12+
final class SpanBench
13+
{
14+
/**
15+
* @var TransactionContext
16+
*/
17+
private $context;
18+
19+
/**
20+
* @var TransactionContext
21+
*/
22+
private $contextWithTimestamp;
23+
24+
public function __construct()
25+
{
26+
$this->context = TransactionContext::fromSentryTrace('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0');
27+
$this->contextWithTimestamp = TransactionContext::fromSentryTrace('566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-0');
28+
$this->contextWithTimestamp->setStartTimestamp(microtime(true));
29+
}
30+
31+
/**
32+
* @Revs(100000)
33+
* @Iterations(10)
34+
*/
35+
public function benchConstructor(): void
36+
{
37+
$span = new Span();
38+
}
39+
40+
/**
41+
* @Revs(100000)
42+
* @Iterations(10)
43+
*/
44+
public function benchConstructorWithInjectedContext(): void
45+
{
46+
$span = new Span($this->context);
47+
}
48+
49+
/**
50+
* @Revs(100000)
51+
* @Iterations(10)
52+
*/
53+
public function benchConstructorWithInjectedContextAndStartTimestamp(): void
54+
{
55+
$span = new Span($this->contextWithTimestamp);
56+
}
57+
}

0 commit comments

Comments
 (0)