Skip to content

Commit

Permalink
perf: ignore uuid and timestamp generation for NoopContext (#361)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsonglew authored Nov 12, 2024
1 parent cb2165b commit 7dd9a30
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<!-- ==== 📈 Remove this line WHEN AND ONLY WHEN you're improving the performance, follow the checklist 👇 ====
### Improve the performance of <class or module or ...>
- [ ] 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)
- [ ] 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)
- [ ] The benchmark result.
```text
<Paste the benchmark results here>
Expand Down
33 changes: 32 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ gunicorn = "*"
uvicorn = "*"
kafka-python = "*"
requests = "*"
pytest-benchmark = "4.0.0"

[tool.poetry.group.plugins.dependencies]
urllib3 = "1.26.7"
Expand Down
3 changes: 2 additions & 1 deletion skywalking/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.
#

from typing import Optional
import uuid

from skywalking.utils.counter import AtomicCounter
Expand All @@ -23,7 +24,7 @@


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

def __str__(self):
Expand Down
12 changes: 9 additions & 3 deletions skywalking/trace/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from skywalking import sampling
from skywalking.trace import ID
from skywalking.trace.carrier import Carrier
from skywalking.trace.segment import Segment, SegmentRef
from skywalking.trace.segment import NoopSegment, Segment, SegmentRef
from skywalking.trace.snapshot import Snapshot
from skywalking.trace.span import Span, Kind, NoopSpan, EntrySpan, ExitSpan
from skywalking.utils.counter import Counter
Expand Down Expand Up @@ -95,7 +95,7 @@ def get_name(self):

class SpanContext:
def __init__(self):
self.segment: Segment = Segment()
self.segment = Segment()
self._sid: Counter = Counter()
self._correlation: dict = {}
self._nspans: int = 0
Expand Down Expand Up @@ -285,7 +285,13 @@ def continued(self, snapshot: 'Snapshot'):

class NoopContext(SpanContext):
def __init__(self):
super().__init__()
self.segment = NoopSegment()
self._sid: Counter = Counter()
self._correlation: dict = {}
self._nspans: int = 0
self.profile_status: Optional[ProfileStatusReference] = None
self.create_time = 0
self.primary_endpoint: Optional[PrimaryEndpoint] = None

def new_local_span(self, op: str) -> Span:
return NoopSpan(self)
Expand Down
14 changes: 14 additions & 0 deletions skywalking/trace/segment.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class _NewID(ID):
pass


class _NewNoopID(ID):

def __init__(self):
self.value = ''


@tostring
class Segment(object):
def __init__(self):
Expand All @@ -83,3 +89,11 @@ def relate(self, trace_id: ID):
if isinstance(self.related_traces[0], _NewID):
del self.related_traces[-1]
self.related_traces.append(trace_id)


class NoopSegment(Segment):
def __init__(self):
self.segment_id = _NewNoopID()
self.spans = []
self.timestamp = 0
self.related_traces = [_NewNoopID()]
16 changes: 16 additions & 0 deletions tests/benchmark/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
36 changes: 36 additions & 0 deletions tests/benchmark/test_span.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from typing import Any
from skywalking.trace.context import NoopContext
from skywalking.trace.span import NoopSpan


def test_noopspan_1000(benchmark: Any):
result = benchmark(lambda: [NoopSpan(NoopContext()) for _ in range(1000)])
assert result


def test_noopspan_40000(benchmark: Any):
result = benchmark(lambda: [NoopSpan(NoopContext()) for _ in range(40000)])
assert result


def test_noopspan_200k(benchmark: Any):
result = benchmark(
lambda: [NoopSpan(NoopContext()) for _ in range(200000)])
assert result

0 comments on commit 7dd9a30

Please sign in to comment.