Utils
utils
DeferredError
DeferredError(exception)
Stores an exception and raises it at a later time if this object is accessed in any way.
Useful to allow soft-dependencies on imports, so that the ImportError can be raised again later if code actually relies on the missing library. Lifted from https://github.com/aws/sagemaker-python-sdk/blob/e626647692d136155d72cf5b100043af41ab6c43/src/sagemaker/utils.py#L788
Example::
try:
import obscurelib
except ImportError as e:
logger.warning("Failed to import obscurelib. Obscure features will not work.")
obscurelib = DeferredError(e)
Source code in llmeter/utils.py
30 31 32 33 34 35 36 37 | |
__getattr__
__getattr__(name)
Called by Python interpreter before using any method or property on the object.
So this will short-circuit essentially any access to this object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the attribute being accessed |
required |
Source code in llmeter/utils.py
39 40 41 42 43 44 45 46 47 | |
RunningStats
RunningStats(metrics)
Accumulate summary statistics incrementally from individual responses.
Maintains sorted value lists per metric so that percentiles (p50, p90, p99),
averages, and sums can be computed at any point — both mid-run (for live
progress-bar display via :meth:snapshot) and at the end of a run (for the
final :class:~llmeter.results.Result stats via :meth:to_stats).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metrics
|
Sequence[str]
|
Names of numeric response fields to track (e.g.
|
required |
Example::
rs = RunningStats(metrics=["time_to_first_token", "time_to_last_token"])
rs.update({"time_to_first_token": 0.3, "time_to_last_token": 0.8})
rs.update({"time_to_first_token": 0.5, "time_to_last_token": 1.2, "error": None})
rs.to_stats()
# {'failed_requests': 0, ..., 'time_to_first_token-p50': 0.4, ...}
Source code in llmeter/utils.py
111 112 113 114 115 116 117 118 | |
to_stats
to_stats(end_time=None, result_dict=None)
Compute all accumulated statistics as raw numeric values.
This is the single source of truth for stats computation. It is called
once at the end of a run (with all arguments) to produce the full
Result.stats dict, and also called internally by :meth:snapshot
(without arguments) for mid-run progress display.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
end_time
|
datetime | None
|
Wall-clock end time of the run. Used together with
|
None
|
result_dict
|
dict[str, Any] | None
|
Base key-value pairs to include in the output (typically
from |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A flat dictionary of statistics. |
Source code in llmeter/utils.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
update
update(response_dict)
Record one response's metric values.
Call this once per :class:~llmeter.endpoints.base.InvocationResponse
(typically via response.to_dict()). The method extracts each tracked
metric from response_dict, skipping None and NaN values, and
increments the failure counter when an "error" key is present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_dict
|
dict[str, Any]
|
A flat dictionary of response fields, as returned by
|
required |
Example::
rs = RunningStats(metrics=["time_to_first_token"])
rs.update({"time_to_first_token": 0.42, "error": None})
rs.update({"time_to_first_token": None, "error": "timeout"})
assert rs._failed == 1
Source code in llmeter/utils.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
ensure_path
ensure_path(path: ReadablePathLike | WritablePathLike) -> UPath
ensure_path(path: None) -> None
ensure_path(path)
Normalize a path-like argument to a UPath instance.
Converts strings, os.PathLike objects, and UPath instances into a consistent UPath representation. Passes through None unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
ReadablePathLike | WritablePathLike | None
|
A string, path-like object, or None. |
required |
Returns:
| Type | Description |
|---|---|
UPath | None
|
A UPath instance, or None if the input was None. |
Source code in llmeter/utils.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
now_utc
now_utc()
Returns the current UTC datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
Current UTC datetime object |
Source code in llmeter/utils.py
229 230 231 232 233 234 235 | |
summary_stats_from_list
summary_stats_from_list(data, percentiles=(50, 90, 99))
Calculate summary statistics for a sequence of numbers in pure Python
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[int | float]
|
Sequence of numbers |
required |
percentiles
|
Sequence[int]
|
Integer percentiles from 1-99 |
(50, 90, 99)
|
Returns:
stats: Dictionary of descriptive statistics including "average" (the arithmetic mean of the
input data), and the requested percentiles in format "p50", "p90", "p99", etc.
Source code in llmeter/utils.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |