|
1 | | -# JIT-Optimization-Engine |
| 1 | +# cloudsealed-jit |
2 | 2 |
|
3 | | -[](https://opensource.org/licenses/MIT) |
4 | | -[](https://www.python.org/downloads/) |
5 | | -[]() |
| 3 | +Detects structural waste in cloud billing exports. |
6 | 4 |
|
7 | | -## 🚀 Overview |
| 5 | +Given a billing export from AWS, GCP or Azure, it models what each day *should* |
| 6 | +have cost, reports the days that did not match, and turns the excess into a |
| 7 | +monthly figure. It is a library, a CLI and an HTTP service. |
8 | 8 |
|
9 | | -**JIT-Optimization-Engine** is a high-performance data processing core designed for **analytical diagnostics** and stochastic optimization. At its heart, the project leverages **LLVM-based Just-In-Time (JIT) compilation** (via Numba) to achieve low-level execution speeds, allowing for the analysis of massive datasets in fractions of a second. |
10 | | - |
11 | | -This engine was engineered to serve as a **Technical Audit and Simulation** layer, capable of processing hundreds of thousands of telemetry records and time-series data to identify computational inefficiencies and latency bottlenecks. |
| 9 | +[](LICENSE) |
| 10 | +[](https://www.python.org/downloads/) |
12 | 11 |
|
13 | 12 | --- |
14 | 13 |
|
15 | | -## 🛠️ Technical Architecture & Key Pillars |
| 14 | +## The problem |
16 | 15 |
|
17 | | -The engine is built upon four pillars of advanced software engineering: |
| 16 | +Cloud cost anomaly detection is usually done by comparing each day against the |
| 17 | +period average and flagging anything beyond two or three standard deviations. |
| 18 | +On billing data that method fails in two specific ways. |
18 | 19 |
|
19 | | -1. **JIT Compilation (Numba/LLVM):** Transforms complex Python functions into native machine code. This allows the engine to perform mathematical and logical calculations with performance comparable to C++, which is essential for processing infrastructure logs without the overhead of the standard Python interpreter. |
20 | | -2. **Massive Parallel Processing:** Utilizes `ProcessPoolExecutor` to distribute the analytical workload across multiple CPU cores, enabling the simultaneous processing of data from high-throughput databases such as **QuestDB**. |
21 | | -3. **Stochastic Simulation Engine:** Implements specialized algorithms for calculating **Z-Score**, **Sharpe Ratio**, and **Expectancy**. In an engineering context, these metrics validate the stability and predictability of the analyzed datasets. |
22 | | -4. **Micro-latency Diagnostics:** Designed for environments where milliseconds matter, capturing performance variations (jitter) that standard monitoring tools often overlook. |
| 20 | +**Standard deviation is inflated by the very spikes you are looking for.** A |
| 21 | +handful of large anomalies raises σ enough to pull themselves back inside the |
| 22 | +threshold, and to hide every smaller anomaly with them. This is the masking |
| 23 | +effect, and it gets worse as the anomalies get bigger. |
23 | 24 |
|
24 | | ---- |
| 25 | +**A flat average ignores the weekly cycle.** Most cloud bills have a pronounced |
| 26 | +weekday/weekend shape. Measured against a flat mean, ordinary Mondays look like |
| 27 | +overspend and ordinary Sundays look like savings. |
25 | 28 |
|
26 | | -## 📈 Application in FinOps & Engineering (CloudSealed) |
| 29 | +## The method |
27 | 30 |
|
28 | | -This script serves as the technological foundation for **Advanced FinOps** diagnostics. While it does not automate refactoring, it provides the **data intelligence** required for: |
| 31 | +**Baseline.** Expected spend for a day is a level term times a weekday term: |
29 | 32 |
|
30 | | -* **Waste Auditing:** Analyzing CPU and Memory consumption logs to prove where legacy code is causing excessive cloud costs. |
31 | | -* **Performance Validation:** Acting as the "benchmark" that compares system efficiency before and after senior-level code refactoring interventions. |
32 | | -* **ROI Simulation:** Accurately quantifying the potential reduction in Cloud Spend when transitioning to high-performance architectures. |
| 33 | +``` |
| 34 | +expected[i] = rolling_median(cost, 7)[i] × dow_factor[weekday(i)] |
| 35 | +``` |
33 | 36 |
|
34 | | ---- |
| 37 | +The rolling median follows growth and step changes without being dragged by |
| 38 | +spikes. The weekday factor is the median ratio of observed spend to the level |
| 39 | +term for that weekday. It is only estimated with at least two full weeks of |
| 40 | +data; below that every factor is 1.0. |
| 41 | + |
| 42 | +**Scoring.** Residuals are scored with a modified z-score built on the median |
| 43 | +absolute deviation: |
| 44 | + |
| 45 | +``` |
| 46 | +z = 0.6745 × (x − baseline) / MAD |
| 47 | +``` |
| 48 | + |
| 49 | +The 0.6745 constant makes MAD a consistent estimator of σ for normal data, so |
| 50 | +the score keeps the familiar "number of deviations" reading while tolerating |
| 51 | +contamination in roughly half the sample. Days at or above |z| = 3.5 are |
| 52 | +reported — the threshold recommended by Iglewicz & Hoaglin (1993). |
| 53 | + |
| 54 | +**Waste.** Only positive excess counts. Waste percentage is the share of total |
| 55 | +spend sitting above the baseline on anomalous days, which converts directly to |
| 56 | +currency instead of being a count of unusual days. |
| 57 | + |
| 58 | +**Recommendations.** Each carries a figure derived from the series itself, |
| 59 | +normalised to 30 days, and states its assumption in the description. Estimates |
| 60 | +that depend on facts the analyser cannot observe — whether a workload is |
| 61 | +production, whether a commitment is acceptable — are labelled conditional |
| 62 | +rather than presented as findings. |
35 | 63 |
|
36 | | -## ⚡ Quick Start |
| 64 | +## Does it actually work better? |
37 | 65 |
|
38 | | -### Prerequisites |
39 | | -* Python 3.9+ |
40 | | -* Libraries: `pandas`, `numpy`, `numba`, `requests`, `pytz` |
| 66 | +Yes, and it is measured, not asserted. `benchmarks/masking_benchmark.py` builds |
| 67 | +synthetic bills whose anomalies are known by construction and scores this |
| 68 | +method against the textbook mean+standard-deviation approach: |
| 69 | + |
| 70 | +| scenario | textbook F1 | this method F1 | |
| 71 | +|---|---|---| |
| 72 | +| masking (scale estimator) | 0.667 | **0.923** | |
| 73 | +| seasonality (baseline) | 0.667 | **1.000** | |
| 74 | +| end-to-end | 0.667 | **1.000** | |
| 75 | + |
| 76 | +Full derivation and reproduction steps in [METHODOLOGY.md](METHODOLOGY.md); the |
| 77 | +design of the codebase is in [architecture.md](architecture.md). The benchmark |
| 78 | +runs in CI (`--check`) and fails the build if the advantage ever regresses. |
| 79 | + |
| 80 | +## Install |
41 | 81 |
|
42 | | -### Installation & Execution |
43 | 82 | ```bash |
44 | | -# Clone the repository |
45 | | -git clone [https://github.com/cloudsealed/JIT-Optimization-Engine.git](https://github.com/cloudsealed/JIT-Optimization-Engine.git) |
| 83 | +pip install cloudsealed-jit # library + CLI |
| 84 | +pip install "cloudsealed-jit[jit]" # + numba-compiled kernels |
| 85 | +pip install "cloudsealed-jit[jit,api]" # + HTTP service |
| 86 | +``` |
| 87 | + |
| 88 | +`numba` is optional. Without it the kernels run on pure NumPy and the results |
| 89 | +are identical; only large inputs get slower. |
| 90 | + |
| 91 | +## Use |
| 92 | + |
| 93 | +### CLI |
| 94 | + |
| 95 | +```bash |
| 96 | +cloudsealed-jit billing-export.csv |
| 97 | +cloudsealed-jit billing-export.csv --json > findings.json |
| 98 | +cat export.csv | cloudsealed-jit - --type cost-forecast |
| 99 | +``` |
| 100 | + |
| 101 | +### Library |
| 102 | + |
| 103 | +```python |
| 104 | +from cloudsealed_jit import parse_billing_csv, analyze |
| 105 | + |
| 106 | +series = parse_billing_csv(open("export.csv").read()) |
| 107 | +result = analyze(series) |
| 108 | + |
| 109 | +print(result.metrics.wastePercentage) |
| 110 | +for r in result.recommendations: |
| 111 | + print(r.title, r.potentialSavings) |
| 112 | +``` |
| 113 | + |
| 114 | +### HTTP service |
| 115 | + |
| 116 | +```bash |
| 117 | +docker run -p 8091:8091 cloudsealed/jit-optimization-engine |
| 118 | +``` |
| 119 | + |
| 120 | +``` |
| 121 | +GET /health |
| 122 | +POST /v1/analyze-billing |
| 123 | +``` |
| 124 | + |
| 125 | +```bash |
| 126 | +curl -X POST localhost:8091/v1/analyze-billing \ |
| 127 | + -H 'Content-Type: application/json' \ |
| 128 | + -d '{"companyName":"Acme","csvContent":"date,cost\n2026-01-01,100\n..."}' |
| 129 | +``` |
| 130 | + |
| 131 | +Set `JIT_OPTIMIZATION_API_KEY` to require an `X-Api-Key` header. Set |
| 132 | +`JIT_MAX_CSV_BYTES` to change the 64 MB upload ceiling. |
| 133 | + |
| 134 | +Response shape: |
| 135 | + |
| 136 | +```jsonc |
| 137 | +{ |
| 138 | + "anomalies": [ |
| 139 | + { "date": "2026-01-31", "expectedCost": 99.0, "actualCost": 500.0, |
| 140 | + "deviation": 405.05, "zScore": 7.82, "severity": "CRITICAL", |
| 141 | + "description": "Spend above the day-of-week baseline by USD 401.00 (405.1%)." } |
| 142 | + ], |
| 143 | + "metrics": { |
| 144 | + "averageDailyCost": 106.32, |
| 145 | + "stdDeviation": 51.69, |
| 146 | + "sharpeRatio": 2.06, // spend stability: mean / stddev of daily cost |
| 147 | + "wastePercentage": 6.29 // share of total spend above the baseline |
| 148 | + }, |
| 149 | + "recommendations": [ |
| 150 | + { "title": "...", "description": "...", "potentialSavings": 200.5, "effort": "MEDIUM" } |
| 151 | + ], |
| 152 | + "summary": "..." |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +`sharpeRatio` is a **spend stability ratio** — mean daily cost divided by its |
| 157 | +standard deviation, the reciprocal of the coefficient of variation. Higher |
| 158 | +means more predictable spend. It is named for the field in the consuming API |
| 159 | +contract; it is not a risk-adjusted return. |
| 160 | + |
| 161 | +## Supported exports |
| 162 | + |
| 163 | +| Provider | Date column | Cost column | |
| 164 | +|---|---|---| |
| 165 | +| AWS Cost and Usage Report | `lineItem/UsageStartDate` | `lineItem/UnblendedCost` | |
| 166 | +| GCP billing export | `usage_start_time` | `cost` | |
| 167 | +| Azure cost export | `Date`, `UsageDateTime` | `Cost`, `CostInBillingCurrency` | |
| 168 | +| Generic | heuristic | heuristic | |
| 169 | + |
| 170 | +Line items are aggregated to calendar days. Days with no line items are |
| 171 | +inserted as zero-spend days rather than skipped. Rows that cannot be parsed are |
| 172 | +counted and reported in the summary rather than dropped silently. |
| 173 | + |
| 174 | +## Development |
| 175 | + |
| 176 | +```bash |
| 177 | +pip install -e ".[jit,api,dev]" |
| 178 | +pytest |
| 179 | +``` |
| 180 | + |
| 181 | +The test suite builds synthetic exports whose correct answer is known in |
| 182 | +advance — a known spike at a known date, a known weekend-idle service, a stable |
| 183 | +series that must produce no findings — so the assertions test behaviour rather |
| 184 | +than the current output. |
46 | 185 |
|
47 | | -# Install dependencies |
48 | | -pip install pandas numpy numba requests pytz |
| 186 | +## License |
49 | 187 |
|
50 | | -# Run the diagnostic engine |
51 | | -python main.py |
| 188 | +MIT. See [LICENSE](LICENSE). |
0 commit comments