Store and display benchmark results.
| 159 | |
| 160 | |
| 161 | class BenchmarkResults: |
| 162 | """Store and display benchmark results.""" |
| 163 | |
| 164 | def __init__(self): |
| 165 | self.results = [] |
| 166 | |
| 167 | def add(self, name, iterations, duration, chunks=None, bytes_total=None, |
| 168 | memory_start=None, memory_end=None): |
| 169 | throughput = iterations / duration if duration > 0 else 0 |
| 170 | result = { |
| 171 | "name": name, |
| 172 | "iterations": iterations, |
| 173 | "duration_s": round(duration, 4), |
| 174 | "throughput_per_s": round(throughput, 2), |
| 175 | } |
| 176 | if chunks: |
| 177 | result["chunks_per_s"] = round(chunks / duration, 2) |
| 178 | if bytes_total: |
| 179 | result["mb_per_s"] = round(bytes_total / (1024 * 1024) / duration, 2) |
| 180 | if memory_start is not None and memory_end is not None: |
| 181 | result["memory_start_mb"] = round(memory_start / (1024 * 1024), 2) |
| 182 | result["memory_end_mb"] = round(memory_end / (1024 * 1024), 2) |
| 183 | result["memory_delta_mb"] = round((memory_end - memory_start) / (1024 * 1024), 2) |
| 184 | self.results.append(result) |
| 185 | |
| 186 | def display(self): |
| 187 | print("\n" + "=" * 70) |
| 188 | print("BENCHMARK RESULTS") |
| 189 | print("=" * 70) |
| 190 | for result in self.results: |
| 191 | print(f"\n{result['name']}") |
| 192 | print("-" * 50) |
| 193 | for key, value in result.items(): |
| 194 | if key != "name": |
| 195 | print(f" {key}: {value}") |
| 196 | print("\n" + "=" * 70) |
| 197 | |
| 198 | def save_json(self, filepath): |
| 199 | with open(filepath, 'w') as f: |
| 200 | json.dump({ |
| 201 | "timestamp": datetime.now().isoformat(), |
| 202 | "results": self.results |
| 203 | }, f, indent=2) |
| 204 | print(f"Results saved to {filepath}") |
| 205 | |
| 206 | |
| 207 | async def benchmark_worker_streaming_throughput(results, chunk_size=1024, num_chunks=1000): |
no outgoing calls
no test coverage detected