bottlenecks 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bottlenecks/__init__.py +18 -0
- bottlenecks/__main__.py +3 -0
- bottlenecks/cli.py +98 -0
- bottlenecks/code.py +231 -0
- bottlenecks/models.py +89 -0
- bottlenecks/pipeline.py +199 -0
- bottlenecks/render.py +56 -0
- bottlenecks/runtime.py +114 -0
- bottlenecks/system.py +156 -0
- bottlenecks-0.1.0.dist-info/METADATA +108 -0
- bottlenecks-0.1.0.dist-info/RECORD +15 -0
- bottlenecks-0.1.0.dist-info/WHEEL +5 -0
- bottlenecks-0.1.0.dist-info/entry_points.txt +2 -0
- bottlenecks-0.1.0.dist-info/licenses/LICENSE +201 -0
- bottlenecks-0.1.0.dist-info/top_level.txt +1 -0
bottlenecks/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""bottlenecks – find bottlenecks in code, runtime, pipelines and host systems."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.1.0"
|
|
4
|
+
|
|
5
|
+
from .code import analyze_code, analyze_source # noqa: E402
|
|
6
|
+
from .models import Bottleneck, Report # noqa: E402
|
|
7
|
+
from .pipeline import analyze_dag, analyze_pipeline, analyze_stages, critical_path # noqa: E402
|
|
8
|
+
from .render import render # noqa: E402
|
|
9
|
+
from .runtime import load_pstats, profile, profile_callable, profile_script # noqa: E402
|
|
10
|
+
from .system import analyze_metrics, analyze_system, collect_metrics # noqa: E402
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"__version__", "Bottleneck", "Report", "render",
|
|
14
|
+
"analyze_code", "analyze_source",
|
|
15
|
+
"profile", "profile_callable", "profile_script", "load_pstats",
|
|
16
|
+
"analyze_dag", "analyze_stages", "analyze_pipeline", "critical_path",
|
|
17
|
+
"analyze_system", "analyze_metrics", "collect_metrics",
|
|
18
|
+
]
|
bottlenecks/__main__.py
ADDED
bottlenecks/cli.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Command line interface: ``bottlenecks {code,run,pstats,pipeline,system,scan}``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from typing import List, Optional
|
|
9
|
+
|
|
10
|
+
from . import __version__
|
|
11
|
+
from .code import analyze_code
|
|
12
|
+
from .models import SEVERITIES, Report, severity_rank
|
|
13
|
+
from .pipeline import analyze_pipeline
|
|
14
|
+
from .render import RENDERERS, render
|
|
15
|
+
from .runtime import load_pstats, profile_script
|
|
16
|
+
from .system import analyze_system
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _common(p: argparse.ArgumentParser) -> None:
|
|
20
|
+
p.add_argument("-f", "--format", choices=sorted(RENDERERS), default="text")
|
|
21
|
+
p.add_argument("--min-severity", choices=SEVERITIES, default=None, help="hide findings below this severity")
|
|
22
|
+
p.add_argument("--top", type=int, default=None, help="show at most N findings")
|
|
23
|
+
p.add_argument("--fail-on", choices=SEVERITIES, default=None,
|
|
24
|
+
help="exit with code 1 when a finding reaches this severity (CI gate)")
|
|
25
|
+
p.add_argument("-o", "--output", default=None, help="write the report to a file")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
29
|
+
parser = argparse.ArgumentParser(prog="bottlenecks", description="Find bottlenecks in code, runtime, pipelines and systems.")
|
|
30
|
+
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
31
|
+
sub = parser.add_subparsers(dest="command", required=True)
|
|
32
|
+
|
|
33
|
+
p = sub.add_parser("code", help="static analysis of Python sources")
|
|
34
|
+
p.add_argument("paths", nargs="+")
|
|
35
|
+
p.add_argument("--complexity", type=int, default=10)
|
|
36
|
+
p.add_argument("--max-lines", type=int, default=80)
|
|
37
|
+
_common(p)
|
|
38
|
+
|
|
39
|
+
p = sub.add_parser("run", help="profile a Python script with cProfile")
|
|
40
|
+
p.add_argument("script")
|
|
41
|
+
p.add_argument("args", nargs=argparse.REMAINDER)
|
|
42
|
+
p.add_argument("--threshold", type=float, default=0.05, help="min share of total time to report")
|
|
43
|
+
_common(p)
|
|
44
|
+
|
|
45
|
+
p = sub.add_parser("pstats", help="analyse a saved cProfile .prof file")
|
|
46
|
+
p.add_argument("file")
|
|
47
|
+
p.add_argument("--threshold", type=float, default=0.05)
|
|
48
|
+
_common(p)
|
|
49
|
+
|
|
50
|
+
p = sub.add_parser("pipeline", help="analyse a pipeline/DAG JSON spec (tasks and/or stages)")
|
|
51
|
+
p.add_argument("spec", help="JSON file, or '-' for stdin")
|
|
52
|
+
_common(p)
|
|
53
|
+
|
|
54
|
+
p = sub.add_parser("system", help="sample host resources")
|
|
55
|
+
p.add_argument("--interval", type=float, default=0.5)
|
|
56
|
+
p.add_argument("--mount", action="append", default=None)
|
|
57
|
+
_common(p)
|
|
58
|
+
|
|
59
|
+
p = sub.add_parser("scan", help="code + system in one go")
|
|
60
|
+
p.add_argument("paths", nargs="*", default=["."])
|
|
61
|
+
p.add_argument("--interval", type=float, default=0.5)
|
|
62
|
+
_common(p)
|
|
63
|
+
return parser
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def run(argv: Optional[List[str]] = None) -> int:
|
|
67
|
+
args = build_parser().parse_args(argv)
|
|
68
|
+
report: Report
|
|
69
|
+
if args.command == "code":
|
|
70
|
+
report = analyze_code(args.paths, complexity_threshold=args.complexity, length_threshold=args.max_lines)
|
|
71
|
+
elif args.command == "run":
|
|
72
|
+
report = profile_script(args.script, args.args, threshold=args.threshold)
|
|
73
|
+
elif args.command == "pstats":
|
|
74
|
+
report = load_pstats(args.file, threshold=args.threshold)
|
|
75
|
+
elif args.command == "pipeline":
|
|
76
|
+
text = sys.stdin.read() if args.spec == "-" else open(args.spec, encoding="utf-8").read()
|
|
77
|
+
report = analyze_pipeline(json.loads(text))
|
|
78
|
+
elif args.command == "system":
|
|
79
|
+
report = analyze_system(interval=args.interval, mounts=args.mount or ["/"])
|
|
80
|
+
else: # scan
|
|
81
|
+
report = analyze_code(args.paths).merge(analyze_system(interval=args.interval))
|
|
82
|
+
output = render(report, args.format, min_severity=args.min_severity, top=args.top)
|
|
83
|
+
if args.output:
|
|
84
|
+
with open(args.output, "w", encoding="utf-8") as fh:
|
|
85
|
+
fh.write(output + ("" if output.endswith("\n") else "\n"))
|
|
86
|
+
else:
|
|
87
|
+
print(output)
|
|
88
|
+
if args.fail_on and any(severity_rank(b.severity) >= severity_rank(args.fail_on) for b in report.items):
|
|
89
|
+
return 1
|
|
90
|
+
return 0
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def main() -> None:
|
|
94
|
+
sys.exit(run())
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if __name__ == "__main__": # pragma: no cover
|
|
98
|
+
main()
|
bottlenecks/code.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"""Static (AST based) bottleneck detection for Python source code.
|
|
2
|
+
|
|
3
|
+
Rules
|
|
4
|
+
-----
|
|
5
|
+
* ``nested-loops`` loops nested >= 2 deep (polynomial work)
|
|
6
|
+
* ``io-in-loop`` file / network / subprocess / DB / sleep calls inside a loop
|
|
7
|
+
* ``str-concat-in-loop`` ``s += "..."`` string building inside a loop
|
|
8
|
+
* ``regex-in-loop`` ``re.*`` compilation/matching with a literal pattern inside a loop
|
|
9
|
+
* ``sort-in-loop`` ``sorted()`` / ``.sort()`` inside a loop
|
|
10
|
+
* ``high-complexity`` cyclomatic complexity above threshold
|
|
11
|
+
* ``long-function`` function body longer than threshold lines
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import ast
|
|
17
|
+
import os
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Iterable, Iterator, List, Optional, Sequence, Union
|
|
20
|
+
|
|
21
|
+
from .models import Bottleneck, Report
|
|
22
|
+
|
|
23
|
+
IO_CALLS = {
|
|
24
|
+
"open", "input", "print",
|
|
25
|
+
"time.sleep", "sleep",
|
|
26
|
+
"subprocess.run", "subprocess.call", "subprocess.check_call",
|
|
27
|
+
"subprocess.check_output", "subprocess.Popen", "os.system", "os.popen",
|
|
28
|
+
"urllib.request.urlopen", "urlopen",
|
|
29
|
+
"socket.create_connection",
|
|
30
|
+
}
|
|
31
|
+
IO_PREFIXES = ("requests.", "httpx.", "aiohttp.", "urllib.", "boto3.", "sqlite3.", "psycopg", "pymysql.")
|
|
32
|
+
IO_METHODS = {"execute", "executemany", "fetchall", "fetchone", "commit", "query",
|
|
33
|
+
"read_text", "write_text", "read_bytes", "write_bytes", "urlopen",
|
|
34
|
+
"get", "post", "put", "delete", "patch", "request", "send", "recv"}
|
|
35
|
+
IO_METHOD_RECEIVERS = {"requests", "httpx", "session", "client", "conn", "connection",
|
|
36
|
+
"cursor", "cur", "db", "sock", "socket", "s3", "http"}
|
|
37
|
+
REGEX_FUNCS = {"compile", "match", "search", "findall", "finditer", "sub", "split", "fullmatch"}
|
|
38
|
+
DEFAULT_EXCLUDES = {".git", ".venv", "venv", "env", "node_modules", "__pycache__", "build",
|
|
39
|
+
"dist", ".tox", ".mypy_cache", ".pytest_cache", ".worktrees", "site-packages"}
|
|
40
|
+
|
|
41
|
+
LOOP_TYPES = (ast.For, ast.AsyncFor, ast.While, ast.ListComp, ast.SetComp, ast.DictComp, ast.GeneratorExp)
|
|
42
|
+
FUNC_TYPES = (ast.FunctionDef, ast.AsyncFunctionDef)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _dotted(node: ast.AST) -> str:
|
|
46
|
+
parts: List[str] = []
|
|
47
|
+
while isinstance(node, ast.Attribute):
|
|
48
|
+
parts.append(node.attr)
|
|
49
|
+
node = node.value
|
|
50
|
+
if isinstance(node, ast.Name):
|
|
51
|
+
parts.append(node.id)
|
|
52
|
+
elif isinstance(node, ast.Call):
|
|
53
|
+
parts.append(_dotted(node.func) + "()")
|
|
54
|
+
return ".".join(reversed(parts))
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _is_io_call(call: ast.Call) -> Optional[str]:
|
|
58
|
+
name = _dotted(call.func)
|
|
59
|
+
if not name:
|
|
60
|
+
return None
|
|
61
|
+
if name in IO_CALLS or name.startswith(IO_PREFIXES):
|
|
62
|
+
return name
|
|
63
|
+
if isinstance(call.func, ast.Attribute) and call.func.attr in IO_METHODS:
|
|
64
|
+
receiver = _dotted(call.func.value).split(".")[-1].lower().rstrip("()")
|
|
65
|
+
if receiver in IO_METHOD_RECEIVERS or receiver.endswith(("session", "client", "cursor", "conn")):
|
|
66
|
+
return name
|
|
67
|
+
return None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def cyclomatic_complexity(func: ast.AST) -> int:
|
|
71
|
+
"""McCabe-style complexity of a single function (nested defs excluded)."""
|
|
72
|
+
complexity = 1
|
|
73
|
+
for node in _walk_own(func):
|
|
74
|
+
if isinstance(node, (ast.If, ast.IfExp, ast.For, ast.AsyncFor, ast.While,
|
|
75
|
+
ast.ExceptHandler, ast.Assert)):
|
|
76
|
+
complexity += 1
|
|
77
|
+
elif isinstance(node, ast.BoolOp):
|
|
78
|
+
complexity += len(node.values) - 1
|
|
79
|
+
elif isinstance(node, ast.comprehension):
|
|
80
|
+
complexity += 1 + len(node.ifs)
|
|
81
|
+
elif hasattr(ast, "match_case") and isinstance(node, getattr(ast, "match_case")):
|
|
82
|
+
complexity += 1
|
|
83
|
+
return complexity
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _walk_own(func: ast.AST) -> Iterator[ast.AST]:
|
|
87
|
+
"""Walk a function body without descending into nested functions/classes."""
|
|
88
|
+
stack = list(ast.iter_child_nodes(func))
|
|
89
|
+
while stack:
|
|
90
|
+
node = stack.pop()
|
|
91
|
+
yield node
|
|
92
|
+
if isinstance(node, FUNC_TYPES + (ast.ClassDef, ast.Lambda)):
|
|
93
|
+
continue
|
|
94
|
+
stack.extend(ast.iter_child_nodes(node))
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class _Visitor(ast.NodeVisitor):
|
|
98
|
+
def __init__(self, path: str, complexity_threshold: int, length_threshold: int) -> None:
|
|
99
|
+
self.path = path
|
|
100
|
+
self.complexity_threshold = complexity_threshold
|
|
101
|
+
self.length_threshold = length_threshold
|
|
102
|
+
self.loop_depth = 0
|
|
103
|
+
self.func_stack: List[str] = []
|
|
104
|
+
self.items: List[Bottleneck] = []
|
|
105
|
+
self._reported_nesting: set = set()
|
|
106
|
+
|
|
107
|
+
# helpers -----------------------------------------------------------
|
|
108
|
+
def _where(self, node: ast.AST) -> str:
|
|
109
|
+
func = self.func_stack[-1] if self.func_stack else "<module>"
|
|
110
|
+
return f"{self.path}:{getattr(node, 'lineno', 0)} ({func})"
|
|
111
|
+
|
|
112
|
+
def _add(self, node: ast.AST, kind: str, message: str, score: float, suggestion: str, **metrics) -> None:
|
|
113
|
+
self.items.append(Bottleneck("code", kind, self._where(node), message, score, suggestion=suggestion,
|
|
114
|
+
metrics=dict(metrics, loop_depth=self.loop_depth)))
|
|
115
|
+
|
|
116
|
+
# functions ---------------------------------------------------------
|
|
117
|
+
def _visit_func(self, node: ast.AST) -> None:
|
|
118
|
+
name = getattr(node, "name", "<lambda>")
|
|
119
|
+
cc = cyclomatic_complexity(node)
|
|
120
|
+
if cc > self.complexity_threshold:
|
|
121
|
+
score = min(1.0, 0.3 + (cc - self.complexity_threshold) / (self.complexity_threshold * 3))
|
|
122
|
+
self.items.append(Bottleneck(
|
|
123
|
+
"code", "high-complexity", f"{self.path}:{node.lineno} ({name})",
|
|
124
|
+
f"cyclomatic complexity {cc} > {self.complexity_threshold}", score,
|
|
125
|
+
suggestion="split into smaller functions, use lookup tables / early returns",
|
|
126
|
+
metrics={"complexity": cc}))
|
|
127
|
+
end = getattr(node, "end_lineno", node.lineno) or node.lineno
|
|
128
|
+
length = end - node.lineno + 1
|
|
129
|
+
if length > self.length_threshold:
|
|
130
|
+
score = min(0.7, 0.15 + (length - self.length_threshold) / (self.length_threshold * 4))
|
|
131
|
+
self.items.append(Bottleneck(
|
|
132
|
+
"code", "long-function", f"{self.path}:{node.lineno} ({name})",
|
|
133
|
+
f"function is {length} lines long (> {self.length_threshold})", score,
|
|
134
|
+
suggestion="extract cohesive blocks into helpers", metrics={"lines": length}))
|
|
135
|
+
saved_depth, self.loop_depth = self.loop_depth, 0
|
|
136
|
+
self.func_stack.append(name)
|
|
137
|
+
self.generic_visit(node)
|
|
138
|
+
self.func_stack.pop()
|
|
139
|
+
self.loop_depth = saved_depth
|
|
140
|
+
|
|
141
|
+
visit_FunctionDef = _visit_func
|
|
142
|
+
visit_AsyncFunctionDef = _visit_func
|
|
143
|
+
|
|
144
|
+
# loops -------------------------------------------------------------
|
|
145
|
+
def _visit_loop(self, node: ast.AST) -> None:
|
|
146
|
+
self.loop_depth += 1
|
|
147
|
+
if self.loop_depth >= 2:
|
|
148
|
+
key = (self.func_stack[-1] if self.func_stack else "<module>", getattr(node, "lineno", 0))
|
|
149
|
+
if key not in self._reported_nesting:
|
|
150
|
+
self._reported_nesting.add(key)
|
|
151
|
+
score = min(1.0, 0.25 + 0.2 * (self.loop_depth - 1))
|
|
152
|
+
self._add(node, "nested-loops", f"loop nested {self.loop_depth} levels deep (O(n^{self.loop_depth}))",
|
|
153
|
+
score, "index with dict/set, pre-group data, vectorise or use itertools/joins")
|
|
154
|
+
self.generic_visit(node)
|
|
155
|
+
self.loop_depth -= 1
|
|
156
|
+
|
|
157
|
+
visit_For = _visit_loop
|
|
158
|
+
visit_AsyncFor = _visit_loop
|
|
159
|
+
visit_While = _visit_loop
|
|
160
|
+
visit_ListComp = _visit_loop
|
|
161
|
+
visit_SetComp = _visit_loop
|
|
162
|
+
visit_DictComp = _visit_loop
|
|
163
|
+
visit_GeneratorExp = _visit_loop
|
|
164
|
+
|
|
165
|
+
# statements inside loops ------------------------------------------
|
|
166
|
+
def visit_AugAssign(self, node: ast.AugAssign) -> None:
|
|
167
|
+
is_str = isinstance(node.value, ast.JoinedStr) or (
|
|
168
|
+
isinstance(node.value, ast.Constant) and isinstance(node.value.value, str))
|
|
169
|
+
if self.loop_depth and isinstance(node.op, ast.Add) and is_str:
|
|
170
|
+
self._add(node, "str-concat-in-loop", "string concatenation with += inside a loop", 0.3,
|
|
171
|
+
"collect parts in a list and ''.join() them once")
|
|
172
|
+
self.generic_visit(node)
|
|
173
|
+
|
|
174
|
+
def visit_Call(self, node: ast.Call) -> None:
|
|
175
|
+
if self.loop_depth:
|
|
176
|
+
io_name = _is_io_call(node)
|
|
177
|
+
if io_name and io_name != "print":
|
|
178
|
+
score = min(1.0, 0.45 + 0.15 * (self.loop_depth - 1))
|
|
179
|
+
self._add(node, "io-in-loop", f"blocking I/O call '{io_name}' inside a loop", score,
|
|
180
|
+
"batch the operation, cache results, or run concurrently (asyncio/threads)",
|
|
181
|
+
call=io_name)
|
|
182
|
+
name = _dotted(node.func)
|
|
183
|
+
if name.startswith("re.") and name.split(".")[-1] in REGEX_FUNCS and node.args \
|
|
184
|
+
and isinstance(node.args[0], ast.Constant):
|
|
185
|
+
self._add(node, "regex-in-loop", f"'{name}' with a literal pattern inside a loop", 0.2,
|
|
186
|
+
"pre-compile the pattern once outside the loop", call=name)
|
|
187
|
+
if name == "sorted" or (isinstance(node.func, ast.Attribute) and node.func.attr == "sort"):
|
|
188
|
+
self._add(node, "sort-in-loop", "sorting inside a loop", 0.35,
|
|
189
|
+
"sort once outside the loop or use heapq/bisect", call=name)
|
|
190
|
+
self.generic_visit(node)
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def analyze_source(source: str, path: str = "<string>", complexity_threshold: int = 10,
|
|
194
|
+
length_threshold: int = 80) -> List[Bottleneck]:
|
|
195
|
+
"""Analyse a single Python source string."""
|
|
196
|
+
tree = ast.parse(source, filename=path)
|
|
197
|
+
visitor = _Visitor(path, complexity_threshold, length_threshold)
|
|
198
|
+
visitor.visit(tree)
|
|
199
|
+
return visitor.items
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def iter_python_files(paths: Sequence[Union[str, os.PathLike]], excludes: Iterable[str] = DEFAULT_EXCLUDES) -> Iterator[Path]:
|
|
203
|
+
excludes = set(excludes)
|
|
204
|
+
for raw in paths:
|
|
205
|
+
p = Path(raw)
|
|
206
|
+
if p.is_file() and p.suffix == ".py":
|
|
207
|
+
yield p
|
|
208
|
+
elif p.is_dir():
|
|
209
|
+
for root, dirs, files in os.walk(p):
|
|
210
|
+
dirs[:] = sorted(d for d in dirs if d not in excludes and not d.endswith(".egg-info"))
|
|
211
|
+
for f in sorted(files):
|
|
212
|
+
if f.endswith(".py"):
|
|
213
|
+
yield Path(root) / f
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def analyze_code(paths: Union[str, os.PathLike, Sequence[Union[str, os.PathLike]]],
|
|
217
|
+
complexity_threshold: int = 10, length_threshold: int = 80) -> Report:
|
|
218
|
+
"""Scan files/directories and return a :class:`Report` of code bottlenecks."""
|
|
219
|
+
if isinstance(paths, (str, os.PathLike)):
|
|
220
|
+
paths = [paths]
|
|
221
|
+
report = Report()
|
|
222
|
+
files = errors = 0
|
|
223
|
+
for file in iter_python_files(paths):
|
|
224
|
+
files += 1
|
|
225
|
+
try:
|
|
226
|
+
source = file.read_text(encoding="utf-8", errors="replace")
|
|
227
|
+
report.extend(analyze_source(source, str(file), complexity_threshold, length_threshold))
|
|
228
|
+
except (SyntaxError, ValueError):
|
|
229
|
+
errors += 1
|
|
230
|
+
report.summary["code"] = {"files": files, "parse_errors": errors, "findings": len(report.items)}
|
|
231
|
+
return report
|
bottlenecks/models.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Common data model shared by every analyzer."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import asdict, dataclass, field
|
|
6
|
+
from typing import Any, Dict, Iterable, List, Optional
|
|
7
|
+
|
|
8
|
+
SEVERITIES = ("info", "low", "medium", "high", "critical")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def severity_rank(severity: str) -> int:
|
|
12
|
+
"""Return the ordinal of a severity name (unknown names rank lowest)."""
|
|
13
|
+
try:
|
|
14
|
+
return SEVERITIES.index(severity)
|
|
15
|
+
except ValueError:
|
|
16
|
+
return 0
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def severity_from_score(score: float) -> str:
|
|
20
|
+
"""Map a normalised score (0..1) to a severity name."""
|
|
21
|
+
if score >= 0.85:
|
|
22
|
+
return "critical"
|
|
23
|
+
if score >= 0.6:
|
|
24
|
+
return "high"
|
|
25
|
+
if score >= 0.35:
|
|
26
|
+
return "medium"
|
|
27
|
+
if score >= 0.15:
|
|
28
|
+
return "low"
|
|
29
|
+
return "info"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
@dataclass
|
|
33
|
+
class Bottleneck:
|
|
34
|
+
"""A single detected bottleneck."""
|
|
35
|
+
|
|
36
|
+
domain: str # code | runtime | pipeline | system
|
|
37
|
+
kind: str # machine readable rule id, e.g. "io-in-loop"
|
|
38
|
+
location: str # file:line, function name, task name, resource name
|
|
39
|
+
message: str
|
|
40
|
+
score: float # normalised impact 0..1
|
|
41
|
+
severity: str = ""
|
|
42
|
+
suggestion: str = ""
|
|
43
|
+
metrics: Dict[str, Any] = field(default_factory=dict)
|
|
44
|
+
|
|
45
|
+
def __post_init__(self) -> None:
|
|
46
|
+
self.score = max(0.0, min(1.0, float(self.score)))
|
|
47
|
+
if not self.severity:
|
|
48
|
+
self.severity = severity_from_score(self.score)
|
|
49
|
+
|
|
50
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
51
|
+
return asdict(self)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass
|
|
55
|
+
class Report:
|
|
56
|
+
"""A ranked collection of bottlenecks plus analyzer summaries."""
|
|
57
|
+
|
|
58
|
+
items: List[Bottleneck] = field(default_factory=list)
|
|
59
|
+
summary: Dict[str, Any] = field(default_factory=dict)
|
|
60
|
+
|
|
61
|
+
def extend(self, items: Iterable[Bottleneck]) -> "Report":
|
|
62
|
+
self.items.extend(items)
|
|
63
|
+
return self
|
|
64
|
+
|
|
65
|
+
def merge(self, other: "Report") -> "Report":
|
|
66
|
+
self.items.extend(other.items)
|
|
67
|
+
self.summary.update(other.summary)
|
|
68
|
+
return self
|
|
69
|
+
|
|
70
|
+
def ranked(self, min_severity: Optional[str] = None) -> List[Bottleneck]:
|
|
71
|
+
floor = severity_rank(min_severity) if min_severity else 0
|
|
72
|
+
items = [b for b in self.items if severity_rank(b.severity) >= floor]
|
|
73
|
+
return sorted(items, key=lambda b: (-b.score, b.domain, b.location))
|
|
74
|
+
|
|
75
|
+
def top(self, n: int = 10) -> List[Bottleneck]:
|
|
76
|
+
return self.ranked()[:n]
|
|
77
|
+
|
|
78
|
+
def worst_severity(self) -> str:
|
|
79
|
+
if not self.items:
|
|
80
|
+
return "info"
|
|
81
|
+
return max((b.severity for b in self.items), key=severity_rank)
|
|
82
|
+
|
|
83
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
84
|
+
return {
|
|
85
|
+
"summary": self.summary,
|
|
86
|
+
"count": len(self.items),
|
|
87
|
+
"worst_severity": self.worst_severity(),
|
|
88
|
+
"items": [b.to_dict() for b in self.ranked()],
|
|
89
|
+
}
|
bottlenecks/pipeline.py
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"""Pipeline / DAG / queue bottleneck detection.
|
|
2
|
+
|
|
3
|
+
Two models are supported:
|
|
4
|
+
|
|
5
|
+
* **DAG of tasks** – ``[{"name": "build", "duration": 12, "deps": ["fetch"]}, ...]``.
|
|
6
|
+
Computes the critical path, slack, parallelism and fan-in/fan-out choke points.
|
|
7
|
+
* **Stage pipeline** – ``[{"name": "parse", "service_time": 0.2, "workers": 2}, ...]``
|
|
8
|
+
with an ``arrival_rate`` (items / second). Computes utilisation (Little / M/M/c
|
|
9
|
+
style ``rho``) and the throughput-limiting stage.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
from typing import Any, Dict, List, Mapping, Optional, Sequence
|
|
15
|
+
|
|
16
|
+
from .models import Bottleneck, Report
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class CycleError(ValueError):
|
|
20
|
+
"""Raised when the task graph contains a cycle."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def _normalise(tasks: Sequence[Mapping[str, Any]]) -> Dict[str, Dict[str, Any]]:
|
|
24
|
+
graph: Dict[str, Dict[str, Any]] = {}
|
|
25
|
+
for t in tasks:
|
|
26
|
+
name = str(t["name"])
|
|
27
|
+
graph[name] = {"duration": float(t.get("duration", 0.0)), "deps": [str(d) for d in t.get("deps", [])]}
|
|
28
|
+
for name, t in graph.items():
|
|
29
|
+
for d in t["deps"]:
|
|
30
|
+
if d not in graph:
|
|
31
|
+
raise ValueError(f"task '{name}' depends on unknown task '{d}'")
|
|
32
|
+
return graph
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _toposort(graph: Dict[str, Dict[str, Any]]) -> List[str]:
|
|
36
|
+
indeg = {n: len(t["deps"]) for n, t in graph.items()}
|
|
37
|
+
children: Dict[str, List[str]] = {n: [] for n in graph}
|
|
38
|
+
for n, t in graph.items():
|
|
39
|
+
for d in t["deps"]:
|
|
40
|
+
children[d].append(n)
|
|
41
|
+
ready = sorted(n for n, k in indeg.items() if k == 0)
|
|
42
|
+
order: List[str] = []
|
|
43
|
+
while ready:
|
|
44
|
+
n = ready.pop(0)
|
|
45
|
+
order.append(n)
|
|
46
|
+
for c in sorted(children[n]):
|
|
47
|
+
indeg[c] -= 1
|
|
48
|
+
if indeg[c] == 0:
|
|
49
|
+
ready.append(c)
|
|
50
|
+
if len(order) != len(graph):
|
|
51
|
+
raise CycleError("task graph contains a cycle: " + ", ".join(sorted(set(graph) - set(order))))
|
|
52
|
+
return order
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def critical_path(tasks: Sequence[Mapping[str, Any]]) -> Dict[str, Any]:
|
|
56
|
+
"""Return earliest/latest start, slack and the critical path of a task DAG."""
|
|
57
|
+
graph = _normalise(tasks)
|
|
58
|
+
order = _toposort(graph)
|
|
59
|
+
children: Dict[str, List[str]] = {n: [] for n in graph}
|
|
60
|
+
for n, t in graph.items():
|
|
61
|
+
for d in t["deps"]:
|
|
62
|
+
children[d].append(n)
|
|
63
|
+
es: Dict[str, float] = {}
|
|
64
|
+
for n in order:
|
|
65
|
+
es[n] = max((es[d] + graph[d]["duration"] for d in graph[n]["deps"]), default=0.0)
|
|
66
|
+
makespan = max((es[n] + graph[n]["duration"] for n in graph), default=0.0)
|
|
67
|
+
ls: Dict[str, float] = {}
|
|
68
|
+
for n in reversed(order):
|
|
69
|
+
lf = min((ls[c] for c in children[n]), default=makespan)
|
|
70
|
+
ls[n] = lf - graph[n]["duration"]
|
|
71
|
+
slack = {n: round(ls[n] - es[n], 9) for n in graph}
|
|
72
|
+
# walk the critical chain from a zero-slack root following zero-slack successors
|
|
73
|
+
path: List[str] = []
|
|
74
|
+
current: Optional[str] = None
|
|
75
|
+
roots = [n for n in order if not graph[n]["deps"] and slack[n] == 0]
|
|
76
|
+
if roots:
|
|
77
|
+
current = max(roots, key=lambda n: graph[n]["duration"])
|
|
78
|
+
while current is not None:
|
|
79
|
+
path.append(current)
|
|
80
|
+
nxt = [c for c in children[current]
|
|
81
|
+
if slack[c] == 0 and abs(es[c] - (es[current] + graph[current]["duration"])) < 1e-9]
|
|
82
|
+
current = max(nxt, key=lambda n: graph[n]["duration"]) if nxt else None
|
|
83
|
+
total_work = sum(t["duration"] for t in graph.values())
|
|
84
|
+
return {
|
|
85
|
+
"makespan": makespan,
|
|
86
|
+
"total_work": total_work,
|
|
87
|
+
"parallelism": (total_work / makespan) if makespan else 0.0,
|
|
88
|
+
"critical_path": path,
|
|
89
|
+
"earliest_start": es,
|
|
90
|
+
"slack": slack,
|
|
91
|
+
"children": children,
|
|
92
|
+
"graph": graph,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def analyze_dag(tasks: Sequence[Mapping[str, Any]], workers: Optional[int] = None) -> Report:
|
|
97
|
+
"""Detect bottlenecks in a DAG of tasks with durations and dependencies."""
|
|
98
|
+
cp = critical_path(tasks)
|
|
99
|
+
graph, children, makespan = cp["graph"], cp["children"], cp["makespan"]
|
|
100
|
+
report = Report()
|
|
101
|
+
for name in cp["critical_path"]:
|
|
102
|
+
dur = graph[name]["duration"]
|
|
103
|
+
share = dur / makespan if makespan else 0.0
|
|
104
|
+
if share >= 0.1:
|
|
105
|
+
report.items.append(Bottleneck(
|
|
106
|
+
"pipeline", "critical-task", name,
|
|
107
|
+
f"on the critical path, {share:.0%} of makespan ({dur:g} of {makespan:g})",
|
|
108
|
+
min(1.0, 0.2 + share), suggestion="speed up, split or cache this task – it directly bounds total time",
|
|
109
|
+
metrics={"duration": dur, "share": round(share, 4)}))
|
|
110
|
+
total = len(graph)
|
|
111
|
+
for name, kids in children.items():
|
|
112
|
+
descendants = _descendants(name, children)
|
|
113
|
+
if total > 3 and len(descendants) / (total - 1) >= 0.5 and len(kids) >= 2:
|
|
114
|
+
ratio = len(descendants) / (total - 1)
|
|
115
|
+
report.items.append(Bottleneck(
|
|
116
|
+
"pipeline", "choke-point", name,
|
|
117
|
+
f"blocks {len(descendants)} of {total - 1} downstream tasks ({len(kids)} direct)",
|
|
118
|
+
min(1.0, 0.15 + 0.5 * ratio), suggestion="make it fail fast, isolate failures and start independent branches early",
|
|
119
|
+
metrics={"descendants": len(descendants), "direct_children": len(kids)}))
|
|
120
|
+
deps = graph[name]["deps"]
|
|
121
|
+
if len(deps) >= 4:
|
|
122
|
+
report.items.append(Bottleneck(
|
|
123
|
+
"pipeline", "fan-in", name, f"waits for {len(deps)} upstream tasks (join barrier)",
|
|
124
|
+
min(0.8, 0.1 + 0.05 * len(deps)), suggestion="reduce dependencies or allow partial/streaming joins",
|
|
125
|
+
metrics={"deps": len(deps)}))
|
|
126
|
+
parallelism = cp["parallelism"]
|
|
127
|
+
if total >= 3 and parallelism < 1.3:
|
|
128
|
+
report.items.append(Bottleneck(
|
|
129
|
+
"pipeline", "serial-chain", " -> ".join(cp["critical_path"]),
|
|
130
|
+
f"pipeline is essentially serial (parallelism {parallelism:.2f})", 0.5,
|
|
131
|
+
suggestion="break artificial dependencies so independent tasks can run concurrently",
|
|
132
|
+
metrics={"parallelism": round(parallelism, 3)}))
|
|
133
|
+
if workers and parallelism > workers * 1.5:
|
|
134
|
+
report.items.append(Bottleneck(
|
|
135
|
+
"pipeline", "worker-starvation", f"workers={workers}",
|
|
136
|
+
f"available parallelism {parallelism:.1f} far exceeds {workers} workers", 0.45,
|
|
137
|
+
suggestion="add worker slots or prioritise critical-path tasks",
|
|
138
|
+
metrics={"parallelism": round(parallelism, 3), "workers": workers}))
|
|
139
|
+
report.summary["pipeline"] = {
|
|
140
|
+
"tasks": total, "makespan": makespan, "total_work": cp["total_work"],
|
|
141
|
+
"parallelism": round(parallelism, 3), "critical_path": cp["critical_path"],
|
|
142
|
+
}
|
|
143
|
+
return report
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _descendants(name: str, children: Dict[str, List[str]]) -> set:
|
|
147
|
+
seen: set = set()
|
|
148
|
+
stack = list(children[name])
|
|
149
|
+
while stack:
|
|
150
|
+
n = stack.pop()
|
|
151
|
+
if n not in seen:
|
|
152
|
+
seen.add(n)
|
|
153
|
+
stack.extend(children[n])
|
|
154
|
+
return seen
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def analyze_stages(stages: Sequence[Mapping[str, Any]], arrival_rate: Optional[float] = None) -> Report:
|
|
158
|
+
"""Detect the throughput-limiting stage of a linear pipeline.
|
|
159
|
+
|
|
160
|
+
Each stage: ``name``, ``service_time`` (seconds per item) and optional ``workers``.
|
|
161
|
+
"""
|
|
162
|
+
report = Report()
|
|
163
|
+
caps = []
|
|
164
|
+
for s in stages:
|
|
165
|
+
workers = max(1, int(s.get("workers", 1)))
|
|
166
|
+
st = float(s["service_time"])
|
|
167
|
+
capacity = workers / st if st > 0 else float("inf")
|
|
168
|
+
caps.append((str(s["name"]), capacity, workers, st))
|
|
169
|
+
if not caps:
|
|
170
|
+
return report
|
|
171
|
+
throughput = min(c[1] for c in caps)
|
|
172
|
+
rate = arrival_rate if arrival_rate is not None else throughput
|
|
173
|
+
for name, capacity, workers, st in caps:
|
|
174
|
+
rho = rate / capacity if capacity != float("inf") else 0.0
|
|
175
|
+
is_limit = capacity == throughput
|
|
176
|
+
if rho >= 0.7 or is_limit:
|
|
177
|
+
score = min(1.0, rho) if arrival_rate is not None else (0.6 if is_limit else rho * 0.6)
|
|
178
|
+
msg = f"utilisation {rho:.0%} (capacity {capacity:.2f}/s with {workers} worker(s))"
|
|
179
|
+
if rho >= 1:
|
|
180
|
+
msg += " – queue grows without bound"
|
|
181
|
+
report.items.append(Bottleneck(
|
|
182
|
+
"pipeline", "saturated-stage" if rho >= 0.85 else "limiting-stage", name, msg, score,
|
|
183
|
+
suggestion="add workers, reduce service time or buffer/batch upstream",
|
|
184
|
+
metrics={"utilisation": round(rho, 4), "capacity": capacity, "workers": workers, "service_time": st}))
|
|
185
|
+
report.summary["stages"] = {"throughput": throughput, "arrival_rate": rate,
|
|
186
|
+
"limiting_stage": min(caps, key=lambda c: c[1])[0]}
|
|
187
|
+
return report
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def analyze_pipeline(spec: Mapping[str, Any]) -> Report:
|
|
191
|
+
"""Analyse a JSON-like spec containing ``tasks`` and/or ``stages``."""
|
|
192
|
+
report = Report()
|
|
193
|
+
if "tasks" in spec:
|
|
194
|
+
report.merge(analyze_dag(spec["tasks"], workers=spec.get("workers")))
|
|
195
|
+
if "stages" in spec:
|
|
196
|
+
report.merge(analyze_stages(spec["stages"], arrival_rate=spec.get("arrival_rate")))
|
|
197
|
+
if not report.summary:
|
|
198
|
+
raise ValueError("pipeline spec must contain 'tasks' and/or 'stages'")
|
|
199
|
+
return report
|
bottlenecks/render.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""Render reports as text, Markdown or JSON."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from typing import Optional
|
|
7
|
+
|
|
8
|
+
from .models import Report
|
|
9
|
+
|
|
10
|
+
ICONS = {"critical": "!!", "high": "! ", "medium": "~ ", "low": ". ", "info": " "}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def to_json(report: Report, min_severity: Optional[str] = None, top: Optional[int] = None) -> str:
|
|
14
|
+
data = report.to_dict()
|
|
15
|
+
items = [b.to_dict() for b in report.ranked(min_severity)]
|
|
16
|
+
data["items"] = items[:top] if top else items
|
|
17
|
+
return json.dumps(data, indent=2, default=str)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def to_text(report: Report, min_severity: Optional[str] = None, top: Optional[int] = None) -> str:
|
|
21
|
+
items = report.ranked(min_severity)
|
|
22
|
+
if top:
|
|
23
|
+
items = items[:top]
|
|
24
|
+
lines = [f"bottlenecks: {len(report.items)} finding(s), worst={report.worst_severity()}"]
|
|
25
|
+
for b in items:
|
|
26
|
+
lines.append(f"{ICONS.get(b.severity, ' ')}[{b.severity:<8}] {b.score:.2f} {b.domain}/{b.kind} {b.location}")
|
|
27
|
+
lines.append(f" {b.message}")
|
|
28
|
+
if b.suggestion:
|
|
29
|
+
lines.append(f" -> {b.suggestion}")
|
|
30
|
+
if not items:
|
|
31
|
+
lines.append(" no bottlenecks above threshold")
|
|
32
|
+
return "\n".join(lines)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def to_markdown(report: Report, min_severity: Optional[str] = None, top: Optional[int] = None) -> str:
|
|
36
|
+
items = report.ranked(min_severity)
|
|
37
|
+
if top:
|
|
38
|
+
items = items[:top]
|
|
39
|
+
out = ["# Bottlenecks report", "",
|
|
40
|
+
f"Findings: **{len(report.items)}**, worst severity: **{report.worst_severity()}**", "",
|
|
41
|
+
"| Severity | Score | Domain | Kind | Location | Message | Suggestion |",
|
|
42
|
+
"|---|---:|---|---|---|---|---|"]
|
|
43
|
+
for b in items:
|
|
44
|
+
cells = [b.severity, f"{b.score:.2f}", b.domain, b.kind, f"`{b.location}`", b.message, b.suggestion]
|
|
45
|
+
out.append("| " + " | ".join(c.replace("|", "\\|") for c in cells) + " |")
|
|
46
|
+
return "\n".join(out) + "\n"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
RENDERERS = {"text": to_text, "json": to_json, "markdown": to_markdown, "md": to_markdown}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def render(report: Report, fmt: str = "text", min_severity: Optional[str] = None, top: Optional[int] = None) -> str:
|
|
53
|
+
try:
|
|
54
|
+
return RENDERERS[fmt](report, min_severity=min_severity, top=top)
|
|
55
|
+
except KeyError:
|
|
56
|
+
raise ValueError(f"unknown format '{fmt}', choose from {sorted(RENDERERS)}") from None
|
bottlenecks/runtime.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"""Runtime bottleneck detection built on the stdlib :mod:`cProfile`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import cProfile
|
|
6
|
+
import os
|
|
7
|
+
import pstats
|
|
8
|
+
import runpy
|
|
9
|
+
import sys
|
|
10
|
+
from contextlib import contextmanager
|
|
11
|
+
from typing import Any, Callable, Iterator, List, Optional, Sequence, Tuple
|
|
12
|
+
|
|
13
|
+
from .models import Bottleneck, Report
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _label(func: Tuple[str, int, str]) -> str:
|
|
17
|
+
filename, line, name = func
|
|
18
|
+
if filename == "~":
|
|
19
|
+
return name # builtin
|
|
20
|
+
return f"{filename}:{line} ({name})"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def stats_to_report(stats: pstats.Stats, threshold: float = 0.05, top: int = 20,
|
|
24
|
+
include_stdlib: bool = False) -> Report:
|
|
25
|
+
"""Turn :class:`pstats.Stats` into a ranked report.
|
|
26
|
+
|
|
27
|
+
A function is flagged when its *own* time (``tottime``) is at least
|
|
28
|
+
``threshold`` of total runtime, or when it is called very often.
|
|
29
|
+
"""
|
|
30
|
+
raw = stats.stats # type: ignore[attr-defined]
|
|
31
|
+
total = sum(v[2] for v in raw.values()) or 1e-12
|
|
32
|
+
stdlib = os.path.dirname(os.__file__)
|
|
33
|
+
rows = []
|
|
34
|
+
for func, (cc, nc, tt, ct, _callers) in raw.items():
|
|
35
|
+
filename = func[0]
|
|
36
|
+
if not include_stdlib and (filename.startswith(stdlib) or "cProfile" in filename
|
|
37
|
+
or "bottlenecks/runtime" in filename.replace(os.sep, "/")):
|
|
38
|
+
continue
|
|
39
|
+
rows.append((func, cc, nc, tt, ct))
|
|
40
|
+
rows.sort(key=lambda r: r[3], reverse=True)
|
|
41
|
+
report = Report()
|
|
42
|
+
for func, cc, nc, tt, ct in rows[:top]:
|
|
43
|
+
share = tt / total
|
|
44
|
+
if share < threshold:
|
|
45
|
+
continue
|
|
46
|
+
per_call = tt / nc if nc else 0.0
|
|
47
|
+
kind = "hot-function"
|
|
48
|
+
suggestion = "optimise the algorithm, cache results or move work out of the hot path"
|
|
49
|
+
if nc >= 10_000 and per_call < 1e-4:
|
|
50
|
+
kind = "call-overhead"
|
|
51
|
+
suggestion = "inline/vectorise the tiny function or reduce call count (memoise, batch)"
|
|
52
|
+
if cc != nc:
|
|
53
|
+
suggestion += "; function is recursive"
|
|
54
|
+
report.items.append(Bottleneck(
|
|
55
|
+
"runtime", kind, _label(func),
|
|
56
|
+
f"{share:.1%} of own time, {nc} calls, {tt * 1000:.1f} ms self / {ct * 1000:.1f} ms cumulative",
|
|
57
|
+
min(1.0, share * 1.6), suggestion=suggestion,
|
|
58
|
+
metrics={"share": round(share, 4), "calls": nc, "tottime": tt, "cumtime": ct, "per_call": per_call}))
|
|
59
|
+
report.summary["runtime"] = {"total_seconds": round(total, 6), "functions": len(raw),
|
|
60
|
+
"findings": len(report.items)}
|
|
61
|
+
return report
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def profile_callable(func: Callable[..., Any], *args: Any, threshold: float = 0.05, top: int = 20,
|
|
65
|
+
**kwargs: Any) -> Tuple[Any, Report]:
|
|
66
|
+
"""Run ``func(*args, **kwargs)`` under cProfile and return ``(result, report)``."""
|
|
67
|
+
profiler = cProfile.Profile()
|
|
68
|
+
result = profiler.runcall(func, *args, **kwargs)
|
|
69
|
+
return result, stats_to_report(pstats.Stats(profiler), threshold=threshold, top=top)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class _Holder:
|
|
73
|
+
report: Optional[Report] = None
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@contextmanager
|
|
77
|
+
def profile(threshold: float = 0.05, top: int = 20) -> Iterator[_Holder]:
|
|
78
|
+
"""Context manager: ``with profile() as p: ...; p.report``."""
|
|
79
|
+
holder = _Holder()
|
|
80
|
+
profiler = cProfile.Profile()
|
|
81
|
+
profiler.enable()
|
|
82
|
+
try:
|
|
83
|
+
yield holder
|
|
84
|
+
finally:
|
|
85
|
+
profiler.disable()
|
|
86
|
+
holder.report = stats_to_report(pstats.Stats(profiler), threshold=threshold, top=top)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def profile_script(path: str, argv: Optional[Sequence[str]] = None, threshold: float = 0.05,
|
|
90
|
+
top: int = 20) -> Report:
|
|
91
|
+
"""Execute a Python script as ``__main__`` under cProfile."""
|
|
92
|
+
saved_argv, saved_path = sys.argv[:], sys.path[:]
|
|
93
|
+
sys.argv = [path] + list(argv or [])
|
|
94
|
+
sys.path.insert(0, os.path.dirname(os.path.abspath(path)))
|
|
95
|
+
profiler = cProfile.Profile()
|
|
96
|
+
try:
|
|
97
|
+
profiler.enable()
|
|
98
|
+
try:
|
|
99
|
+
runpy.run_path(path, run_name="__main__")
|
|
100
|
+
except SystemExit:
|
|
101
|
+
pass
|
|
102
|
+
finally:
|
|
103
|
+
profiler.disable()
|
|
104
|
+
finally:
|
|
105
|
+
sys.argv, sys.path[:] = saved_argv, saved_path
|
|
106
|
+
return stats_to_report(pstats.Stats(profiler), threshold=threshold, top=top)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def load_pstats(path: str, threshold: float = 0.05, top: int = 20) -> Report:
|
|
110
|
+
"""Analyse a saved ``.prof`` / ``.pstats`` file."""
|
|
111
|
+
return stats_to_report(pstats.Stats(path), threshold=threshold, top=top, include_stdlib=True)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
__all__: List[str] = ["profile", "profile_callable", "profile_script", "load_pstats", "stats_to_report"]
|
bottlenecks/system.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"""Host system bottleneck detection (CPU, memory, swap, disk, I/O wait, load).
|
|
2
|
+
|
|
3
|
+
Uses :mod:`psutil` when installed (``pip install bottlenecks[system]``), otherwise
|
|
4
|
+
falls back to ``/proc`` (Linux) and the stdlib.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import shutil
|
|
11
|
+
import time
|
|
12
|
+
from typing import Any, Dict, List, Optional, Sequence, Tuple
|
|
13
|
+
|
|
14
|
+
from .models import Bottleneck, Report
|
|
15
|
+
|
|
16
|
+
try: # pragma: no cover - optional dependency
|
|
17
|
+
import psutil # type: ignore
|
|
18
|
+
except Exception: # pragma: no cover
|
|
19
|
+
psutil = None # type: ignore
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _read_proc_stat() -> Optional[Tuple[float, float, float]]:
|
|
23
|
+
try:
|
|
24
|
+
with open("/proc/stat", encoding="ascii") as fh:
|
|
25
|
+
fields = [float(x) for x in fh.readline().split()[1:]]
|
|
26
|
+
except (OSError, ValueError):
|
|
27
|
+
return None
|
|
28
|
+
idle = fields[3] + (fields[4] if len(fields) > 4 else 0.0)
|
|
29
|
+
iowait = fields[4] if len(fields) > 4 else 0.0
|
|
30
|
+
return sum(fields), idle, iowait
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _cpu_and_iowait(interval: float) -> Tuple[Optional[float], Optional[float]]:
|
|
34
|
+
if psutil is not None:
|
|
35
|
+
times = psutil.cpu_times_percent(interval=interval)
|
|
36
|
+
return 100.0 - times.idle - getattr(times, "iowait", 0.0), getattr(times, "iowait", None)
|
|
37
|
+
a = _read_proc_stat()
|
|
38
|
+
if a is None:
|
|
39
|
+
return None, None
|
|
40
|
+
time.sleep(interval)
|
|
41
|
+
b = _read_proc_stat()
|
|
42
|
+
if b is None:
|
|
43
|
+
return None, None
|
|
44
|
+
total = b[0] - a[0] or 1.0
|
|
45
|
+
return 100.0 * (1 - (b[1] - a[1]) / total), 100.0 * (b[2] - a[2]) / total
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _meminfo() -> Dict[str, float]:
|
|
49
|
+
if psutil is not None:
|
|
50
|
+
vm, sw = psutil.virtual_memory(), psutil.swap_memory()
|
|
51
|
+
return {"mem_percent": vm.percent, "mem_total": vm.total, "swap_percent": sw.percent}
|
|
52
|
+
info: Dict[str, float] = {}
|
|
53
|
+
try:
|
|
54
|
+
with open("/proc/meminfo", encoding="ascii") as fh:
|
|
55
|
+
for line in fh:
|
|
56
|
+
key, _, rest = line.partition(":")
|
|
57
|
+
info[key] = float(rest.split()[0]) * 1024
|
|
58
|
+
except (OSError, ValueError, IndexError):
|
|
59
|
+
return {}
|
|
60
|
+
total = info.get("MemTotal", 0.0)
|
|
61
|
+
avail = info.get("MemAvailable", info.get("MemFree", 0.0))
|
|
62
|
+
swap_total = info.get("SwapTotal", 0.0)
|
|
63
|
+
out = {"mem_total": total}
|
|
64
|
+
if total:
|
|
65
|
+
out["mem_percent"] = 100.0 * (1 - avail / total)
|
|
66
|
+
if swap_total:
|
|
67
|
+
out["swap_percent"] = 100.0 * (1 - info.get("SwapFree", 0.0) / swap_total)
|
|
68
|
+
return out
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _top_processes(n: int = 5) -> List[Dict[str, Any]]:
|
|
72
|
+
if psutil is None:
|
|
73
|
+
return []
|
|
74
|
+
procs = []
|
|
75
|
+
for p in psutil.process_iter(["pid", "name", "cpu_percent", "memory_percent"]):
|
|
76
|
+
procs.append(p.info)
|
|
77
|
+
procs.sort(key=lambda p: ((p.get("cpu_percent") or 0) + (p.get("memory_percent") or 0)), reverse=True)
|
|
78
|
+
return procs[:n]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def collect_metrics(interval: float = 0.5, mounts: Sequence[str] = ("/",)) -> Dict[str, Any]:
|
|
82
|
+
"""Collect a snapshot of host metrics."""
|
|
83
|
+
cpu, iowait = _cpu_and_iowait(interval)
|
|
84
|
+
metrics: Dict[str, Any] = {"cpu_count": os.cpu_count() or 1, "cpu_percent": cpu, "iowait_percent": iowait}
|
|
85
|
+
try:
|
|
86
|
+
metrics["load_avg"] = os.getloadavg()
|
|
87
|
+
except (OSError, AttributeError):
|
|
88
|
+
metrics["load_avg"] = None
|
|
89
|
+
metrics.update(_meminfo())
|
|
90
|
+
disks = {}
|
|
91
|
+
for m in mounts:
|
|
92
|
+
try:
|
|
93
|
+
du = shutil.disk_usage(m)
|
|
94
|
+
disks[m] = 100.0 * du.used / du.total if du.total else 0.0
|
|
95
|
+
except OSError:
|
|
96
|
+
continue
|
|
97
|
+
metrics["disk_percent"] = disks
|
|
98
|
+
metrics["top_processes"] = _top_processes()
|
|
99
|
+
return metrics
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _ramp(value: float, warn: float, crit: float) -> float:
|
|
103
|
+
"""Score 0.35 at ``warn`` rising to 0.9 at ``crit``."""
|
|
104
|
+
if value < warn:
|
|
105
|
+
return 0.0
|
|
106
|
+
return min(1.0, 0.35 + 0.55 * (value - warn) / max(crit - warn, 1e-9))
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def analyze_metrics(metrics: Dict[str, Any], cpu_warn: float = 80.0, mem_warn: float = 85.0,
|
|
110
|
+
disk_warn: float = 90.0, iowait_warn: float = 15.0, swap_warn: float = 30.0) -> Report:
|
|
111
|
+
"""Turn a metrics snapshot (see :func:`collect_metrics`) into a report."""
|
|
112
|
+
report = Report()
|
|
113
|
+
items = report.items
|
|
114
|
+
cpu = metrics.get("cpu_percent")
|
|
115
|
+
if cpu is not None and (s := _ramp(cpu, cpu_warn, 98.0)):
|
|
116
|
+
items.append(Bottleneck("system", "cpu-saturation", "cpu", f"CPU busy {cpu:.0f}%", s,
|
|
117
|
+
suggestion="profile the top CPU consumers, add cores or reduce parallel jobs",
|
|
118
|
+
metrics={"cpu_percent": cpu}))
|
|
119
|
+
load = metrics.get("load_avg")
|
|
120
|
+
ncpu = metrics.get("cpu_count") or 1
|
|
121
|
+
if load:
|
|
122
|
+
per_core = load[0] / ncpu
|
|
123
|
+
if (s := _ramp(per_core, 1.0, 3.0)):
|
|
124
|
+
items.append(Bottleneck("system", "run-queue", "load", f"1-min load {load[0]:.2f} on {ncpu} cores "
|
|
125
|
+
f"({per_core:.2f}/core)", s,
|
|
126
|
+
suggestion="too many runnable tasks – throttle workers (AIMD) or scale out",
|
|
127
|
+
metrics={"load_avg": list(load), "per_core": per_core}))
|
|
128
|
+
iow = metrics.get("iowait_percent")
|
|
129
|
+
if iow is not None and (s := _ramp(iow, iowait_warn, 50.0)):
|
|
130
|
+
items.append(Bottleneck("system", "io-wait", "disk-io", f"CPU waiting on I/O {iow:.0f}% of the time", s,
|
|
131
|
+
suggestion="faster storage, fewer fsyncs, batch writes or cache reads",
|
|
132
|
+
metrics={"iowait_percent": iow}))
|
|
133
|
+
mem = metrics.get("mem_percent")
|
|
134
|
+
if mem is not None and (s := _ramp(mem, mem_warn, 98.0)):
|
|
135
|
+
items.append(Bottleneck("system", "memory-pressure", "memory", f"memory used {mem:.0f}%", s,
|
|
136
|
+
suggestion="find leaks / large caches, stream data instead of loading it whole",
|
|
137
|
+
metrics={"mem_percent": mem}))
|
|
138
|
+
swap = metrics.get("swap_percent")
|
|
139
|
+
if swap is not None and (s := _ramp(swap, swap_warn, 90.0)):
|
|
140
|
+
items.append(Bottleneck("system", "swapping", "swap", f"swap used {swap:.0f}%", s,
|
|
141
|
+
suggestion="reduce memory footprint – swapping slows everything down",
|
|
142
|
+
metrics={"swap_percent": swap}))
|
|
143
|
+
for mount, pct in (metrics.get("disk_percent") or {}).items():
|
|
144
|
+
if (s := _ramp(pct, disk_warn, 99.0)):
|
|
145
|
+
items.append(Bottleneck("system", "disk-full", mount, f"filesystem {mount} {pct:.0f}% full", s,
|
|
146
|
+
suggestion="clean caches/logs/artifacts or grow the volume",
|
|
147
|
+
metrics={"disk_percent": pct}))
|
|
148
|
+
report.summary["system"] = {k: v for k, v in metrics.items() if k != "top_processes"}
|
|
149
|
+
if metrics.get("top_processes"):
|
|
150
|
+
report.summary["system"]["top_processes"] = metrics["top_processes"]
|
|
151
|
+
return report
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def analyze_system(interval: float = 0.5, mounts: Sequence[str] = ("/",), **thresholds: float) -> Report:
|
|
155
|
+
"""Sample the host and report resource bottlenecks."""
|
|
156
|
+
return analyze_metrics(collect_metrics(interval=interval, mounts=mounts), **thresholds)
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bottlenecks
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Find bottlenecks everywhere: Python code (static), runtime (cProfile), pipelines/DAGs and host system resources
|
|
5
|
+
Author-email: Tom Sapletta <tom@sapletta.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/semcod/bottlenecks
|
|
8
|
+
Project-URL: Repository, https://github.com/semcod/bottlenecks
|
|
9
|
+
Project-URL: Issues, https://github.com/semcod/bottlenecks/issues
|
|
10
|
+
Keywords: bottleneck,performance,profiling,static-analysis,critical-path,dag,monitoring
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
15
|
+
Classifier: Topic :: System :: Monitoring
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: system
|
|
20
|
+
Requires-Dist: psutil>=5.9; extra == "system"
|
|
21
|
+
Provides-Extra: dev
|
|
22
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
23
|
+
Requires-Dist: build>=1.0; extra == "dev"
|
|
24
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
25
|
+
Requires-Dist: goal>=2.1.0; python_version >= "3.12" and extra == "dev"
|
|
26
|
+
Requires-Dist: costs>=0.1.53; python_version >= "3.9" and extra == "dev"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# bottlenecks
|
|
30
|
+
|
|
31
|
+
[](https://pypi.org/project/bottlenecks/)
|
|
32
|
+
[](https://pypi.org/project/bottlenecks/)
|
|
33
|
+
[](LICENSE)
|
|
34
|
+
|
|
35
|
+
Finds bottlenecks in four places, and ranks them all in one report:
|
|
36
|
+
|
|
37
|
+
| Domain | What it finds | How |
|
|
38
|
+
|------------|---------------|-----|
|
|
39
|
+
| `code` | nested loops (O(n^k)), blocking I/O in loops, `+=` string building, regex/sort in loops, high complexity, long functions | static AST analysis |
|
|
40
|
+
| `runtime` | hot functions, call-overhead hotspots, recursion | stdlib `cProfile` |
|
|
41
|
+
| `pipeline` | critical path, choke points, join barriers (fan-in), serial chains, worker starvation, saturated queue stages | DAG and queueing math |
|
|
42
|
+
| `system` | CPU saturation, run queue/load, I/O wait, memory pressure, swapping, full disks | `psutil` (optional) or `/proc` |
|
|
43
|
+
|
|
44
|
+
Each finding is a `Bottleneck` with a `domain`, a `kind`, a `location`, a `score` from 0 to 1, a `severity`, a `suggestion` and raw `metrics`.
|
|
45
|
+
|
|
46
|
+
The core has **no dependencies**. To get richer system data, including top processes, install the `psutil` extra.
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
pip install bottlenecks # core, stdlib only
|
|
52
|
+
pip install "bottlenecks[system]" # + psutil
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## CLI
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
bottlenecks code src/ --top 20 # static analysis
|
|
59
|
+
bottlenecks code src/ --fail-on high # CI gate (exit 1)
|
|
60
|
+
bottlenecks run script.py arg1 arg2 # profile a script
|
|
61
|
+
bottlenecks pstats out.prof # analyse a saved profile
|
|
62
|
+
bottlenecks pipeline pipeline.json -f markdown # DAG / stage analysis
|
|
63
|
+
bottlenecks system --interval 1 # host resources
|
|
64
|
+
bottlenecks scan . # code + system
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Output formats: `text` (default), `json`, `markdown`. Use `-o FILE` to write the report to a file.
|
|
68
|
+
|
|
69
|
+
### Pipeline spec
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"workers": 4,
|
|
74
|
+
"tasks": [
|
|
75
|
+
{"name": "fetch", "duration": 2},
|
|
76
|
+
{"name": "build", "duration": 10, "deps": ["fetch"]},
|
|
77
|
+
{"name": "lint", "duration": 1, "deps": ["fetch"]},
|
|
78
|
+
{"name": "test", "duration": 5, "deps": ["build"]},
|
|
79
|
+
{"name": "publish", "duration": 1, "deps": ["test", "lint"]}
|
|
80
|
+
],
|
|
81
|
+
"arrival_rate": 2.5,
|
|
82
|
+
"stages": [
|
|
83
|
+
{"name": "parse", "service_time": 0.1},
|
|
84
|
+
{"name": "embed", "service_time": 1.0, "workers": 2},
|
|
85
|
+
{"name": "store", "service_time": 0.2}
|
|
86
|
+
]
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Python API
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from bottlenecks import analyze_code, profile, analyze_dag, analyze_system, render
|
|
94
|
+
|
|
95
|
+
report = analyze_code("src/")
|
|
96
|
+
report.merge(analyze_system())
|
|
97
|
+
print(render(report, "text", top=10))
|
|
98
|
+
|
|
99
|
+
with profile(threshold=0.02) as p:
|
|
100
|
+
expensive()
|
|
101
|
+
print(render(p.report))
|
|
102
|
+
|
|
103
|
+
print(analyze_dag(tasks).summary["pipeline"]["critical_path"])
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
Licensed under Apache-2.0.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
bottlenecks/__init__.py,sha256=rIwCP108cCUO5TJ2nUoeiW66xcAgD0iqhwWlPIKYBBU,842
|
|
2
|
+
bottlenecks/__main__.py,sha256=bYt9eEaoRQWdejEHFD8REx9jxVEdZptECFsV7F49Ink,30
|
|
3
|
+
bottlenecks/cli.py,sha256=j3MtOrRlRRPuFVV_1tqdEbGN4fiHFbOMLBp9wM1_3K8,4050
|
|
4
|
+
bottlenecks/code.py,sha256=_dOOgl6IowxnWMqakgyzTkSye_WAnHMMAKdac38E0Vo,10909
|
|
5
|
+
bottlenecks/models.py,sha256=S4LMRFQGd4mkTZVswVB1H5RJYNNWUFCjYfn-qwbySYg,2740
|
|
6
|
+
bottlenecks/pipeline.py,sha256=nVKluyVFKucXOfpBf0CxbU7LoPvT0brdcAZxutk0PGc,9008
|
|
7
|
+
bottlenecks/render.py,sha256=v6QoRMDL8TXKQB6MF4qe3KmNkiNzNx-njfI-XqhfFhU,2280
|
|
8
|
+
bottlenecks/runtime.py,sha256=0POBUgppf-7yEYOwya1pFcY5mvB5yqJ2sncFWQ-A3SQ,4456
|
|
9
|
+
bottlenecks/system.py,sha256=3bH492k9GAbQZPHMdQXdXoNetHWoscQhowVv2HuA51M,6977
|
|
10
|
+
bottlenecks-0.1.0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
11
|
+
bottlenecks-0.1.0.dist-info/METADATA,sha256=OjX7V17-H2OVou1szTxgWv-LaMkVFvHQ5ttOMWrOiCk,4127
|
|
12
|
+
bottlenecks-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
13
|
+
bottlenecks-0.1.0.dist-info/entry_points.txt,sha256=dVLucC5MKgGwx7TmAhILorlmxQHUQsZLSM74HUIjlwY,53
|
|
14
|
+
bottlenecks-0.1.0.dist-info/top_level.txt,sha256=eULp23zGkkfVd06PBIkvVLrioHtOszDKucn5p3dTnCY,12
|
|
15
|
+
bottlenecks-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
bottlenecks
|