Skip to content

Commit 7dd9a30

Browse files
authored
perf: ignore uuid and timestamp generation for NoopContext (#361)
1 parent cb2165b commit 7dd9a30

File tree

8 files changed

+111
-6
lines changed

8 files changed

+111
-6
lines changed

.github/PULL_REQUEST_TEMPLATE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<!-- ==== 📈 Remove this line WHEN AND ONLY WHEN you're improving the performance, follow the checklist 👇 ====
1515
### Improve the performance of <class or module or ...>
16-
- [ ] Add a benchmark for the improvement, refer to [the existing ones](https://github.com/apache/skywalking/blob/master/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/LinkedArrayBenchmark.java)
16+
- [ ] Add a benchmark for the improvement, refer to [the existing ones](https://github.com/apache/skywalking/blob/master/oap-server/microbench/src/main/java/org/apache/skywalking/oap/server/microbench/library/datacarrier/LinkedArrayBenchmark.java)
1717
- [ ] The benchmark result.
1818
```text
1919
<Paste the benchmark results here>

poetry.lock

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ gunicorn = "*"
100100
uvicorn = "*"
101101
kafka-python = "*"
102102
requests = "*"
103+
pytest-benchmark = "4.0.0"
103104

104105
[tool.poetry.group.plugins.dependencies]
105106
urllib3 = "1.26.7"

skywalking/trace/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18+
from typing import Optional
1819
import uuid
1920

2021
from skywalking.utils.counter import AtomicCounter
@@ -23,7 +24,7 @@
2324

2425

2526
class ID(object):
26-
def __init__(self, raw_id: str = None):
27+
def __init__(self, raw_id: Optional[str] = None):
2728
self.value = raw_id or str(uuid.uuid1()).replace('-', '')
2829

2930
def __str__(self):

skywalking/trace/context.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from skywalking import sampling
2424
from skywalking.trace import ID
2525
from skywalking.trace.carrier import Carrier
26-
from skywalking.trace.segment import Segment, SegmentRef
26+
from skywalking.trace.segment import NoopSegment, Segment, SegmentRef
2727
from skywalking.trace.snapshot import Snapshot
2828
from skywalking.trace.span import Span, Kind, NoopSpan, EntrySpan, ExitSpan
2929
from skywalking.utils.counter import Counter
@@ -95,7 +95,7 @@ def get_name(self):
9595

9696
class SpanContext:
9797
def __init__(self):
98-
self.segment: Segment = Segment()
98+
self.segment = Segment()
9999
self._sid: Counter = Counter()
100100
self._correlation: dict = {}
101101
self._nspans: int = 0
@@ -285,7 +285,13 @@ def continued(self, snapshot: 'Snapshot'):
285285

286286
class NoopContext(SpanContext):
287287
def __init__(self):
288-
super().__init__()
288+
self.segment = NoopSegment()
289+
self._sid: Counter = Counter()
290+
self._correlation: dict = {}
291+
self._nspans: int = 0
292+
self.profile_status: Optional[ProfileStatusReference] = None
293+
self.create_time = 0
294+
self.primary_endpoint: Optional[PrimaryEndpoint] = None
289295

290296
def new_local_span(self, op: str) -> Span:
291297
return NoopSpan(self)

skywalking/trace/segment.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class _NewID(ID):
6868
pass
6969

7070

71+
class _NewNoopID(ID):
72+
73+
def __init__(self):
74+
self.value = ''
75+
76+
7177
@tostring
7278
class Segment(object):
7379
def __init__(self):
@@ -83,3 +89,11 @@ def relate(self, trace_id: ID):
8389
if isinstance(self.related_traces[0], _NewID):
8490
del self.related_traces[-1]
8591
self.related_traces.append(trace_id)
92+
93+
94+
class NoopSegment(Segment):
95+
def __init__(self):
96+
self.segment_id = _NewNoopID()
97+
self.spans = []
98+
self.timestamp = 0
99+
self.related_traces = [_NewNoopID()]

tests/benchmark/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#

tests/benchmark/test_span.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
from typing import Any
19+
from skywalking.trace.context import NoopContext
20+
from skywalking.trace.span import NoopSpan
21+
22+
23+
def test_noopspan_1000(benchmark: Any):
24+
result = benchmark(lambda: [NoopSpan(NoopContext()) for _ in range(1000)])
25+
assert result
26+
27+
28+
def test_noopspan_40000(benchmark: Any):
29+
result = benchmark(lambda: [NoopSpan(NoopContext()) for _ in range(40000)])
30+
assert result
31+
32+
33+
def test_noopspan_200k(benchmark: Any):
34+
result = benchmark(
35+
lambda: [NoopSpan(NoopContext()) for _ in range(200000)])
36+
assert result

0 commit comments

Comments
 (0)