Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jopel committed Oct 22, 2024
1 parent 8d9a031 commit 929b9d1
Show file tree
Hide file tree
Showing 4 changed files with 486 additions and 0 deletions.
34 changes: 34 additions & 0 deletions benchmark/benchmark_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import subprocess
import venv
import shutil

def create_venv(venv_path: str) -> None:
venv.create(venv_path, with_pip=True)

def install_package(venv_path: str, package: str) -> int:
subprocess.run([f"{venv_path}/bin/pip", "install", package], check=True)

def import_package(venv_path: str, package: str) -> str:
subprocess.run([f"{venv_path}/bin/python", "-X", "importtime", "-c", f"{package}"], check=True)

"""
v0.5.0
import time: self [us] | cumulative | imported package
import time: 297 | 408355 | snowflake.telemetry._internal.exporter.otlp.proto.logs
"""

"""
v0.6.0.dev
import time: self [us] | cumulative | imported package
import time: 320 | 219479 | snowflake.telemetry._internal.exporter.otlp.proto.logs
"""
def main():
venv_path = ".benchmark-import-venv"
create_venv(venv_path)
install_package(venv_path, "./")
print("Import time:")
import_package(venv_path, "import snowflake.telemetry._internal.exporter.otlp.proto.logs")
shutil.rmtree(venv_path)

if __name__ == "__main__":
main()
46 changes: 46 additions & 0 deletions benchmark/benchmark_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from util import get_logs_data

from snowflake.telemetry._internal.opentelemetry.exporter.otlp.proto.common._log_encoder import encode_logs

from opentelemetry.exporter.otlp.proto.common._log_encoder import encode_logs as pb2_encode_logs


# Run with pytest --memray tests/benchmark_memory.py

"""
===================================================================================== MEMRAY REPORT ======================================================================================
Allocation results for benchmark/benchmark_memory.py::test_pb2_encode_logs at the high watermark
📦 Total memory allocated: 10.0MiB
📏 Total allocations: 7
📊 Histogram of allocation sizes: |█ ▄▄█|
🥇 Biggest allocating functions:
- _encode_log:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py:52 -> 6.8MiB
- _encode_resource_logs:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py:88 -> 2.3MiB
- _encode_resource_logs:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py:82 -> 865.0KiB
- _encode_resource_logs:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py:74 -> 34.4KiB
- _encode_log:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py:58 -> 646.0B
Allocation results for benchmark/benchmark_memory.py::test_encode_logs at the high watermark
📦 Total memory allocated: 2.0MiB
📏 Total allocations: 7
📊 Histogram of allocation sizes: |▅ ▂█|
🥇 Biggest allocating functions:
- __iter__:<frozen _collections_abc>:861 -> 1.0MiB
- serialize_message:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/snowflake/telemetry/_internal/serialize/__init__.py:95 -> 488.5KiB
- serialize_bytes:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/snowflake/telemetry/_internal/serialize/__init__.py:75 -> 367.1KiB
- serialize_message:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/snowflake/telemetry/_internal/serialize/__init__.py:95 -> 162.2KiB
- _encode_log:/Users/jopel/workspace/snowflake-telemetry-python/.venv/lib/python3.11/site-packages/snowflake/telemetry/_internal/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py:56 -> 646.0B
"""

logs_data = []
for _ in range(1000):
logs_data.extend(get_logs_data())

def test_encode_logs():
encode_logs(logs_data)

def test_pb2_encode_logs():
pb2_encode_logs(logs_data).SerializeToString()
72 changes: 72 additions & 0 deletions benchmark/benchmark_serialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import google_benchmark as benchmark

from util import get_logs_data, get_metrics_data, get_traces_data

from snowflake.telemetry._internal.opentelemetry.exporter.otlp.proto.common._log_encoder import encode_logs
from snowflake.telemetry._internal.opentelemetry.exporter.otlp.proto.common.metrics_encoder import encode_metrics
from snowflake.telemetry._internal.opentelemetry.exporter.otlp.proto.common.trace_encoder import encode_spans

from opentelemetry.exporter.otlp.proto.common._log_encoder import encode_logs as pb2_encode_logs
from opentelemetry.exporter.otlp.proto.common.metrics_encoder import encode_metrics as pb2_encode_metrics
from opentelemetry.exporter.otlp.proto.common.trace_encoder import encode_spans as pb2_encode_spans

"""
-----------------------------------------------------------------------------
Benchmark Time CPU Iterations
-----------------------------------------------------------------------------
test_bm_serialize_logs_data 78590 ns 78590 ns 8893
test_bm_pb2_serialize_logs_data 96043 ns 96043 ns 7277
test_bm_serialize_metrics_data 132482 ns 132482 ns 5285
test_bm_pb2_serialize_metrics_data 163929 ns 163931 ns 4270
test_bm_serialize_traces_data 103523 ns 103524 ns 6656
test_bm_pb2_serialize_traces_data 132048 ns 132048 ns 5294
"""

def sanity_check():
logs_data = get_logs_data()
metrics_data = get_metrics_data()
traces_data = get_traces_data()

assert encode_logs(logs_data) == pb2_encode_logs(logs_data).SerializeToString()
assert encode_metrics(metrics_data) == pb2_encode_metrics(metrics_data).SerializeToString()
assert encode_spans(traces_data) == pb2_encode_spans(traces_data).SerializeToString()

@benchmark.register
def test_bm_serialize_logs_data(state):
logs_data = get_logs_data()
while state:
encode_logs(logs_data)

@benchmark.register
def test_bm_pb2_serialize_logs_data(state):
logs_data = get_logs_data()
while state:
pb2_encode_logs(logs_data).SerializeToString()

@benchmark.register
def test_bm_serialize_metrics_data(state):
metrics_data = get_metrics_data()
while state:
encode_metrics(metrics_data)

@benchmark.register
def test_bm_pb2_serialize_metrics_data(state):
metrics_data = get_metrics_data()
while state:
pb2_encode_metrics(metrics_data).SerializeToString()

@benchmark.register
def test_bm_serialize_traces_data(state):
traces_data = get_traces_data()
while state:
encode_spans(traces_data)

@benchmark.register
def test_bm_pb2_serialize_traces_data(state):
traces_data = get_traces_data()
while state:
pb2_encode_spans(traces_data).SerializeToString()

if __name__ == "__main__":
sanity_check()
benchmark.main()
Loading

0 comments on commit 929b9d1

Please sign in to comment.