Skip to content

Commit 2016821

Browse files
committed
chore: create story to reproduce issue
1 parent a936de5 commit 2016821

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import React, { useCallback, useEffect, useState } from "react";
2+
3+
import { Meta, StoryFn } from "@storybook/react";
4+
5+
import type { TBlock, TConnection } from "../../../src";
6+
import { GraphBlock, GraphCanvas, useGraph } from "../../../src/react-component";
7+
8+
const NUM_BLOCKS = 2000;
9+
const SUBGRAPH_SIZE = 100;
10+
11+
function generateBlocks(count: number): TBlock[] {
12+
const blocks: TBlock[] = [];
13+
for (let i = 0; i < count; i++) {
14+
blocks.push({
15+
id: "block-" + i,
16+
name: "Block " + i,
17+
x: (i % 20) * 150,
18+
y: Math.floor(i / 20) * 100,
19+
width: 100,
20+
height: 50,
21+
selected: false,
22+
anchors: [],
23+
is: "Block",
24+
});
25+
}
26+
return blocks;
27+
}
28+
29+
function generateConnections(blocks: TBlock[]): TConnection[] {
30+
const connections: TConnection[] = [];
31+
for (let i = 0; i < blocks.length - 1; i++) {
32+
if (i % 2 !== 0) {
33+
connections.push({
34+
id: "conn-" + i,
35+
sourceBlockId: blocks[i].id,
36+
targetBlockId: blocks[i + 1].id,
37+
});
38+
}
39+
}
40+
return connections;
41+
}
42+
43+
const allBlocks = generateBlocks(NUM_BLOCKS);
44+
const allConnections = generateConnections(allBlocks);
45+
const subGraphBlocks = allBlocks.slice(0, SUBGRAPH_SIZE);
46+
47+
const meta: Meta = {
48+
title: "Issues/Issue 86 Performance Drop",
49+
component: GraphCanvas,
50+
};
51+
export default meta;
52+
53+
const Template: StoryFn = () => {
54+
const { graph } = useGraph({});
55+
const [renderTime, setRenderTime] = useState<number | null>(null);
56+
const [currentView, setCurrentView] = useState<"full" | "sub">("full");
57+
58+
useEffect(() => {
59+
if (graph) {
60+
const startTime = performance.now();
61+
graph.setEntities({ blocks: allBlocks, connections: allConnections });
62+
graph.start();
63+
const endTime = performance.now();
64+
setRenderTime(endTime - startTime);
65+
setCurrentView("full");
66+
}
67+
}, [graph]);
68+
69+
const showFullGraph = useCallback(() => {
70+
if (graph) {
71+
const startTime = performance.now();
72+
graph.setEntities({ blocks: allBlocks, connections: allConnections });
73+
const endTime = performance.now();
74+
setRenderTime(endTime - startTime);
75+
setCurrentView("full");
76+
}
77+
}, [graph]);
78+
79+
const showSubGraph = useCallback(() => {
80+
if (graph) {
81+
const startTime = performance.now();
82+
const validSubGraphConnections = generateConnections(subGraphBlocks);
83+
graph.setEntities({ blocks: subGraphBlocks, connections: validSubGraphConnections });
84+
graph.start();
85+
const endTime = performance.now();
86+
setRenderTime(endTime - startTime);
87+
setCurrentView("sub");
88+
}
89+
}, [graph]);
90+
91+
const renderBlock = useCallback(
92+
(g, block) => (
93+
<GraphBlock graph={g} block={block}>
94+
{block.name}
95+
</GraphBlock>
96+
),
97+
[]
98+
);
99+
100+
return (
101+
<div style={{ display: "flex", flexDirection: "column", height: "100vh" }}>
102+
<div style={{ padding: "10px", borderBottom: "1px solid #ccc" }}>
103+
<button onClick={showFullGraph} style={{ marginRight: "10px" }} disabled={currentView === "full"}>
104+
{`Show Full Graph (~${NUM_BLOCKS} blocks)`}
105+
</button>
106+
<button onClick={showSubGraph} style={{ marginRight: "10px" }} disabled={currentView === "sub"}>
107+
{`Show Sub-Graph (~${SUBGRAPH_SIZE} blocks)`}
108+
</button>
109+
{renderTime !== null && (
110+
<span style={{ marginLeft: "20px" }}>{`Last setEntities took: ${renderTime.toFixed(2)} ms`}</span>
111+
)}
112+
<p>Current view: {currentView}</p>
113+
<p>
114+
<strong>Instructions:</strong>
115+
<ol>
116+
<li>Initially, the full graph is shown. Note the time.</li>
117+
<li>Click "Show Sub-Graph". Note the time.</li>
118+
<li>Click "Show Full Graph". Note the time. This is expected to be slow.</li>
119+
<li>Click "Show Sub-Graph" again.</li>
120+
<li>Click "Show Full Graph (with Key Change Workaround)". This will re-mount the graph. Note the time.</li>
121+
</ol>
122+
</p>
123+
</div>
124+
<div style={{ flexGrow: 1, border: "1px solid black", overflow: "hidden" }}>
125+
<GraphCanvas graph={graph} renderBlock={renderBlock} />
126+
</div>
127+
</div>
128+
);
129+
};
130+
131+
export const PerformanceIssueReproduction = Template.bind({});
132+
PerformanceIssueReproduction.args = {};

0 commit comments

Comments
 (0)