Skip to content

tdfs4ds.profiling

tdfs4ds.profiling

Where a tdfs4ds call spends its time: profile() (client) and run_profile() (database) (0.3.8.0).

::

with tdfs4ds.profile() as p:
    tdfs4ds.roll_out(...)
p.report()                                  # statements and time per phase, client side
tdfs4ds.run_profile(p.call_ids[0], client=p)  # + DBQL: DB time, CPU, I/O, spool, skew; verdict

Three kinds of time are told apart (they need different cures):

  • DB time: DBQL FirstRespTime - StartTime of the statements (the database working);
  • wire time: the client's time in the statement minus its DB time (round trip, transfer);
  • Python time: the wall time of the phase minus the time inside its statements (teradataml, pandas, tdfs4ds code between two statements).

A call dominated by wire time is latency-bound: fewer statements, or running closer to the database, help; better SQL does not. A call dominated by DB time is work-bound: SQL, primary index, partitioning. The phases are named by tdfs4ds.utils.phases (see its docstring for the contract: tag format, names, groups, and the SQL helpers to read them from DBQL).

DBQL writes from a cache (10 minutes by default): a call that just ended reads PENDING with the time to retry. A DBQL that cannot be read, or does not log the user, leaves the client side alone with state saying why (same handling as read_execution_resources).

STATE_OK module-attribute

STATE_OK = 'OK'

STATE_PARTIAL module-attribute

STATE_PARTIAL = 'PARTIAL'

STATE_PENDING module-attribute

STATE_PENDING = 'PENDING'

STATE_NOT_LOGGED module-attribute

STATE_NOT_LOGGED = 'NOT_LOGGED'

STATE_NOT_FOUND module-attribute

STATE_NOT_FOUND = 'NOT_FOUND'

STATE_UNAVAILABLE module-attribute

STATE_UNAVAILABLE = 'UNAVAILABLE'

STATE_CLIENT_ONLY module-attribute

STATE_CLIENT_ONLY = 'CLIENT_ONLY'

DB_COLUMNS module-attribute

DB_COLUMNS = ('DB_STATEMENTS', 'DB_SEC', 'AMP_CPU_SEC', 'IMPACT_CPU_SEC', 'CPU_SKEW_PCT', 'TOTAL_IO', 'IO_SKEW_PCT', 'MAX_SPOOL_BYTES', 'ROWS_RETURNED', 'N_ERRORS', 'N_EXPECTED_ERRORS')

CLIENT_STORE_MAX module-attribute

CLIENT_STORE_MAX = 200

_CLIENT_STORE module-attribute

_CLIENT_STORE: 'collections.OrderedDict' = collections.OrderedDict()

_STORE_LOCK module-attribute

_STORE_LOCK = threading.Lock()

Profile

What :func:profile yields. counts is the underlying StatementCount; call_ids the ids of the tdfs4ds calls made inside the block (outermost calls).

counts instance-attribute

counts = counts

walls instance-attribute

walls: dict = {}

walls_by_call instance-attribute

walls_by_call: dict = {}

calls instance-attribute

calls: list = []

t0 instance-attribute

t0 = time.perf_counter()

started instance-attribute

started = time.time()

ended instance-attribute

ended = None

total_wall instance-attribute

total_wall = None

call_ids property

call_ids: list

total property

total: int

__init__

__init__(counts)

wall

wall() -> float

by_phase

by_phase(call_id=None)

