Debian Codegraph @

pkg/playground/speed.js

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 import cockpit from "cockpit";
3
4 import '../lib/patternfly/patternfly-6-cockpit.scss';
5 import "../../node_modules/@patternfly/patternfly/components/Button/button.css";
6 import "../../node_modules/@patternfly/patternfly/components/Page/page.css";
7 import "./speed.css";
8
9 let channel = null;
10 let websocket = null;
11 let timer = null;
12 let start = null;
13 let total = 0;
14 let proc = null;
15 let close_problem;
16
17 function update() {
18 const element = document.getElementById("speed");
19 if (channel || websocket) {
20 element.textContent = cockpit.format_bytes_per_sec((total * 1000) / (Date.now() - start));
21 console.log(total);
22 } else {
23 element.textContent = "";
24 }
25
26 const memory = document.getElementById("memory");
27 const pid = document.getElementById("pid");
28 if (!proc) {
29 proc = cockpit.script("echo $PPID && cat /proc/$PPID/statm");
30 proc.then(function(data) {
31 const parts = data.split("\n");
32 pid.textContent = parts[0];
33 memory.textContent = parts[1];
34 proc = null;
35 }, function(ex) {
36 memory.textContent = String(ex);
37 proc = null;
38 });
39 }
40 }
41
42 function echo(ev) {
43 stop();
44
45 const sideband = ev.target.id == "echo-sideband";
46
47 function generate(length, binary) {
48 if (binary)
49 return new window.ArrayBuffer(length);
50 else
51 return (new Array(length)).join("x");
52 }
53
54 const length = parseInt(document.getElementById("message").value, 10);
55 const batch = parseInt(document.getElementById("batch").value, 10);
56 const interval = parseInt(document.getElementById("interval").value, 10);
57
58 if (isNaN(length) || isNaN(interval) || isNaN(batch)) {
59 window.alert("Bad value");
60 return;
61 }
62
63 const binary = document.getElementById.checked;
64 const options = { payload: "echo" };
65 const input = generate(length, binary);
66 start = new Date();
67 total = 0;
68
69 if (sideband) {
70 if (binary)
71 options.binary = "raw";
72
73 websocket = new window.WebSocket(cockpit.transport.uri("channel/" + cockpit.transport.csrf_token) +
74 "?" + window.btoa(JSON.stringify(options)));
75 websocket.binaryType = 'arraybuffer';
76
77 websocket.onopen = function() {
78 for (let i = 0; i < batch; i++)
79 websocket.send(input);
80 timer = window.setInterval(function() {
81 for (let i = 0; i < batch; i++)
82 websocket.send(input);
83 }, interval);
84 };
85
86 websocket.onmessage = function(event) {
87 if (binary)
88 total += event.data.byteLength;
89 else
90 total += event.data.length;
91 };
92
93 websocket.onclose = function(event) {
94 if (websocket)
95 window.alert("channel closed");
96 stop();
97 };
98 } else {
99 if (binary)
100 options.binary = true;
101
102 channel = cockpit.channel(options);
103
104 channel.addEventListener("message", function(event, data) {
105 total += data.length;
106 });
107 channel.addEventListener("close", function(event, options) {
108 if (options.problem)
109 window.alert(options.problem);
110 stop();
111 });
112
113 for (let i = 0; i < batch; i++)
114 channel.send(input);
115
116 timer = window.setInterval(function() {
117 for (let i = 0; i < batch; i++)
118 channel.send(input);
119 }, interval);
120 }
121 }
122
123 function read(ev) {
124 stop();
125
126 const sideband = ev.target.id == "read-sideband";
127 const path = document.getElementById("read-path");
128
129 const options = {
130 payload: "fsread1",
131 path: path.value,
132 max_read_size: 100 * 1024 * 1024 * 1024,
133 binary: sideband ? "raw" : true,
134 };
135
136 start = Date.now();
137 total = 0;
138
139 if (sideband) {
140 websocket = new window.WebSocket(cockpit.transport.uri("channel/" + cockpit.transport.csrf_token) +
141 "?" + window.btoa(JSON.stringify(options)));
142 websocket.binaryType = 'arraybuffer';
143 websocket.onmessage = function(event) {
144 total += event.data.byteLength;
145 };
146 websocket.onclose = function(event) {
147 if (websocket)
148 window.alert("channel closed");
149 stop();
150 };
151 } else {
152 channel = cockpit.channel(options);
153 channel.addEventListener("message", function(event, data) {
154 total += data.length;
155 });
156 channel.addEventListener("close", function(event, options) {
157 if (options.problem)
158 window.alert(options.problem);
159 stop();
160 });
161 }
162 }
163
164 function download(ev) {
165 stop();
166
167 const path = document.getElementById("download-path");
168
169 const options = {
170 binary: "raw",
171 max_read_size: 100 * 1024 * 1024 * 1024,
172 external: {
173 "content-disposition": 'attachment; filename="download"',
174 "content-type": "application/octet-stream"
175 }
176 };
177
178 /* Allow use of HTTP URLs */
179 if (path.value.indexOf("http") === 0) {
180 const url = new URL(path.value);
181 options.payload = "http-stream2";
182 options.address = url.hostname;
183 options.port = parseInt(url.port, 10);
184 options.path = url.pathname;
185 options.method = "GET";
186 } else {
187 options.payload = "fsread1";
188 options.path = path.value;
189 }
190
191 console.log("Download", options);
192
193 start = Date.now();
194 total = 0;
195
196 const prefix = (new URL(cockpit.transport.uri("channel/" + cockpit.transport.csrf_token))).pathname;
197 const query = window.btoa(JSON.stringify(options));
198 window.open(prefix + "?" + query);
199 }
200
201 function spawn() {
202 stop();
203
204 document.getElementById("spawn-result").textContent = "Running...";
205 const command = document.getElementById("spawn-command").value;
206
207 start = Date.now();
208 total = 0;
209 close_problem = "terminated";
210
211 console.log("spawning", command);
212 channel = cockpit.script(command, { err: "message" });
213 channel.stream(data => {
214 console.log("spawn: stream block length", data.length);
215 total += data.length;
216 document.getElementById("spawn-output").textContent = data;
217 });
218 channel.then(() => {
219 console.log("spawn: command finished successfully");
220 stop();
221 document.getElementById("spawn-result").textContent = "success";
222 });
223 channel.catch(ex => {
224 console.log("spawn: command failed", JSON.stringify(ex));
225 stop();
226 document.getElementById("spawn-result").textContent = `failed with exit code ${ex.exit_status}: ${ex.message}`;
227 });
228 }
229
230 function spawn_input() {
231 stop();
232
233 const el_result = document.getElementById("spawn-input-result");
234 el_result.textContent = "Running...";
235
236 start = Date.now();
237 total = 0;
238 channel = cockpit.spawn(["dd", "of=/tmp/spawninput"], { err: "message" });
239 const chunk = 'a'.repeat(65536);
240 for (let i = 0; i < 1000; i++) {
241 channel.input(chunk, true);
242 total += chunk.length;
243 }
244 channel.input(); // send EOF
245 channel
246 .then(() => {
247 el_result.textContent = "success";
248 })
249 .catch(ex => {
250 el_result.textContent = `failed with exit code ${ex.exit_status}: ${ex.message}`;
251 })
252 .finally(stop);
253 }
254
255 function stop() {
256 update();
257
258 if (channel)
259 channel.close(close_problem);
260 channel = null;
261 close_problem = undefined;
262 const ws = websocket;
263 websocket = null;
264 if (ws)
265 ws.close();
266
267 window.clearInterval(timer);
268 timer = null;
269 }
270
271 cockpit.transport.wait(function() {
272 document.getElementById("echo-normal").addEventListener("click", echo);
273 document.getElementById("echo-sideband").addEventListener("click", echo);
274 document.getElementById("read-normal").addEventListener("click", read);
275 document.getElementById("read-sideband").addEventListener("click", read);
276 document.getElementById("download-external").addEventListener("click", download);
277 document.getElementById("spawn").addEventListener("click", spawn);
278 document.getElementById("spawn-input").addEventListener("click", spawn_input);
279 document.getElementById("stop").addEventListener("click", stop);
280 window.setInterval(update, 500);
281 document.body.removeAttribute("hidden");
282 });