Skip to content

Commit b72de7e

Browse files
committed
Initial draft: insights
1 parent 5679006 commit b72de7e

File tree

20 files changed

+769
-0
lines changed

20 files changed

+769
-0
lines changed
Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
import {useEffect, useState} from "react";
2+
import {Circle, Group, Layer, Line, Rect, Stage, Text} from "react-konva";
3+
4+
5+
6+
7+
8+
9+
10+
11+
const baseColors = ['hsl(4, 72%, 63.8%)', 'hsl(340, 65.6%, 57.2%)', 'hsl(291, 51.2%, 46.2%)', 'hsl(262, 41.6%, 51.7%)', 'hsl(231, 38.4%, 52.8%)', 'hsl(207, 72%, 59.4%)', 'hsl(199, 78.4%, 52.8%)', 'hsl(187, 80%, 46.2%)', 'hsl(174, 80%, 31.9%)', 'hsl(122, 31.2%, 53.9%)', 'hsl(88, 40%, 58.3%)', 'hsl(66, 56%, 59.4%)', 'hsl(45, 80%, 56.1%)', 'hsl(36, 80%, 55%)', 'hsl(14, 80%, 62.7%)', 'hsl(16, 20%, 41.8%)', 'hsl(200, 14.4%, 50.6%)', 'hsl(54, 80%, 68.2%)']
12+
const variantColors = ['hsl(4, 57.6%, 82.94%)', 'hsl(340, 52.48%, 74.36%)', 'hsl(291, 40.96%, 60.06%)', 'hsl(262, 33.28%, 67.21000000000001%)', 'hsl(231, 30.72%, 68.64%)', 'hsl(207, 57.6%, 77.22%)', 'hsl(199, 62.720000000000006%, 68.64%)', 'hsl(187, 64%, 60.06%)', 'hsl(174, 64%, 41.47%)', 'hsl(122, 24.96%, 70.07%)', 'hsl(88, 32%, 75.78999999999999%)', 'hsl(66, 44.8%, 77.22%)', 'hsl(45, 64%, 72.93%)', 'hsl(36, 64%, 71.5%)', 'hsl(14, 64%, 81.51%)', 'hsl(16, 16%, 54.339999999999996%)', 'hsl(200, 11.52%, 65.78%)', 'hsl(54, 64%, 88.66%)']
13+
14+
export const TraceViewer = ({traceData,width, height, onSelect}) => {
15+
const [showExecutionOnly, setShowExecutionOnly] = useState(true)
16+
const [data, setData] = useState([])
17+
let [scaleX, setScaleX] = useState(1);
18+
let [timeRange, setTimeRange] = useState([0, 1]);
19+
let [initialScaleX, setInitialScaleX] = useState(1)
20+
const [maxTime, setMaxTime] = useState(1)
21+
const [threads, setThreads] = useState([])
22+
const [categoryColors, setCategoryColors] = useState({})
23+
const [canvasWidth, setCanvasWidth] = useState(0)
24+
const [dataWidth,setDataWidth]= useState(0)
25+
useEffect(()=> {
26+
if (traceData) {
27+
let executionRecord = traceData.filter((d) => d.category === "Execution" && d.name === "run")
28+
let filteredData = showExecutionOnly ? traceData.filter((d) => d.start >= executionRecord[0].start && d.start <= executionRecord[0].start + executionRecord[0].duration) : traceData
29+
let analyzedData = filteredData.reduce((acc, d) => {
30+
let event = d
31+
if (!acc[d.tid]) {
32+
acc[d.tid] = []
33+
}
34+
for (let i = 0; i < acc[d.tid].length; i++) {
35+
let lastElementInRow = acc[d.tid][i][acc[d.tid][i].length - 1]
36+
let end = lastElementInRow.start + lastElementInRow.duration
37+
if (d.start >= end) {
38+
acc[d.tid][i].push(event)
39+
return acc
40+
}
41+
}
42+
acc[d.tid].push([event])
43+
return acc
44+
}, {})
45+
let numRows = 0
46+
let maxTime = 0
47+
let minTime = filteredData.reduce((acc, d) => Math.min(acc, d.start), Infinity)
48+
let categoryColors = {}
49+
let data = []
50+
let threads = []
51+
for (let tid in analyzedData) {
52+
threads.push({"threadId": tid, "startRow": numRows})
53+
for (let row in analyzedData[tid]) {
54+
for (let entryIdx in analyzedData[tid][row]) {
55+
let entry = analyzedData[tid][row][entryIdx]
56+
maxTime = Math.max(maxTime, entry.start + entry.duration - minTime)
57+
if (entry.category) {
58+
if (!categoryColors[entry.category + "::" + entry.name]) {
59+
let idx = Object.keys(categoryColors).length % baseColors.length
60+
categoryColors[entry.category + "::" + entry.name] = baseColors[idx]
61+
}
62+
}
63+
data.push({...entry, start: entry.start - minTime, duration: entry.duration, row: numRows,})
64+
}
65+
numRows++
66+
}
67+
}
68+
setData(data)
69+
const newScaleX = dataWidth / maxTime;
70+
setScaleX(newScaleX)
71+
setInitialScaleX(newScaleX)
72+
setTimeRange([0, maxTime])
73+
setMaxTime(maxTime)
74+
setThreads(threads)
75+
setCategoryColors(categoryColors)
76+
}
77+
},[traceData,showExecutionOnly,dataWidth])
78+
79+
let offsetY = 20
80+
let offsetX = 100
81+
let rowOffset = 20;
82+
useEffect(() => {
83+
setCanvasWidth(width - 20)
84+
setDataWidth(width - 20 - offsetX)
85+
}, [width]);
86+
const [selectedEvent, setSelectedEvent] = useState(null)
87+
const selectEvent = (event) => {
88+
setSelectedEvent(event)
89+
onSelect(event)
90+
}
91+
const formatExtraText = (d) => {
92+
let extra = []
93+
for (let prop in d.extra) {
94+
extra.push({key: prop, value: d.extra[prop]})
95+
}
96+
let extraText = extra.map((d, i) => {
97+
return `${d.key}: ${d.value}`
98+
}).join(", ")
99+
return extraText
100+
}
101+
const handleScroll = (e) => {
102+
if (e.evt.ctrlKey) {
103+
e.evt.preventDefault();
104+
let layerX = e.evt.layerX-offsetX
105+
let timePosition = layerX / scaleX + timeRange[0]
106+
let factor = e.evt.wheelDelta / 100;
107+
let newScaleX = factor > 0 ? scaleX * factor : scaleX / -factor;
108+
if (newScaleX < initialScaleX) {
109+
setScaleX(initialScaleX)
110+
setTimeRange([0, maxTime])
111+
} else {
112+
let newPositionOfScrolledPoint = (timePosition - timeRange[0]) * newScaleX
113+
let diff = layerX - newPositionOfScrolledPoint
114+
var newTimeStart = timeRange[0] - diff / newScaleX
115+
var newTimeEnd = newTimeStart + dataWidth / newScaleX
116+
if (newTimeEnd > maxTime) {
117+
newTimeStart -= newTimeEnd - maxTime
118+
newTimeEnd = maxTime
119+
}
120+
let newTimeRange = [newTimeStart, newTimeEnd]
121+
setScaleX(newScaleX)
122+
setTimeRange(newTimeRange)
123+
}
124+
}
125+
}
126+
127+
const [mouseDownLocation, setMouseDownLocation] = useState(null)
128+
const handleMouseDown = (e) => {
129+
setMouseDownLocation(e.evt.layerX)
130+
}
131+
const handleMouseUp = (e) => {
132+
if (mouseDownLocation) {
133+
let diff = e.evt.layerX - mouseDownLocation;
134+
let newTimeStart = timeRange[0] - diff / scaleX
135+
let newTimeEnd = newTimeStart + dataWidth / scaleX
136+
if (newTimeStart < 0) {
137+
newTimeStart = 0
138+
newTimeEnd = dataWidth / scaleX
139+
}
140+
if (newTimeEnd > maxTime) {
141+
newTimeStart -= newTimeEnd - maxTime
142+
newTimeEnd = maxTime
143+
}
144+
setTimeRange([newTimeStart, newTimeEnd])
145+
setMouseDownLocation(null)
146+
}
147+
}
148+
const handleMouseMove = (e) => {
149+
if (mouseDownLocation) {
150+
let diff = e.evt.layerX - mouseDownLocation;
151+
let newTimeStart = timeRange[0] - diff / scaleX
152+
let newTimeEnd = newTimeStart + dataWidth / scaleX
153+
if (newTimeStart < 0) {
154+
newTimeStart = 0
155+
newTimeEnd = dataWidth / scaleX
156+
}
157+
if (newTimeEnd > maxTime) {
158+
newTimeStart -= newTimeEnd - maxTime
159+
newTimeEnd = maxTime
160+
}
161+
setTimeRange([newTimeStart, newTimeEnd])
162+
setMouseDownLocation(e.evt.layerX)
163+
}
164+
}
165+
const [rows, setRows] = useState([]);
166+
167+
168+
function roundUpToStepSize(value) {
169+
const magnitude = Math.pow(10, Math.floor(Math.log10(value))); // Find the magnitude of the value
170+
const normalized = value / magnitude; // Normalize the value to the range [1, 10)
171+
172+
let step;
173+
if (normalized <= 1) {
174+
step = 1;
175+
} else if (normalized <= 2) {
176+
step = 2;
177+
} else if (normalized <= 5) {
178+
step = 5;
179+
} else {
180+
step = 10;
181+
}
182+
183+
return step * magnitude;
184+
}
185+
186+
const [splitters, setSplitters] = useState([])
187+
const [splitterStep, setSplitterStep] = useState(0)
188+
useEffect(() => {
189+
let numSplitters = dataWidth / 200;
190+
let timeDuration = timeRange[1] - timeRange[0]
191+
let timeStep = roundUpToStepSize(timeDuration / numSplitters)
192+
let firstSplitter = Math.ceil(timeRange[0] / timeStep) * timeStep
193+
let newSplitters = []
194+
for (let i = firstSplitter; i < timeRange[1]; i += timeStep) {
195+
newSplitters.push(i)
196+
}
197+
setSplitters(newSplitters)
198+
setSplitterStep(timeStep)
199+
200+
}, [timeRange]);
201+
const formatTime = (time) => {
202+
if (time < 1000) {
203+
return `${time} us`
204+
} else if (time < 1000000) {
205+
return `${time / 1000} ms`
206+
} else {
207+
return `${time / 1000000} s`
208+
}
209+
}
210+
//useEffect(() => {
211+
// doTest();
212+
//}, []);
213+
return (
214+
<div>
215+
<div style={{maxHeight: height-60, overflowY: "auto"}}>
216+
<Stage width={canvasWidth} height={window.innerHeight}
217+
onWheel={(e) => handleScroll(e)} onMouseDown={handleMouseDown} onMouseUp={handleMouseUp}
218+
onMouseMove={handleMouseMove}>
219+
<Layer>
220+
{
221+
threads.map((t, i) => <Group>
222+
<Rect x={0} y={offsetY + t.startRow * rowOffset - 2} width={canvasWidth} height={1}
223+
fill={"black"}></Rect>
224+
<Text x={0} y={offsetY + t.startRow * rowOffset} text={"Thread " + t.threadId}
225+
fontSize={14} fill={"black"}></Text>
226+
</Group>)
227+
}
228+
{
229+
splitters.map((d, i) => <Group>
230+
<Rect x={offsetX + (d - timeRange[0]) * scaleX} y={0} width={1}
231+
height={window.innerHeight} fill={"black"}></Rect>
232+
<Text x={offsetX + (d - timeRange[0]) * scaleX} y={0}
233+
text={`${formatTime(d)} step: ${formatTime(splitterStep)}`} fontSize={14}
234+
fill={"black"}></Text>
235+
</Group>)
236+
237+
}
238+
{data.map((d, i) => {
239+
let duration = d.duration
240+
if (d.start + duration < timeRange[0] || d.start > timeRange[1]) {
241+
return null
242+
} else {
243+
let correctedStart = d.start < timeRange[0] ? 0 : d.start - timeRange[0]
244+
var correctedDuration = d.start < timeRange[0] ? duration - (timeRange[0] - d.start) : duration
245+
correctedDuration = d.start + duration > timeRange[1] ? duration - (d.start + duration - timeRange[1]) : correctedDuration
246+
if (duration == 0) {
247+
return <Circle x={correctedStart * scaleX+offsetX} y={d.row * rowOffset+offsetY} radius={2} fill={"black"} onClick={()=> selectEvent(d)}></Circle>
248+
} else {
249+
let extraText = formatExtraText(d)
250+
return (<Group onClick={() => selectEvent(d)}>
251+
<Rect x={offsetX + correctedStart * scaleX} y={offsetY + d.row * rowOffset} height={15}
252+
width={correctedDuration * scaleX}
253+
fill={d.category=== "Ignore" ? "white":categoryColors[d.category + "::" + d.name]} stroke={"gray"} strokeWidth={0.4}
254+
onClick={() => console.log(d)}></Rect>
255+
256+
<Text x={offsetX + correctedStart * scaleX} y={offsetY + d.row * rowOffset}
257+
text={`${d.name} (${extraText})`}
258+
width={correctedDuration * scaleX} fontSize={14} fill={"white"} wrap={"none"}
259+
ellipsis={true}></Text>
260+
{d.category === "Ignore" &&
261+
<Group>
262+
<Line points={[offsetX + correctedStart * scaleX,offsetY + d.row * rowOffset,offsetX + correctedStart * scaleX+correctedDuration*scaleX,offsetY + d.row * rowOffset+15]} stroke={"red"} strokeWidth={1}></Line>
263+
<Line points={[offsetX + correctedStart * scaleX,offsetY + d.row * rowOffset+15,offsetX + correctedStart * scaleX+correctedDuration*scaleX,offsetY + d.row * rowOffset]} stroke={"red"} strokeWidth={1}></Line>
264+
</Group>
265+
}
266+
</Group>)
267+
}
268+
}
269+
}
270+
)}
271+
</Layer>
272+
</Stage>
273+
</div>
274+
<div>
275+
<input type="checkbox" checked={showExecutionOnly} onChange={(e) => setShowExecutionOnly(e.target.checked)}/> Show Execution Only
276+
{selectedEvent &&
277+
<div style={{display:"inline"}}><b>Selected Element:</b> Category: {selectedEvent.category} Name: {selectedEvent.name} Duration: {formatTime(selectedEvent.duration)} Metadata: {formatExtraText(selectedEvent)}
278+
</div>
279+
280+
}
281+
</div>
282+
</div>
283+
);
284+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Getting Started with Create React App
2+
3+
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4+
5+
## Available Scripts
6+
7+
In the project directory, you can run:
8+
9+
### `npm start`
10+
11+
Runs the app in the development mode.\
12+
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
13+
14+
The page will reload when you make changes.\
15+
You may also see any lint errors in the console.
16+
17+
### `npm test`
18+
19+
Launches the test runner in the interactive watch mode.\
20+
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
22+
### `npm run build`
23+
24+
Builds the app for production to the `build` folder.\
25+
It correctly bundles React in production mode and optimizes the build for the best performance.
26+
27+
The build is minified and the filenames include the hashes.\
28+
Your app is ready to be deployed!
29+
30+
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31+
32+
### `npm run eject`
33+
34+
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
35+
36+
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37+
38+
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
39+
40+
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
41+
42+
## Learn More
43+
44+
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45+
46+
To learn React, check out the [React documentation](https://reactjs.org/).
47+
48+
### Code Splitting
49+
50+
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
51+
52+
### Analyzing the Bundle Size
53+
54+
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
55+
56+
### Making a Progressive Web App
57+
58+
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
59+
60+
### Advanced Configuration
61+
62+
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
63+
64+
### Deployment
65+
66+
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
67+
68+
### `npm run build` fails to minify
69+
70+
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var path = require('path')
2+
3+
const { override, babelInclude } = require('customize-cra')
4+
5+
module.exports = function (config, env) {
6+
return Object.assign(
7+
config,
8+
override(
9+
babelInclude([
10+
/* transpile (converting to es5) code in src/ and shared component library */
11+
path.resolve('src'),
12+
path.resolve('../common'),
13+
])
14+
)(config, env)
15+
)
16+
}

0 commit comments

Comments
 (0)