summaryrefslogtreecommitdiff
path: root/js/demo/Ice/throughput/browser/Client.js
blob: ce3dc361f3340ade6b1ad34155204f0f401b4f57 (plain)
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
// **********************************************************************
//
// Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

(function(){

var ThroughputPrx = Demo.ThroughputPrx;

//
// Initialize sequences.
//
var i;
var byteSeq = new Uint8Array(Demo.ByteSeqSize);
for(i = 0; i < Demo.ByteSeqSize; ++i)
{
    byteSeq[i] = 0;
}

var stringSeq = [];
for(i = 0; i < Demo.StringSeqSize; ++i)
{
    stringSeq[i] = "hello";
}

var structSeq = [];
for(i = 0; i < Demo.StringDoubleSeqSize; ++i)
{
    structSeq[i] = new Demo.StringDouble();
    structSeq[i].s = "hello";
    structSeq[i].d = 3.14;
}

var fixedSeq = [];
for(i = 0; i < Demo.FixedSeqSize; ++i)
{
    fixedSeq[i] = new Demo.Fixed();
    fixedSeq[i].i = 0;
    fixedSeq[i].j = 0;
    fixedSeq[i].d = 0;
}

var communicator = Ice.initialize();

//
// Run the throughput test.
//
function run()
{
    //
    // Create a proxy to the throughput object.
    //
    var hostname = document.location.hostname || "127.0.0.1";
    var secure = document.location.protocol.indexOf("https") != -1;
    var ref = secure ?
        "throughput:wss -h " + hostname + " -p 9090 -r /demowss" :
        "throughput:ws -h " + hostname + " -p 8080 -r /demows";
    var proxy = communicator.stringToProxy(ref);

    //
    // Down-cast the proxy to the Demo.Throughput interface.
    //
    return ThroughputPrx.checkedCast(proxy).then(
        function(twoway)
        {
            var oneway = twoway.ice_oneway();

            var seq;
            var seqSize;
            var wireSize;
            var proxy;
            var operation;
            var repetitions = 100;

            var data = $("#data").val();
            //
            // Get the sequence data
            //
            if(data == "byte-seq")
            {
                seq = byteSeq;
                seqSize = Demo.ByteSeqSize;
                seq = byteSeq;
                wireSize = 1;
            }
            else if(data == "string-seq")
            {
                seq = stringSeq;
                seqSize = Demo.StringSeqSize;
                seq = stringSeq;
                wireSize = seq[0].length;
            }
            else if(data == "struct-seq")
            {
                seq = structSeq;
                seqSize = Demo.StringDoubleSeqSize;
                seq = structSeq;
                wireSize = seq[0].s.length;
                //
                // Size of double on the wire.
                //
                wireSize += 8;
            }
            else if(data == "fixed-seq")
            {
                seq = fixedSeq;
                seqSize = Demo.FixedSeqSize;
                seq = fixedSeq;
                //
                // Size of two ints and a double on the wire.
                //
                wireSize = 16;
            }

            //
            // Get the proxy and operation
            //
            var test = $("#test").val();
            if(test == "twoway" || test == "oneway")
            {
                proxy = test == "twoway" ? twoway : oneway;
                if(data == "byte-seq")
                {
                    operation = proxy.sendByteSeq;
                }
                else if(data == "string-seq")
                {
                    operation = proxy.sendStringSeq;
                }
                else if(data == "struct-seq")
                {
                    operation = proxy.sendStructSeq;
                }
                else if(data == "fixed-seq")
                {
                    operation = proxy.sendFixedSeq;
                }
                write("sending");
            }
            else if(test == "receive")
            {
                proxy = twoway;
                if(data == "byte-seq")
                {
                    operation = proxy.recvByteSeq;
                }
                else if(data == "string-seq")
                {
                    operation = proxy.recvStringSeq;
                }
                else if(data == "struct-seq")
                {
                    operation = proxy.recvStructSeq;
                }
                else if(data == "fixed-seq")
                {
                    operation = proxy.recvFixedSeq;
                }
                write("receiving");
            }
            else if(test == "echo")
            {
                proxy = twoway;
                if(data == "byte-seq")
                {
                    operation = proxy.echoByteSeq;
                }
                else if(data == "string-seq")
                {
                    operation = proxy.echoStringSeq;
                }
                else if(data == "struct-seq")
                {
                    operation = proxy.echoStructSeq;
                }
                else if(data == "fixed-seq")
                {
                    operation = proxy.echoFixedSeq;
                }
                write("sending and receiving");
            }

            write(" " + repetitions);
            if(data == "byte-seq")
            {
                write(" byte");
            }
            else if(data == "string-seq")
            {
                write(" string");
            }
            else if(data == "struct-seq")
            {
                write(" variable-length struct");
            }
            else if(data == "fixed-seq")
            {
                write(" fixed-length struct");
            }
            write(" sequences of size " + seqSize);
            if(test == "oneway")
            {
                write(" as oneway");
            }
            writeLine("...");

            //
            // Invoke the test operation in a loop with the required
            // arguments.
            //
            // We chain the promises. A test operation is called only
            // once the promise for the previous operation is
            // fulfilled.
            //
            var start = new Date().getTime();
            var args = test != "receive" ? [seq] : [];
            return loop(
                function()
                {
                    return operation.apply(proxy, args);
                },
                repetitions
            ).then(
                function()
                {
                    //
                    // Write the results.
                    //
                    var total = new Date().getTime() - start;
                    writeLine("time for " + repetitions + " sequences: " + total  + " ms");
                    writeLine("time per sequence: " + total / repetitions + " ms");

                    var mbit = repetitions * seqSize * wireSize * 8.0 / total / 1000.0;
                    if(test == "echo")
                    {
                        mbit *= 2;
                    }
                    mbit = Math.round(mbit * 100) / 100;
                    writeLine("throughput: " + mbit + " Mbps");
                });
        });
}

$("#run").click(
    function()
    {
        //
        // Run the throughput loop if not already running.
        //
        if(state !== State.Running)
        {
            setState(State.Running);

            Ice.Promise.try(
                function()
                {
                    return run();
                }
            ).exception(
                function(ex)
                {
                    $("#output").val(ex.toString());
                }
            ).finally(
                function()
                {
                    setState(State.Idle);
                }
            );
        }
        return false;
    });

//
// Asynchronous loop: each call to the given function returns a
// promise that when fulfilled runs the next iteration.
//
function loop(fn, repetitions)
{
    var i = 0;
    var next = function()
    {
        if(i++ < repetitions)
        {
            return fn.call().then(next);
        }
    };
    return next();
}

//
// Helper functions to write the output.
//
function write(msg)
{
    $("#output").val($("#output").val() + msg);
}

function writeLine(msg)
{
    write(msg + "\n");
    $("#output").scrollTop($("#output").get(0).scrollHeight);
}

//
// Handle the client state.
//
var State = {
    Idle:0,
    Running: 1
};
var state;

function setState(s, ex)
{
    if(s != state)
    {
        switch(s)
        {
            case State.Running:
            {
                $("#output").val("");
                $("#run").addClass("disabled");
                $("#progress").show();
                $("body").addClass("waiting");
                break;
            }
            case State.Idle:
            {
                $("#run").removeClass("disabled");
                $("#progress").hide();
                $("body").removeClass("waiting");
                break;
            }
        }
        state = s;
    }
}

setState(State.Idle);

}());