One row per phase: statements, statement_sec (client time inside the statements), wall_sec (the phase's own wall time), python_sec (wall minus statements). The other row takes the wall time outside every phase block. With call_id: the statements and phase time of that call only (no other row of the block: time outside a call belongs to no call).

report

report(fmt='frame')

'frame' (pandas), 'text' or 'html'.

__repr__

__repr__()

_Snapshot

Bases: Profile

The kept client summary of one call: its per-phase frame and when its block ended. Behaves as the Profile it came from for :func:run_profile (no statement list is kept).

ended instance-attribute

ended = ended

counts instance-attribute

counts = None

total_wall instance-attribute

total_wall = float(frame['wall_sec'].sum()) if len(frame) else 0.0

call_ids property

call_ids: list

total property

total: int

walls instance-attribute

walls: dict = {}

walls_by_call instance-attribute

walls_by_call: dict = {}

calls instance-attribute

calls: list = []

t0 instance-attribute

t0 = time.perf_counter()

started instance-attribute

started = time.time()

__init__

__init__(call_id, frame, ended)

wall

wall() -> float

by_phase

by_phase(call_id=None)

report

report(fmt='frame')

'frame' (pandas), 'text' or 'html'.

__repr__

__repr__()

_remember

_remember(call_id, snap) -> None

client_profile

client_profile(call_id)

The client summary kept for call_id by the last :func:profile block that made the call (this process only), or None.

clear_client_profiles

clear_client_profiles() -> None

_order

_order(name) -> int

profile

profile(keep=0)

Profile what a block sends to the database, by phase (see the module docstring).

Built on count_statements(timings=True) plus the phase blocks of tdfs4ds.utils.phases; adds nothing to the statements. Yields a :class:Profile.

client_summary

client_summary(frame) -> dict

The client side alone, when the database figures are not there yet. Pure on the frame: {"statements", "statement_sec", "python_sec", "wall_sec", "text"}.

_elapsed_sql

_elapsed_sql(start='StartTime', end='FirstRespTime') -> str

run_profile_sql

run_profile_sql(call_ids, days=7) -> str

The DBQL read behind :func:run_profile: one row per phase of the call(s). Pure.

latency_verdict

latency_verdict(db_sec, client_statement_sec, wall_sec, n_statements) -> dict

Latency-bound or work-bound? Pure. Shares are of wall_sec (>= statement time).

{"kind": 'latency' | 'work' | 'client' | 'unknown', "db_share", "wire_share", "python_share", "wire_ms_per_statement", "text"}.

merge_phases

merge_phases(client, db)

Join the client frame (Profile.by_phase(), may be None) and the DBQL frame (may be None) on the phase. Pure on frames. Adds wire_sec = client statement time - DB time.

_read_db

_read_db(call_ids, days)

_numeric

_numeric(frame)

The driver returns DECIMAL as decimal.Decimal: measures become float (None -> NaN).

_kept_client

_kept_client(ids)

The client summary kept by :func:profile for every id of ids (merged by phase when several), or None as soon as one is missing.

flush_query_log

flush_query_log() -> dict

FLUSH QUERY LOGGING WITH ALL: ask the database to write DBQL's cache now. Needs the right for it (EXECUTE on the statement; denied for most users). Fail-open: {"flushed", "error"}.

run_profile

run_profile(call_id=None, *, client=None, fmt='frame', days=7, flush_minutes=None, flush=False)

The cost of one tdfs4ds call by phase, from DBQL, merged with a client profile.

call_id: the id of the call (p.call_ids[0]), a list of ids, or None to take them from client (a :func:profile object). client: the Profile (or its by_phase() frame) of the same call: adds the client time, the round trip (client minus database) and the verdict. fmt: 'frame' (pandas; frame.attrs holds info and verdict), 'text', 'html'.

Without client, the summary a :func:profile block of this process kept for the call id is used (the last 200 calls, CLIENT_STORE_MAX): reading the call after DBQL's flush, in the same session, gives the verdict without the Profile object. While DBQL is PENDING the client side is reported at once (info["client_summary"]) and the database figures follow. flush: first issue FLUSH QUERY LOGGING WITH ALL (never done by default; only works for a user who has the right; info["flush"] says what happened).

info["state"]: OK; PARTIAL (the call's last statement is younger than DBQL's flush, totals may grow); PENDING (nothing logged yet, retry_in_sec says when to ask again); NOT_LOGGED / NOT_FOUND (old call, absent: logging off for the user, or not banded); UNAVAILABLE (DBQL cannot be read: client side only, hint names the grant); CLIENT_ONLY (no database read was possible or asked). Never raises on a DBQL failure.

_render

_render(frame, fmt, title, verdict, info=None)