1import { type FileMetadata } from "../types/files.js";
2import { BenchmarkWrapper } from "./benchmarks.js";
3
4export interface Indexed<T> {
5  // The source of the benchmark.
6  source: string;
7  // `source` index.
8  index: number;
9  // The underlying type.
10  value: T;
11}
12
13export type IndexedWrapper = Indexed<BenchmarkWrapper>;
14
15/**
16 * A Benchmarking plotting session.
17 */
18export class Session {
19  // className -> BenchmarkWrapper[]
20  public classGroups: Record<string, IndexedWrapper[]> = {};
21  // BenchmarkWrapper[]
22  public benchmarks: IndexedWrapper[] = [];
23  public fileNames: Set<string> = new Set();
24
25  constructor(public files: FileMetadata[]) {
26    this.initialize();
27  }
28
29  private initialize() {
30    this.classGroups = {};
31    this.fileNames = new Set();
32    this.benchmarks = [];
33
34    for (let i = 0; i < this.files.length; i += 1) {
35      const fileIndex = i;
36      const fileMeta = this.files[fileIndex];
37      for (let j = 0; j < fileMeta.container.benchmarks.length; j += 1) {
38        const wrapper = new BenchmarkWrapper(fileMeta.container.benchmarks[j]);
39        const fileGroupKey = wrapper.className();
40        const classGroup = this.classGroups[fileGroupKey] || [];
41        const item = {
42          source: fileMeta.file.name,
43          index: fileIndex,
44          value: wrapper
45        };
46        classGroup.push(item);
47        // Update
48        this.classGroups[fileGroupKey] = classGroup;
49        this.fileNames.add(fileMeta.file.name);
50        this.benchmarks.push(item);
51      }
52    }
53  }
54
55  static datasetNames(wrappers: IndexedWrapper[]): Set<string> {
56    const names = new Set<string>();
57    for (let i = 0; i < wrappers.length; i += 1) {
58      const wrapper = wrappers[i];
59      names.add(wrapper.value.datasetName());
60    }
61    return names;
62  }
63
64  static sources(wrappers: IndexedWrapper[]): Set<string> {
65    const sources = new Set<string>();
66    for (let i = 0; i < wrappers.length; i += 1) {
67      sources.add(wrappers[i].source);
68    }
69    return sources;
70  }
71}
72