Skip to content

Commit

Permalink
[python] Expose tiledbsoma stats as JSON string or Python-parsed (#2958)
Browse files Browse the repository at this point in the history
* [python] Expose tiledbsoma stats as JSON string or Python dict

* code-review feedback
  • Loading branch information
johnkerl authored Sep 6, 2024
1 parent dd47db9 commit 796c168
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apis/python/src/tiledbsoma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@
tiledbsoma_stats_enable,
tiledbsoma_stats_reset,
)
from .stats import (
tiledbsoma_stats_json,
tiledbsoma_stats_as_py,
)

__version__ = get_implementation_version()

Expand Down
7 changes: 7 additions & 0 deletions apis/python/src/tiledbsoma/pytiledbsoma.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ PYBIND11_MODULE(pytiledbsoma, m) {
py::print(stats);
},
"Print TileDB internal statistics. Lifecycle: experimental.");
m.def(
"tiledbsoma_stats_string",
[]() {
std::string stats = tiledbsoma::stats::dump();
return stats;
},
"Print TileDB internal statistics. Lifecycle: experimental.");

py::class_<PlatformConfig>(m, "PlatformConfig")
.def(py::init<>())
Expand Down
22 changes: 22 additions & 0 deletions apis/python/src/tiledbsoma/stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) 2024 TileDB, Inc.
#
# Licensed under the MIT License.

import json
from typing import Dict, List, Literal, Union, cast

from .pytiledbsoma import tiledbsoma_stats_string

ParsedStats = List[Dict[Literal["counters", "timers"], Dict[str, Union[float, int]]]]


def tiledbsoma_stats_json() -> str:
"""Returns tiledbsoma stats as a JSON string"""
# cast is needed for pybind11 things
return cast(str, tiledbsoma_stats_string())


def tiledbsoma_stats_as_py() -> ParsedStats:
"""Returns tiledbsoma stats as a Python dict"""
# cast is needed for pybind11 things
return cast(ParsedStats, json.loads(tiledbsoma_stats_string()))
14 changes: 14 additions & 0 deletions apis/python/tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tiledbsoma


def test_stats_json():
out = tiledbsoma.tiledbsoma_stats_json()
assert isinstance(out, str)
assert out[0] == "["
assert out[-2] == "]"
assert out[-1] == "\n"


def test_stats_as_py():
out = tiledbsoma.tiledbsoma_stats_as_py()
assert isinstance(out, list)

0 comments on commit 796c168

Please sign in to comment.