Results.vala 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /**
  2. * Results - Results aggregation and output for benchmarks
  3. *
  4. * Collects benchmark results and provides formatted output
  5. * for comparing performance across different operations.
  6. *
  7. * @version 0.1
  8. * @since 0.1
  9. */
  10. namespace Implexus.Tools.Perf {
  11. /**
  12. * Collects and displays benchmark results.
  13. *
  14. * Results are organized by benchmark name (e.g., "Container", "Document")
  15. * and can be output as a formatted summary table.
  16. */
  17. public class Results : Object {
  18. /**
  19. * Internal storage for benchmark results.
  20. * Maps benchmark name to list of operation results.
  21. */
  22. private Invercargill.DataStructures.Dictionary<string, Invercargill.DataStructures.Vector<BenchmarkResults>> _results;
  23. /**
  24. * Creates a new Results instance.
  25. */
  26. public Results() {
  27. _results = new Invercargill.DataStructures.Dictionary<string, Invercargill.DataStructures.Vector<BenchmarkResults>>();
  28. }
  29. /**
  30. * Adds a benchmark result.
  31. *
  32. * @param benchmark The benchmark name (e.g., "Container")
  33. * @param result The benchmark result to add
  34. */
  35. public void add(string benchmark, BenchmarkResults result) {
  36. if (!_results.has(benchmark)) {
  37. _results.set(benchmark, new Invercargill.DataStructures.Vector<BenchmarkResults>());
  38. }
  39. _results.get(benchmark).add(result);
  40. }
  41. /**
  42. * Gets all results for a specific benchmark.
  43. *
  44. * @param benchmark The benchmark name
  45. * @return An enumerable of results, or empty if not found
  46. */
  47. public Invercargill.Enumerable<BenchmarkResults> get_results(string benchmark) {
  48. if (!_results.has(benchmark)) {
  49. return new Invercargill.DataStructures.Vector<BenchmarkResults>().as_enumerable();
  50. }
  51. return _results.get(benchmark).as_enumerable();
  52. }
  53. /**
  54. * Gets all benchmark names.
  55. *
  56. * @return An enumerable of benchmark names
  57. */
  58. public Invercargill.Enumerable<string> get_benchmark_names() {
  59. var names = new Invercargill.DataStructures.Vector<string>();
  60. foreach (var key in _results.keys) {
  61. names.add(key);
  62. }
  63. return names.as_enumerable();
  64. }
  65. /**
  66. * Prints a formatted summary of all benchmark results.
  67. */
  68. public void print_summary() {
  69. print("\n");
  70. print("================================================================================\n");
  71. print(" IMPLEXUS PERFORMANCE RESULTS \n");
  72. print("================================================================================\n");
  73. print("\n");
  74. // Track overall statistics
  75. int total_operations = 0;
  76. double total_time = 0;
  77. foreach (var benchmark_name in _results.keys) {
  78. var results_vec = _results.get(benchmark_name);
  79. print("=== %s ===\n", benchmark_name);
  80. // Check if any results are batch operations
  81. bool has_batch = false;
  82. foreach (var result in results_vec) {
  83. if (result.batch_size > 0) {
  84. has_batch = true;
  85. break;
  86. }
  87. }
  88. if (has_batch) {
  89. // Extended format for batch operations
  90. print("%-30s %8s %10s %12s %12s %12s\n",
  91. "Operation", "Iters", "Batch", "Total(ms)", "Avg(ms)", "Items/sec");
  92. print("--------------------------------------------------------------------------------------------------------\n");
  93. foreach (var result in results_vec) {
  94. if (result.batch_size > 0) {
  95. // Batch operation - show batch details
  96. print("%-30s %8d %10d %12.2f %12.4f %12.2f\n",
  97. result.operation,
  98. result.iterations,
  99. result.batch_size,
  100. result.total_time_ms,
  101. result.avg_item_time_ms,
  102. result.items_per_second);
  103. } else {
  104. // Non-batch operation
  105. print("%-30s %8d %10s %12.2f %12.4f %12.2f\n",
  106. result.operation,
  107. result.iterations,
  108. "-",
  109. result.total_time_ms,
  110. result.avg_time_ms,
  111. result.ops_per_second);
  112. }
  113. total_operations += result.total_items;
  114. total_time += result.total_time_ms;
  115. }
  116. } else {
  117. // Standard format
  118. print("%-30s %10s %12s %15s\n", "Operation", "Iterations", "Avg (ms)", "Ops/sec");
  119. print("--------------------------------------------------------------------------------\n");
  120. foreach (var result in results_vec) {
  121. print("%-30s %10d %12.4f %15.2f\n",
  122. result.operation,
  123. result.iterations,
  124. result.avg_time_ms,
  125. result.ops_per_second);
  126. total_operations += result.iterations;
  127. total_time += result.total_time_ms;
  128. }
  129. }
  130. print("\n");
  131. }
  132. // Print overall summary
  133. print("================================================================================\n");
  134. print(" SUMMARY \n");
  135. print("================================================================================\n");
  136. print("Total operations: %d\n", total_operations);
  137. print("Total time: %.2f ms\n", total_time);
  138. print("Overall throughput: %.2f ops/sec\n", total_time > 0 ? (total_operations / total_time) * 1000 : 0);
  139. print("================================================================================\n");
  140. }
  141. /**
  142. * Exports results as JSON for comparison or storage.
  143. *
  144. * @return A JSON string representation of the results
  145. */
  146. public string to_json() {
  147. var sb = new StringBuilder();
  148. sb.append("{\n");
  149. bool first_benchmark = true;
  150. foreach (var benchmark_name in _results.keys) {
  151. var results_vec = _results.get(benchmark_name);
  152. if (!first_benchmark) {
  153. sb.append(",\n");
  154. }
  155. first_benchmark = false;
  156. sb.append(" \"%s\": [\n".printf(escape_json_string(benchmark_name)));
  157. bool first_result = true;
  158. foreach (var result in results_vec) {
  159. if (!first_result) {
  160. sb.append(",\n");
  161. }
  162. first_result = false;
  163. sb.append(" {\n");
  164. sb.append(" \"operation\": \"%s\",\n".printf(escape_json_string(result.operation)));
  165. sb.append(" \"iterations\": %d,\n".printf(result.iterations));
  166. if (result.batch_size > 0) {
  167. sb.append(" \"batch_size\": %d,\n".printf(result.batch_size));
  168. sb.append(" \"total_items\": %d,\n".printf(result.total_items));
  169. }
  170. sb.append(" \"total_time_ms\": %.4f,\n".printf(result.total_time_ms));
  171. sb.append(" \"avg_time_ms\": %.4f,\n".printf(result.avg_time_ms));
  172. if (result.batch_size > 0) {
  173. sb.append(" \"avg_item_time_ms\": %.4f,\n".printf(result.avg_item_time_ms));
  174. sb.append(" \"items_per_second\": %.2f\n".printf(result.items_per_second));
  175. } else {
  176. sb.append(" \"ops_per_second\": %.2f\n".printf(result.ops_per_second));
  177. }
  178. sb.append(" }");
  179. }
  180. sb.append("\n ]");
  181. }
  182. sb.append("\n}\n");
  183. return sb.str;
  184. }
  185. /**
  186. * Exports results to a file.
  187. *
  188. * @param path The file path to write to
  189. * @param format The output format ("text" or "json")
  190. * @throws Error if writing fails
  191. */
  192. public void export(string path, string format = "text") throws Error {
  193. string content;
  194. if (format == "json") {
  195. content = to_json();
  196. } else {
  197. content = format_as_text();
  198. }
  199. FileUtils.set_contents(path, content);
  200. }
  201. /**
  202. * Formats results as plain text suitable for file output.
  203. *
  204. * @return A formatted text string
  205. */
  206. private string format_as_text() {
  207. var sb = new StringBuilder();
  208. sb.append("Implexus Performance Benchmark Results\n");
  209. sb.append("======================================\n");
  210. sb.append("Generated: %s\n\n".printf(new DateTime.now_utc().format("%Y-%m-%d %H:%M:%S UTC")));
  211. foreach (var benchmark_name in _results.keys) {
  212. var results_vec = _results.get(benchmark_name);
  213. sb.append("[%s]\n".printf(benchmark_name));
  214. foreach (var result in results_vec) {
  215. sb.append(" %s: %d iterations, %.4f ms avg, %.2f ops/sec\n".printf(
  216. result.operation,
  217. result.iterations,
  218. result.avg_time_ms,
  219. result.ops_per_second
  220. ));
  221. }
  222. sb.append("\n");
  223. }
  224. return sb.str;
  225. }
  226. /**
  227. * Escapes a string for JSON output.
  228. *
  229. * @param s The string to escape
  230. * @return The escaped string
  231. */
  232. private string escape_json_string(string s) {
  233. return s.replace("\\", "\\\\")
  234. .replace("\"", "\\\"")
  235. .replace("\n", "\\n")
  236. .replace("\r", "\\r")
  237. .replace("\t", "\\t");
  238. }
  239. /**
  240. * Calculates summary statistics for a benchmark.
  241. *
  242. * @param benchmark The benchmark name
  243. * @return A SummaryStats object with total_ops, total_time_ms, and avg_ops_per_sec
  244. */
  245. public SummaryStats? get_summary_stats(string benchmark) {
  246. if (!_results.has(benchmark)) {
  247. return null;
  248. }
  249. int total_ops = 0;
  250. double total_time = 0;
  251. foreach (var result in _results.get(benchmark)) {
  252. total_ops += result.iterations;
  253. total_time += result.total_time_ms;
  254. }
  255. double avg_ops = total_time > 0 ? (total_ops / total_time) * 1000 : 0;
  256. return new SummaryStats(total_ops, total_time, avg_ops);
  257. }
  258. }
  259. /**
  260. * Holds summary statistics for a benchmark.
  261. */
  262. public class SummaryStats : Object {
  263. /**
  264. * Total number of operations performed.
  265. */
  266. public int total_ops { get; private set; }
  267. /**
  268. * Total time in milliseconds.
  269. */
  270. public double total_time_ms { get; private set; }
  271. /**
  272. * Average operations per second.
  273. */
  274. public double avg_ops_per_sec { get; private set; }
  275. /**
  276. * Creates a new SummaryStats instance.
  277. */
  278. public SummaryStats(int total_ops, double total_time_ms, double avg_ops_per_sec) {
  279. this.total_ops = total_ops;
  280. this.total_time_ms = total_time_ms;
  281. this.avg_ops_per_sec = avg_ops_per_sec;
  282. }
  283. }
  284. }