This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-python.git


The following commit(s) were added to refs/heads/master by this push:
     new 7dd9a30  perf: ignore uuid and timestamp generation for NoopContext 
(#361)
7dd9a30 is described below

commit 7dd9a30c8b86746ca2e9b42def73d34ed98cca5c
Author: Tsonglew <[email protected]>
AuthorDate: Tue Nov 12 21:03:53 2024 +0800

    perf: ignore uuid and timestamp generation for NoopContext (#361)
---
 .github/PULL_REQUEST_TEMPLATE                      |  2 +-
 poetry.lock                                        | 33 +++++++++++++++++++++-
 pyproject.toml                                     |  1 +
 skywalking/trace/__init__.py                       |  3 +-
 skywalking/trace/context.py                        | 12 ++++++--
 skywalking/trace/segment.py                        | 14 +++++++++
 {skywalking/trace => tests/benchmark}/__init__.py  | 14 ---------
 .../__init__.py => tests/benchmark/test_span.py    | 22 +++++++++------
 8 files changed, 73 insertions(+), 28 deletions(-)

diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE
index 2bbf484..effd268 100644
--- a/.github/PULL_REQUEST_TEMPLATE
+++ b/.github/PULL_REQUEST_TEMPLATE
@@ -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>
diff --git a/poetry.lock b/poetry.lock
index 278b1a7..32b03ff 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -2563,6 +2563,17 @@ all = ["apache-bookkeeper-client (>=4.16.1)", "fastavro 
(==1.7.3)", "grpcio (>=1
 avro = ["fastavro (==1.7.3)"]
 functions = ["apache-bookkeeper-client (>=4.16.1)", "grpcio (>=1.8.2)", 
"prometheus-client", "protobuf (>=3.6.1,<=3.20.3)", "ratelimit"]
 
+[[package]]
+name = "py-cpuinfo"
+version = "9.0.0"
+description = "Get CPU info with pure Python"
+optional = false
+python-versions = "*"
+files = [
+    {file = "py-cpuinfo-9.0.0.tar.gz", hash = 
"sha256:3cdbbf3fac90dc6f118bfd64384f309edeadd902d7c8fb17f02ffa1fc3f49690"},
+    {file = "py_cpuinfo-9.0.0-py3-none-any.whl", hash = 
"sha256:859625bc251f64e21f077d099d4162689c762b5d6a4c3c97553d56241c9674d5"},
+]
+
 [[package]]
 name = "pycodestyle"
 version = "2.9.1"
@@ -2875,6 +2886,26 @@ tomli = {version = ">=1", markers = "python_version < 
\"3.11\""}
 [package.extras]
 dev = ["argcomplete", "attrs (>=19.2)", "hypothesis (>=3.56)", "mock", 
"pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"]
 
+[[package]]
+name = "pytest-benchmark"
+version = "4.0.0"
+description = "A ``pytest`` fixture for benchmarking code. It will group the 
tests into rounds that are calibrated to the chosen timer."
+optional = false
+python-versions = ">=3.7"
+files = [
+    {file = "pytest-benchmark-4.0.0.tar.gz", hash = 
"sha256:fb0785b83efe599a6a956361c0691ae1dbb5318018561af10f3e915caa0048d1"},
+    {file = "pytest_benchmark-4.0.0-py3-none-any.whl", hash = 
"sha256:fdb7db64e31c8b277dff9850d2a2556d8b60bcb0ea6524e36e28ffd7c87f71d6"},
+]
+
+[package.dependencies]
+py-cpuinfo = "*"
+pytest = ">=3.8"
+
+[package.extras]
+aspect = ["aspectlib"]
+elasticsearch = ["elasticsearch"]
+histogram = ["pygal", "pygaljs"]
+
 [[package]]
 name = "pytz"
 version = "2024.2"
@@ -3937,4 +3968,4 @@ sync = ["kafka-python", "requests"]
 [metadata]
 lock-version = "2.0"
 python-versions = ">=3.8, <3.12"
-content-hash = 
"0b095f92380b765e30f77c53577a2b42c7b8917945885ccb9d597040134520db"
+content-hash = 
"957ba196be529a7924617a968a810a5934461cb43936ed738779123e963fcb63"
diff --git a/pyproject.toml b/pyproject.toml
index 881c260..dab159f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -100,6 +100,7 @@ gunicorn = "*"
 uvicorn = "*"
 kafka-python = "*"
 requests = "*"
+pytest-benchmark = "4.0.0"
 
 [tool.poetry.group.plugins.dependencies]
 urllib3 = "1.26.7"
diff --git a/skywalking/trace/__init__.py b/skywalking/trace/__init__.py
index 2c10b94..0e08294 100644
--- a/skywalking/trace/__init__.py
+++ b/skywalking/trace/__init__.py
@@ -15,6 +15,7 @@
 # limitations under the License.
 #
 
+from typing import Optional
 import uuid
 
 from skywalking.utils.counter import AtomicCounter
@@ -23,7 +24,7 @@ _id = AtomicCounter()
 
 
 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):
diff --git a/skywalking/trace/context.py b/skywalking/trace/context.py
index 2fee024..db52935 100644
--- a/skywalking/trace/context.py
+++ b/skywalking/trace/context.py
@@ -23,7 +23,7 @@ from skywalking.profile.profile_status import 
ProfileStatusReference
 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
@@ -95,7 +95,7 @@ class PrimaryEndpoint:
 
 class SpanContext:
     def __init__(self):
-        self.segment: Segment = Segment()
+        self.segment = Segment()
         self._sid: Counter = Counter()
         self._correlation: dict = {}
         self._nspans: int = 0
@@ -285,7 +285,13 @@ class SpanContext:
 
 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)
diff --git a/skywalking/trace/segment.py b/skywalking/trace/segment.py
index 62a0698..31ebddd 100644
--- a/skywalking/trace/segment.py
+++ b/skywalking/trace/segment.py
@@ -68,6 +68,12 @@ class _NewID(ID):
     pass
 
 
+class _NewNoopID(ID):
+
+    def __init__(self):
+        self.value = ''
+
+
 @tostring
 class Segment(object):
     def __init__(self):
@@ -83,3 +89,11 @@ class Segment(object):
         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()]
diff --git a/skywalking/trace/__init__.py b/tests/benchmark/__init__.py
similarity index 74%
copy from skywalking/trace/__init__.py
copy to tests/benchmark/__init__.py
index 2c10b94..b1312a0 100644
--- a/skywalking/trace/__init__.py
+++ b/tests/benchmark/__init__.py
@@ -14,17 +14,3 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-
-import uuid
-
-from skywalking.utils.counter import AtomicCounter
-
-_id = AtomicCounter()
-
-
-class ID(object):
-    def __init__(self, raw_id: str = None):
-        self.value = raw_id or str(uuid.uuid1()).replace('-', '')
-
-    def __str__(self):
-        return self.value
diff --git a/skywalking/trace/__init__.py b/tests/benchmark/test_span.py
similarity index 59%
copy from skywalking/trace/__init__.py
copy to tests/benchmark/test_span.py
index 2c10b94..ebc49b3 100644
--- a/skywalking/trace/__init__.py
+++ b/tests/benchmark/test_span.py
@@ -15,16 +15,22 @@
 # limitations under the License.
 #
 
-import uuid
+from typing import Any
+from skywalking.trace.context import NoopContext
+from skywalking.trace.span import NoopSpan
 
-from skywalking.utils.counter import AtomicCounter
 
-_id = AtomicCounter()
+def test_noopspan_1000(benchmark: Any):
+    result = benchmark(lambda: [NoopSpan(NoopContext()) for _ in range(1000)])
+    assert result
 
 
-class ID(object):
-    def __init__(self, raw_id: str = None):
-        self.value = raw_id or str(uuid.uuid1()).replace('-', '')
+def test_noopspan_40000(benchmark: Any):
+    result = benchmark(lambda: [NoopSpan(NoopContext()) for _ in range(40000)])
+    assert result
 
-    def __str__(self):
-        return self.value
+
+def test_noopspan_200k(benchmark: Any):
+    result = benchmark(
+        lambda: [NoopSpan(NoopContext()) for _ in range(200000)])
+    assert result

Reply via email to