-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadmui.go
368 lines (342 loc) · 12.2 KB
/
admui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package main
import (
"encoding/json"
"fmt"
"log"
"io"
"net/http"
)
// SrvConfig defines the far-end server, and its command and payload ports
type SrvConfig struct {
Host string
RPCPort string
Count uint64
Repeat bool
}
// Command controls the type of function that TCPClient should perform
type Command struct {
Name string
Cfg SrvConfig
}
type BitRate uint64
// Returns bitrate in Mega-bits per second
func (b BitRate) Mbps() float32 {
return float32(b) / float32(1000000)
}
// Returns bitrate in Mega-bytes per second
func (b BitRate) MBps() float32 {
return float32(b) / float32(8*1000000)
}
// Returns bitrate in Kilo-bits per second
func (b BitRate) Kbps() float32 {
return float32(b) / float32(1000)
}
// Returns bitrate in Kilo-bytes per second
func (b BitRate) KBps() float32 {
return float32(b) / float32(8*1000)
}
func (b BitRate) String() string {
return fmt.Sprintf("%d", b)
}
// Stats is type of measurement that TCPClient reports on its stats channel.
type Stats struct {
Stat string
Type string
Rate BitRate
}
type JSONStats struct {
Stat string
Type string
Rate float32
}
// CCmdHandler is the receiver type for handling TCPClient control request
type CCmdHandler struct {
CmdCh chan Command
}
// CStatHandler is the reciever type for handling TCPClient stats requests
type CStatHandler struct {
StatCh chan Stats
}
// This handler parses the form from the user and initiates a TCPClient
// measurement.
func (c *CCmdHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var (
raddr string
rport int
pktt string
tstt string
txsize int
txmult string
txcont string
)
params := map[string]interface{}{
"raddr": &raddr,
"rport": &rport,
"pktt": &pktt,
"tstt": &tstt,
"txsize": &txsize,
"txmult": &txmult,
"txcont": &txcont,
}
Mult := map[string]uint64{
"KB": 1024,
"MB": 1024 * 1024,
"GB": 1024 * 1024 * 1024,
}
getformparams(r, params)
trace.Printf("|CMD|%s|%s|\n", tstt, raddr)
log.Println("CMD: ", raddr, rport, pktt, tstt, txsize, txmult, txcont != "")
cmd := Command{
Name: tstt,
Cfg: SrvConfig{
Host: raddr,
RPCPort: fmt.Sprint(rport),
Count: uint64(txsize) * Mult[txmult],
Repeat: txcont != "",
},
}
c.CmdCh <- cmd
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(""))
}
// parse and store form parameters in the map that's passed in
func getformparams(r *http.Request, params map[string]interface{}) {
for i, x := range params {
if v := r.FormValue(i); v != "" {
fmt.Sscan(v, x)
}
}
}
// This handler deals with GET requests for TCPClient measurement results.
// It returns the measurements since last snapshot in json format.
func (s *CStatHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var jst JSONStats
w.Header().Set("Content-Type", "text/plain")
st, ok := <-s.StatCh
if !ok {
jst = JSONStats{Stat: "Error"}
} else {
jst = JSONStats{st.Stat, st.Type, st.Rate.Mbps()}
}
je := json.NewEncoder(w)
je.Encode(jst)
}
// WebUI is an http server that provides an html UI to the user, annoucing itself at address
// that is passed in. It handles requests for starting and stopping of the load testing
// client and reporting of data.
func WebUI(addr string, cch chan Command, sch chan Stats) {
cl := &CCmdHandler{cch}
st := &CStatHandler{sch}
http.Handle("/cmd", cl)
http.Handle("/stats", st)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
io.WriteString(w, htmlfile)
})
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal("ListenAndServe: " + err.Error())
}
}
// unfortunately there isn't an easy way to include
// a file; so make the changes to index.html and then
// replace the contents of the htmlfile const with it.
const htmlfile = `
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script src="http://yui.yahooapis.com/3.8.0/build/yui/yui-min.js"></script>
<script type='text/javascript'>
google.load('visualization', '1', {packages:['corechart', 'gauge']});
</script>
<script type='text/javascript'>
YUI().use("node", "io", "io-form", "event", "dump", "json-parse", "button", function(Y) {
var that = this;
var running = false;
var gauge_options = {
width: 200, height: 200,
redFrom: 0, redTo: 10,
yellowFrom: 10, yellowTo: 50,
greenFrom: 50, greenTo: 100,
minorTicks: 6,
};
var chart_options = {
width: 600, height: 200,
title : 'Megabits / Second',
animation : { duration : 500 },
};
var upgauge = new google.visualization.Gauge(Y.one('#upgauge').getDOMNode());
var dngauge = new google.visualization.Gauge(Y.one('#dngauge').getDOMNode());
var upchart = new google.visualization.LineChart(Y.one('#upchart').getDOMNode());
var dnchart = new google.visualization.LineChart(Y.one('#dnchart').getDOMNode());
var dtable = new google.visualization.DataTable();
var lUp = 0, lDown = 0;
dtable.addColumn('timeofday', 'Time');
dtable.addColumn('number', 'Bitrate');
upchart.draw(dtable, chart_options);
dnchart.draw(dtable, chart_options);
upgauge.draw(google.visualization.arrayToDataTable([['Label', 'Value'], ['Upload', 0]]), gauge_options);
dngauge.draw(google.visualization.arrayToDataTable([['Label', 'Value'], ['Download', 0]]), gauge_options);
var onSuccess = function (id, o, args) {
dtable = new google.visualization.DataTable();
dtable.addColumn('timeofday', 'Time');
dtable.addColumn('number', 'Bitrate');
Y.one('#status_div').setHTML("<i>Starting...</i>");
Y.all('#tstreqform input').setAttribute('disabled', 'disabled');
Y.later(250, that, updateVisuals, false); // give it time so that GET "/stats" doesn't fail right away
};
var onFailure = function (id, o, args) {
Y.one('#start').removeAttribute('disabled');
Y.all('#tstreqform input').removeAttribute('disabled');
running = false;
Y.one('#params_div').setStyle('opacity', '');
}
var enableForm = function () {
Y.one('#start').removeAttribute('disabled');
Y.all('#tstreqform input').removeAttribute('disabled');
running = false;
Y.one('#params_div').setStyle('opacity', '');
};
Y.one('#start').on('click', function(e) {
if (running) return;
Y.one('#start').setAttribute('disabled', 'disabled');
running = true;
Y.one('#params_div').setStyle('opacity', '0.4');
e.halt(true);
var cfg = {
method : "POST",
form : { id : Y.one('#tstreqform'), useDisabled : false },
on : { success : onSuccess , failure : onFailure },
context : Y,
arguments : { success : '/cmd' },
};
Y.io("/cmd", cfg);
});
enableForm();
/*
Y.one('#stop').on('click', function(e) {
var cfg = { method : "POST", data : "tstt=STOP" }
Y.io("/cmd", cfg);
});
*/
var updateVisuals = function () {
Y.io("/stats", {
on : {
success : function (tx, r) {
var pr;
try {
pr = Y.JSON.parse(r.responseText);
} catch (e) {
alert("JSON Parse failed: ", r.responseText);
Y.one('#status_div').setHTML("<i>Error: "+e+"</i>");
enableForm();
return;
}
var msg = pr.Stat + " " + ((pr.Type=="UP")?"Upload":((pr.Type=="DOWN")?"Download":''));
Y.one('#status_div').setHTML("<i>"+msg+"</i>");
if (pr.Stat != "Running") {
enableForm();
return;
}
var foo = new Date();
var xtm = [foo.getHours(), foo.getMinutes(), foo.getSeconds(), foo.getMilliseconds()];
dtable.addRows([[xtm, pr.Rate]]);
if (pr.Type == "UP") {
upchart.draw(dtable, chart_options);
lUp = pr.Rate;
var dt = google.visualization.arrayToDataTable([
['Label', 'Value'],
['Upload', lUp ]
]);
upgauge.draw(dt, gauge_options);
} else {
dnchart.draw(dtable, chart_options);
lDown = pr.Rate;
var dt = google.visualization.arrayToDataTable([
['Label', 'Value'],
['Download', lDown ]
]);
dngauge.draw(dt, gauge_options);
}
Y.later(500, that, updateVisuals, false);
},
failure : function (tx, r) {
alert("No data");
enableForm();
},
},
});
};
});
</script>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.8.0/build/cssfonts/cssfonts-min.css">
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.8.0/build/cssgrids/grids-min.css">
</head>
<body>
<div> <!-- class="yui3-cssfonts"> -->
<h1>tcpmeter - TCP Speedometer</h1>
<div class="yui3-skin-sam">
<div class="yui3-g">
<div id="params_div" class="yui3-u-1-4">
<form id="tstreqform" method="post" action="/cmd">
<fieldset>
<legend>Server Information</legend>
<p>
<label>Host:<input type=text name=raddr required></label><br />
<label>RPC:<input type=number name=rport default="8001" placeholder="8001" required></label>
</p>
</fieldset>
<p>
<fieldset>
<legend>Packet Type</legend>
<p>
<input type=radio name=pktt value="udp" disabled="disabled">UDP</input>
<input type=radio name=pktt value="tcp" checked="checked">TCP</input>
</p>
</fieldset>
</p>
<p>
<fieldset>
<legend>Type of Measurement</legend>
<p>
<input type=radio name=tstt value="UP" checked="checked">Upload</input>
<input type=radio name=tstt value="DOWN">Download</input>
<input type=radio name=tstt value="RTT">Round Trip</input><br />
<input type=checkbox name=txcont checked="">Continuous</input>
</p>
</fieldset>
</p>
<p>
<fieldset>
<legend>Size of Dataset</legend>
<p>
<label>Amount of data:<input type=number name=txsize></label><br />
<input type=radio name=txmult value="KB">KB</input>
<input type=radio name=txmult value="MB" checked="checked">MB</input>
<input type=radio name=txmult value="GB">GB</input>
</p>
</fieldset>
</p>
</form>
<p>
<input id="start" type="button" value="Start" />
<!-- <input id="stop" type="button" value="Abort" /> -->
</p>
</div>
<div class="yui3-u-3-4">
<div class="yui3-g">
<div class="yui3-u-1-4" id='upgauge'></div>
<div class="yui3-u-3-4" id='upchart'></div>
</div>
<div class="yui3-g">
<div class="yui3-u-1-4" id='dngauge'></div>
<div class="yui3-u-3-4" id='dnchart'></div>
</div>
<div id='status_div' style="text-align:center"><p><i>Stopped</i></p></div>
</div>
</div>
</div>
</div>
</body>
</html>`