4 function perfService(message) {
5 if (perfStats.numResults++ === 0) {
6 perfStats.firstMsg = message; // stored since it has "real" start time
8 perfStats.bytesReceived += message.length;
11 function clearPerfStats(inOrOut) {
18 document.getElementById("in_or_out").innerHTML = inOrOut;
20 // hide results fields
21 document.getElementById("results").style.display = "none";
24 function completePerfStats() {
25 perfStats.timeEnded = new Date().getTime();
27 // get time started from the first sent message
28 perfStats.timeStarted = perfStats.firstMsg.substr(0, perfStats.firstMsg.indexOf(':'));
30 var timeUsedMs = perfStats.timeEnded - perfStats.timeStarted;
33 document.getElementById("results_num_received").innerHTML = perfStats.numResults;
34 document.getElementById("results_bytes_received").innerHTML = perfStats.bytesReceived;
35 document.getElementById("results_time_used").innerHTML = timeUsedMs + "ms";
36 document.getElementById("results_msgs_per_sec").innerHTML = (perfStats.numResults / (timeUsedMs / 1000));
37 document.getElementById("results_bytes_per_sec").innerHTML = (perfStats.bytesReceived / (timeUsedMs / 1000));
38 document.getElementById("test_running").style.display = "none";
39 document.getElementById("results").style.display = "";
42 function initPerfTest() {
44 gadgets.rpc.register("perf_service", perfService);
45 gadgets.rpc.register("clear_perf_stats", clearPerfStats);
46 gadgets.rpc.register("complete_perf_stats", completePerfStats);
49 var alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-*&(){}'";
51 function sendPerfMessage() {
52 var msgToSend = currentRun.msg;
53 if (currentRun.curMsgId++ <= 1) {
54 var nowString = new Date().getTime() + ':';
55 msgToSend = nowString + currentRun.msg.substring(nowString.length);
58 gadgets.rpc.call(currentRun.targetId, "perf_service", null, msgToSend);
59 if (currentRun.curMsgId < currentRun.endMsgId) {
60 // loop, giving up execution in case rpc technique demands it
61 window.setTimeout(sendPerfMessage, 0);
64 window.setTimeout(function() { gadgets.rpc.call(currentRun.targetId, "complete_perf_stats", null); }, 0);
68 function runPerfTest(targetId) {
69 document.getElementById("test_running").style.display = "";
71 // initialize the current run
72 var num_msgs = document.getElementById("num_msgs").value;
73 var msg_size = document.getElementById("msg_size").value;
75 currentRun.targetId = targetId;
76 currentRun.curMsgId = 0;
77 currentRun.endMsgId = num_msgs;
80 for (var i = 0; i < msg_size; ++i) {
81 msg[i] = alphabet.charAt(Math.round(Math.random(alphabet.length)));
83 currentRun.msg = msg.join('');
85 // clear local perf stats
86 clearPerfStats("(outbound)");
88 // clear target perf stats
89 gadgets.rpc.call(targetId, "clear_perf_stats", null, "(inbound)");
91 // kick off the send loop