James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 1 | // This file creates a set of two demonstration plots for testing out the |
| 2 | // plotting utility. |
| 3 | // To run this and see a demo, run: |
| 4 | // bazel run -c opt //aos/network/www:web_proxy_demo |
| 5 | // And then open localhost:8080/graph.html |
| 6 | // |
| 7 | // The plots are just the plots of the handler latency / run times of the first |
| 8 | // timer in the the /aos timing report message (this message was chosen because |
| 9 | // it is already being published by the web proxy process, so the demo requires |
| 10 | // very little setup). |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 11 | import * as configuration from 'org_frc971/aos/configuration_generated'; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 12 | import {Line, Plot} from 'org_frc971/aos/network/www/plotter'; |
| 13 | import * as proxy from 'org_frc971/aos/network/www/proxy'; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 14 | import * as web_proxy from 'org_frc971/aos/network/web_proxy_generated'; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 15 | import * as reflection from 'org_frc971/aos/network/www/reflection' |
| 16 | import * as flatbuffers_builder from 'org_frc971/external/com_github_google_flatbuffers/ts/builder'; |
| 17 | import {ByteBuffer} from 'org_frc971/external/com_github_google_flatbuffers/ts/byte-buffer'; |
| 18 | |
| 19 | import Channel = configuration.aos.Channel; |
| 20 | import Connection = proxy.Connection; |
| 21 | import Configuration = configuration.aos.Configuration; |
| 22 | import Parser = reflection.Parser; |
| 23 | import Table = reflection.Table; |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 24 | import SubscriberRequest = web_proxy.aos.web_proxy.SubscriberRequest; |
| 25 | import ChannelRequest = web_proxy.aos.web_proxy.ChannelRequest; |
| 26 | import TransferMethod = web_proxy.aos.web_proxy.TransferMethod; |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 27 | |
| 28 | const width = 900; |
| 29 | const height = 400; |
| 30 | |
| 31 | const helpDiv = document.createElement('div'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 32 | helpDiv.style.top = '0'; |
| 33 | helpDiv.style.left = '0'; |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 34 | helpDiv.style.position = 'absolute'; |
| 35 | document.body.appendChild(helpDiv); |
| 36 | helpDiv.innerHTML = |
| 37 | 'Help: click + drag to pan, double click to reset, scroll to zoom. ' + |
| 38 | 'Hold the x/y keys to only pan/zoom along the x/y axes.<br>' + |
| 39 | 'X-axes of the top two plots are linked together.'; |
| 40 | |
| 41 | const div = document.createElement('div'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 42 | div.style.top = '40'; |
| 43 | div.style.left = '0'; |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 44 | div.style.position = 'absolute'; |
| 45 | document.body.appendChild(div); |
| 46 | |
| 47 | const div2 = document.createElement('div'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 48 | div2.style.top = (parseFloat(div.style.top) + height).toString(); |
| 49 | div2.style.left = '0'; |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 50 | div2.style.position = 'absolute'; |
| 51 | document.body.appendChild(div2); |
| 52 | |
| 53 | const benchmarkDiv = document.createElement('div'); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 54 | benchmarkDiv.style.top = (parseFloat(div2.style.top) + height).toString(); |
| 55 | benchmarkDiv.style.left = '0'; |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 56 | benchmarkDiv.style.position = 'absolute'; |
| 57 | document.body.appendChild(benchmarkDiv); |
| 58 | |
| 59 | const handlerTimePlot = new Plot(div, width, height); |
| 60 | const latencyPlot = new Plot(div2, width, height); |
| 61 | const benchmarkPlot = new Plot(benchmarkDiv, width, height); |
| 62 | |
| 63 | // Class to store the lines for plotting a Statistics message. |
| 64 | class StatLines { |
| 65 | private max: Line; |
| 66 | private min: Line; |
| 67 | private average: Line; |
| 68 | private maxPoints: number[] = []; |
| 69 | private minPoints: number[] = []; |
| 70 | private averagePoints: number[] = []; |
| 71 | constructor(plotter: Plot) { |
| 72 | this.max = plotter.getDrawer().addLine(); |
| 73 | this.min = plotter.getDrawer().addLine(); |
| 74 | this.average = plotter.getDrawer().addLine(); |
| 75 | |
| 76 | this.max.setLabel("Max"); |
| 77 | this.min.setLabel("Min"); |
| 78 | this.average.setLabel("Average"); |
| 79 | |
| 80 | this.max.setColor([1, 0, 0]); |
| 81 | this.min.setColor([0, 1, 0]); |
| 82 | this.average.setColor([0, 0, 1]); |
| 83 | } |
| 84 | addPoints(parser: Parser, statsTable: Table, time: number) { |
| 85 | this.maxPoints.push(time); |
| 86 | this.minPoints.push(time); |
| 87 | this.averagePoints.push(time); |
| 88 | this.maxPoints.push(parser.readScalar(statsTable, "max") * 1000); |
| 89 | this.minPoints.push(parser.readScalar(statsTable, "min") * 1000); |
| 90 | this.averagePoints.push(parser.readScalar(statsTable, "average") * 1000); |
| 91 | |
| 92 | |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 93 | // TODO: These memory allocations absolutely kill performance. |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 94 | this.max.setPoints(new Float32Array(this.maxPoints)); |
| 95 | this.min.setPoints(new Float32Array(this.minPoints)); |
| 96 | this.average.setPoints(new Float32Array(this.averagePoints)); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | function setupPlot(plotter: Plot, title: string): StatLines { |
| 101 | plotter.getAxisLabels().setXLabel("Monotonic send time since start (sec)"); |
| 102 | plotter.getAxisLabels().setYLabel("Time (msec)"); |
| 103 | plotter.getAxisLabels().setTitle(title); |
| 104 | return new StatLines(plotter); |
| 105 | } |
| 106 | |
| 107 | // Sets of the two x-axes to be shared; remove this to be able to zoom/pan the |
| 108 | // x-axes independently. |
| 109 | handlerTimePlot.linkXAxis(latencyPlot); |
| 110 | |
| 111 | const handlerLines = setupPlot(handlerTimePlot, "Handler Run Times"); |
| 112 | const latencyLines = setupPlot(latencyPlot, "Handler Latencies"); |
| 113 | |
| 114 | const conn = new Connection(); |
| 115 | |
| 116 | conn.connect(); |
| 117 | |
| 118 | const timingReport = { |
| 119 | name: '/aos', |
| 120 | type: 'aos.timing.Report', |
| 121 | }; |
| 122 | |
| 123 | let reportParser = null; |
| 124 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 125 | conn.addConfigHandler((config: Configuration) => { |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 126 | // Locate the timing report schema so that we can read the received messages. |
| 127 | let reportSchema = null; |
| 128 | for (let ii = 0; ii < config.channelsLength(); ++ii) { |
| 129 | if (config.channels(ii).type() === timingReport.type) { |
| 130 | reportSchema = config.channels(ii).schema(); |
| 131 | } |
| 132 | } |
| 133 | if (reportSchema === null) { |
| 134 | throw new Error('Couldn\'t find timing report schema in config.'); |
| 135 | } |
| 136 | reportParser = new Parser(reportSchema); |
| 137 | |
| 138 | // Subscribe to the timing report message. |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 139 | const builder = |
| 140 | new flatbuffers_builder.Builder(512) as unknown as flatbuffers.Builder; |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 141 | const channels: flatbuffers.Offset[] = []; |
| 142 | for (const channel of [timingReport]) { |
| 143 | const nameFb = builder.createString(channel.name); |
| 144 | const typeFb = builder.createString(channel.type); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 145 | Channel.startChannel(builder); |
| 146 | Channel.addName(builder, nameFb); |
| 147 | Channel.addType(builder, typeFb); |
| 148 | const channelFb = Channel.endChannel(builder); |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 149 | ChannelRequest.startChannelRequest(builder); |
| 150 | ChannelRequest.addChannel(builder, channelFb); |
| 151 | ChannelRequest.addMethod(builder, TransferMethod.EVERYTHING_WITH_HISTORY); |
| 152 | channels.push(ChannelRequest.endChannelRequest(builder)); |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 153 | } |
| 154 | |
James Kuszmaul | 71a8193 | 2020-12-15 21:08:01 -0800 | [diff] [blame^] | 155 | const channelsFb = SubscriberRequest.createChannelsToTransferVector(builder, channels); |
| 156 | SubscriberRequest.startSubscriberRequest(builder); |
| 157 | SubscriberRequest.addChannelsToTransfer(builder, channelsFb); |
| 158 | const connect = SubscriberRequest.endSubscriberRequest(builder); |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 159 | builder.finish(connect); |
| 160 | conn.sendConnectMessage(builder); |
| 161 | }); |
| 162 | |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 163 | let startTime = null; |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 164 | conn.addHandler(timingReport.type, (data: Uint8Array, time: number) => { |
| 165 | if (startTime === null) { |
| 166 | startTime = time; |
| 167 | } |
| 168 | time = time - startTime; |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 169 | const table = Table.getRootTable(new ByteBuffer(data)); |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 170 | |
| 171 | const timer = reportParser.readVectorOfTables(table, "timers")[0]; |
| 172 | handlerLines.addPoints( |
| 173 | reportParser, reportParser.readTable(timer, 'handler_time'), time); |
| 174 | latencyLines.addPoints( |
| 175 | reportParser, reportParser.readTable(timer, 'wakeup_latency'), time); |
| 176 | }); |
| 177 | |
| 178 | // Set up and draw the benchmarking plot |
| 179 | benchmarkPlot.getAxisLabels().setTitle( |
| 180 | 'Benchmarkping plot (1M points per line)'); |
| 181 | const line1 = benchmarkPlot.getDrawer().addLine(); |
| 182 | // For demonstration purposes, make line1 only have points and line2 only have |
| 183 | // lines. |
| 184 | line1.setDrawLine(false); |
| 185 | line1.setLabel('LINE ONE'); |
| 186 | const line2 = benchmarkPlot.getDrawer().addLine(); |
| 187 | line2.setPointSize(0); |
| 188 | line2.setLabel('LINE TWO'); |
| 189 | const NUM_POINTS = 1000000; |
| 190 | const points1 = []; |
| 191 | const points2 = []; |
| 192 | for (let ii = 0; ii < NUM_POINTS; ++ii) { |
| 193 | points1.push(ii); |
| 194 | points2.push(ii); |
Philipp Schrader | e625ba2 | 2020-11-16 20:11:37 -0800 | [diff] [blame] | 195 | points1.push(Math.sin(ii * 10 / NUM_POINTS)); |
| 196 | points2.push(Math.cos(ii * 10 / NUM_POINTS)); |
James Kuszmaul | a8f2c45 | 2020-07-05 21:17:56 -0700 | [diff] [blame] | 197 | } |
| 198 | line1.setPoints(new Float32Array(points1)); |
| 199 | line2.setPoints(new Float32Array(points2)); |