Skip to content

Commit cf17be0

Browse files
committed
Add cache for downloading graphs on compare page
1 parent 55fb96d commit cf17be0

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

site/frontend/src/graph/resolver.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {GraphData, GraphsSelector} from "./data";
2+
import {loadGraphs} from "./api";
3+
4+
/**
5+
* Graph API resolver that contains a cache of downloaded graphs.
6+
* This is important for Vue components that download a graph on mount.
7+
* Without a cache, they would download a graph each time they are destroyed
8+
* and recreated.
9+
*/
10+
export class GraphResolver {
11+
private cache: Dict<GraphData> = {};
12+
13+
public async loadGraph(selector: GraphsSelector): Promise<GraphData> {
14+
const key = `${selector.benchmark};${selector.profile};${selector.scenario};${selector.start};${selector.end};${selector.stat};${selector.kind}`;
15+
if (!this.cache.hasOwnProperty(key)) {
16+
this.cache[key] = await loadGraphs(selector);
17+
}
18+
19+
return this.cache[key];
20+
}
21+
}
22+
23+
/**
24+
* This is essentially a global variable, but it makes the code simpler and
25+
* since we currently don't have any unit tests, we don't really need to avoid
26+
* global variables that much. If needed, it could be provided to Vue components
27+
* from a parent via props or context.
28+
*/
29+
export const GRAPH_RESOLVER = new GraphResolver();

site/frontend/src/pages/compare/compile/table/benchmark-detail.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
} from "../common";
88
import {computed, onMounted, Ref, ref} from "vue";
99
import Tooltip from "../../tooltip.vue";
10-
import {loadGraphs} from "../../../../graph/api";
1110
import {ArtifactDescription} from "../../types";
1211
import {getDateInPast} from "./utils";
1312
import {renderPlots} from "../../../../graph/render";
13+
import {GRAPH_RESOLVER} from "../../../../graph/resolver";
14+
import {GraphKind} from "../../../../graph/data";
1415
1516
const props = defineProps<{
1617
testCase: CompileTestCase;
@@ -27,9 +28,9 @@ async function renderGraph() {
2728
stat: props.metric,
2829
start: getDateInPast(props.artifact),
2930
end: props.artifact.commit,
30-
kind: "raw",
31+
kind: "raw" as GraphKind,
3132
};
32-
const graphData = await loadGraphs(selector);
33+
const graphData = await GRAPH_RESOLVER.loadGraph(selector);
3334
renderPlots(graphData, selector, chartElement.value, {
3435
renderTitle: false,
3536
});

0 commit comments

Comments
 (0)