Core Engine (alnoms.core)
The orchestration and performance profiling logic for Pre-Deployment Governance.
⏱️ Performance Profiling
Industrial‑grade performance analyzer for algorithm benchmarking.
The Profiler supports:
- Precision timing using
timeit.default_timer - Warmup runs to stabilize CPU cache and branch predictors
- Statistical aggregation (min, mean, median)
- Doubling‑test complexity estimation
- Decorator‑based profiling for normal program flow
- Stress‑suite benchmarking for head‑to‑head comparisons
Attributes:
| Name | Type | Description |
|---|---|---|
repeats |
int
|
Number of timed runs per benchmark. |
warmup |
int
|
Number of untimed warmup runs. |
mode |
str
|
Statistical mode for final timing ('min', 'mean', 'median'). |
Source code in src/alnoms/core/profiler.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 153 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
__init__(repeats=5, warmup=1, mode='min')
Initialize the Profiler with benchmark settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repeats
|
int
|
Number of timed runs per benchmark. |
5
|
warmup
|
int
|
Number of warmup runs to prime CPU cache. |
1
|
mode
|
str
|
Statistical mode ('min', 'mean', 'median'). |
'min'
|
Notes
repeatsis clamped to at least 1.warmupis clamped to at least 0.
Source code in src/alnoms/core/profiler.py
benchmark(func, *args)
Benchmark a function with GC disabled for timing purity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to benchmark. |
required |
*args
|
Any
|
Arguments passed to the function. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Execution time in seconds, aggregated using the configured mode. |
Notes
- Deepcopies arguments to avoid mutation across runs.
- Disables garbage collection to reduce jitter.
Source code in src/alnoms/core/profiler.py
print_analysis(func_name, results)
Print a formatted table from a doubling test.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func_name
|
str
|
Name of the analyzed function. |
required |
results
|
List[Dict[str, Any]]
|
Output from |
required |
Source code in src/alnoms/core/profiler.py
print_decorator_report()
Print a summary table of all decorator‑tracked timings.
Displays
- Function/block label
- Number of calls
- Average time
- Total time
Source code in src/alnoms/core/profiler.py
profile(func)
Decorator for lightweight profiling during normal execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to wrap. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
Wrapped function that records execution time. |
Notes
- Stores timing data under
self._profile_stats[func.__name__].
Source code in src/alnoms/core/profiler.py
run_doubling_test(func, input_gen, start_n=50, rounds=3, timeout=15.0)
Perform doubling analysis to estimate algorithmic complexity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Algorithm under test. |
required |
input_gen
|
Callable
|
Function generating input for size N. |
required |
start_n
|
int
|
Initial input size. |
50
|
rounds
|
int
|
Number of doubling iterations. |
3
|
timeout
|
float
|
Maximum allowed runtime for the entire test. |
15.0
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List[Dict[str, Any]]: A list of records containing: - "N": Input size - "Time": Execution time - "Ratio": T(2N) / T(N) - "Complexity": Estimated Big‑O class |
Notes
- Automatically increases recursion limit for deep algorithms.
- Stops early if timeout is exceeded.
Source code in src/alnoms/core/profiler.py
run_stress_suite(funcs, input_gen, n_values=[1000, 2000, 4000])
Run multiple algorithms across multiple input sizes.
Useful for head‑to‑head comparisons in research, teaching, and performance governance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
funcs
|
Dict[str, Callable]
|
Mapping of function names to callables. |
required |
input_gen
|
Callable
|
Data generator for size N. |
required |
n_values
|
List[int]
|
Input sizes to test. |
[1000, 2000, 4000]
|
Returns:
| Type | Description |
|---|---|
Dict[int, Dict[str, float]]
|
Dict[int, Dict[str, float]]:
Nested mapping of |
Source code in src/alnoms/core/profiler.py
stopwatch(label='Block')
Context manager for precision timing of a code block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
Identifier for the timed block. |
'Block'
|
Yields:
| Name | Type | Description |
|---|---|---|
None |
None
|
Execution of the wrapped block. |
Side Effects
- Records elapsed time under
self._profile_stats[label].
Source code in src/alnoms/core/profiler.py
🧠 Analysis & Decision Engine
Central orchestrator for the Alnoms governance pipeline.
This class coordinates:
- Script execution and dynamic profiling
- Static AST pattern detection
- Loop‑depth and static complexity estimation
- Optional empirical scaling tests
- Metadata‑driven algorithmic recommendations
- Fixer‑based prescriptive remediation
All methods are static and the class is stateless.
Source code in src/alnoms/core/analyzer.py
42 43 44 45 46 47 48 49 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 153 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
analyze_file(path, deep=False, target_override=None, gen_name=None, data_file=None, start_n=50, rounds=3)
staticmethod
Perform full governance analysis on a Python script.
Pipeline
- Execute + profile the script
- Run static AST pattern detection
- Compute loop depth and static complexity
- Optionally run empirical scaling tests
- Integrate DecisionEngine metadata
- Integrate Fixers for prescriptive remediation
- Produce a unified governance report
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the Python script. |
required |
deep
|
bool
|
Whether to run empirical scaling tests. |
False
|
target_override
|
str
|
Explicit function name for empirical tests. |
None
|
gen_name
|
str
|
Name of a standard generator. |
None
|
data_file
|
str
|
Path to a data file. |
None
|
start_n
|
int
|
Initial input size for empirical tests. |
50
|
rounds
|
int
|
Number of doubling rounds. |
3
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A complete governance analysis report. |
Source code in src/alnoms/core/analyzer.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | |
profile_script(path)
staticmethod
Profile a script and extract the top slowest developer functions.
Uses cProfile to gather cumulative execution time and filters out
non‑user code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the Python script. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
A tuple containing: - list: Top 5 slowest functions with timing info. - float: Total cumulative execution time. - module: The executed module object. |
Source code in src/alnoms/core/analyzer.py
run_empirical_test(module, slowest_func_name, gen_name=None, data_file=None, start_n=50, rounds=3, func_ast=None)
staticmethod
Run empirical doubling tests on a target function.
Input data can come from:
- A script-defined
data_gen() - A standard generator in
alnoms.core.generators - A data file loaded via
DataReader
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Any
|
The executed script module. |
required |
slowest_func_name
|
str
|
Function selected for empirical testing. |
required |
gen_name
|
str
|
Name of a standard generator. |
None
|
data_file
|
str
|
Path to a data file. |
None
|
start_n
|
int
|
Initial input size. |
50
|
rounds
|
int
|
Number of doubling rounds. |
3
|
Returns:
| Type | Description |
|---|---|
Optional[List[Dict[str, Any]]]
|
Optional[List[Dict[str, Any]]]: Empirical results or None. |
Source code in src/alnoms/core/analyzer.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
run_script(path)
staticmethod
Execute a Python script in an isolated module namespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to the Python script. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
module |
The executed module object. |
Source code in src/alnoms/core/analyzer.py
Deterministic rule‑based mapping for OSS‑tier algorithm selection.
The DecisionEngine provides a stable, non‑adaptive mapping from detected performance patterns to recommended data‑structure or algorithmic remedies. All identifiers returned by this engine use snake_case to satisfy OSS‑tier test and governance requirements.
Metadata lookup is also performed using snake_case keys, matching the canonical identifiers stored in the MetadataRegistry.
Source code in src/alnoms/core/decision_engine.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 | |
__init__(metadata)
Initialize the decision engine with metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Dict[str, dict]
|
Mapping of snake_case algorithm identifiers to metadata dictionaries. Each metadata entry typically includes complexity, category, tier, and module import path. |
required |
Source code in src/alnoms/core/decision_engine.py
decide(pattern, intent=None)
Primary OSS entrypoint for algorithm selection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Detected performance pattern. |
required |
intent
|
Optional[str]
|
Developer intent for nested loops. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: Snake_case recommended algorithm identifier. |
Source code in src/alnoms/core/decision_engine.py
decide_algorithm(pattern, intent=None)
Return the recommended algorithm identifier (snake_case).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Detected performance pattern identifier. |
required |
intent
|
Optional[str]
|
Developer intent extracted from AST heuristics. Relevant only
for nested‑loop patterns. Examples include:
|
None
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: Snake_case algorithm identifier, or None if no mapping exists. |
Source code in src/alnoms/core/decision_engine.py
decide_fix(pattern, intent=None)
Return a human‑readable fix recommendation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Detected performance pattern. |
required |
intent
|
Optional[str]
|
Developer intent for nested loops. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
Optional[str]: Short prescriptive recommendation string, or None. |
Source code in src/alnoms/core/decision_engine.py
decide_metadata(algorithm)
Retrieve metadata for a recommended algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
algorithm
|
str
|
Snake_case algorithm identifier returned by |
required |
Returns:
| Type | Description |
|---|---|
Optional[dict]
|
Optional[dict]: Metadata dictionary for the algorithm, or None if not found. |
Source code in src/alnoms/core/decision_engine.py
🎲 Data Generators & I/O
Collection of deterministic and high‑performance dataset generators.
These generators are used throughout the Alnoms ecosystem for:
- Algorithm benchmarking
- Worst‑case and best‑case scenario construction
- Empirical scaling tests (doubling tests)
- Teaching and demonstration notebooks
- Reproducible research workflows
All methods are static and side‑effect‑free.
Source code in src/alnoms/core/generators.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
large_scale_dataset(n)
staticmethod
Generate a large dataset optimized for high‑volume research.
Attempts to use NumPy for high‑throughput integer generation. If NumPy
is unavailable, falls back to the pure‑Python random_array generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of integers to generate. |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
List[int]: A list of random integers suitable for large‑scale tests. |
Source code in src/alnoms/core/generators.py
random_array(n, lo=0, hi=1000)
staticmethod
Generate an array of random integers.
This is the default dependency‑free generator used across the OSS tier.
It relies solely on Python's built‑in random module and is suitable
for lightweight benchmarking or environments where NumPy is unavailable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of integers to generate. |
required |
lo
|
int
|
Lower bound of the random range (inclusive). |
0
|
hi
|
int
|
Upper bound of the random range (inclusive). |
1000
|
Returns:
| Type | Description |
|---|---|
List[int]
|
List[int]: A list of |
Source code in src/alnoms/core/generators.py
reverse_sorted_array(n)
staticmethod
Generate a descending array from n‑1 to 0.
This is a convenience wrapper around sorted_array(reverse=True) and
is frequently used to construct worst‑case inputs for algorithms such
as insertion sort or bubble sort.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of elements to generate. |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
List[int]: A descending list of integers. |
Source code in src/alnoms/core/generators.py
sorted_array(n, reverse=False)
staticmethod
Generate a sorted array of integers from 0 to n‑1.
Useful for constructing best‑case or worst‑case inputs for sorting algorithms and search routines.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Number of elements to generate. |
required |
reverse
|
bool
|
If True, return the array in descending order. |
False
|
Returns:
| Type | Description |
|---|---|
List[int]
|
List[int]: A sorted list of integers. |
Source code in src/alnoms/core/generators.py
square_matrices(n)
staticmethod
Generate a pair of N×N matrices filled with constant values.
Designed for benchmarking matrix multiplication algorithms where the computational complexity—not the numerical values—is the primary focus.
Complexity
- Time: O(N²) to initialize both matrices.
- Space: O(N²) for storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
Dimension of each square matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
A tuple |
Source code in src/alnoms/core/generators.py
Utility functions for loading test datasets from files.
All methods are static and designed for predictable, dependency‑free behavior. They support common formats used in algorithm benchmarking, including whitespace‑separated integers, tokens, and raw lines.
Source code in src/alnoms/core/io.py
read_all_ints(path)
staticmethod
Read all whitespace‑separated integers from a file.
The file may contain integers separated by spaces, tabs, or newlines. This format is commonly used for sorting and searching benchmarks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Absolute or relative path to the input file. |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
List[int]: A list of parsed integers. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If any token cannot be parsed as an integer. |
Source code in src/alnoms/core/io.py
read_all_strings(path)
staticmethod
Read all whitespace‑separated tokens from a file.
Useful for loading datasets for Trie benchmarks, MSD/LSD string sorts, and token‑based algorithm tests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Absolute or relative path to the input file. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of string tokens. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
Source code in src/alnoms/core/io.py
read_lines(path)
staticmethod
Read all lines from a file, stripping leading and trailing whitespace.
Empty lines are preserved as empty strings. This is useful for line‑oriented algorithms, text processing, and structured input formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Absolute or relative path to the input file. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: A list of cleaned lines. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |