summaryrefslogtreecommitdiff
path: root/js/demo/Ice/latency/Client.js
blob: b0a2034e40e1bbf78e36aecb43de4c0a9bac9be5 (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
// **********************************************************************
//
// Copyright (c) 2003-2014 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(){

require("Ice");
require("./Latency");

var communicator;

//
// 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();
}

Ice.Promise.try(
    function()
    {
        //
        // Initialize the communicator and create a proxy to the 
        // ping object.
        //
        communicator = Ice.initialize();
        var repetitions = 10000;
        var proxy = communicator.stringToProxy("ping:default -p 10000");
        
        //
        // Down-cast the proxy to the Demo.Ping interface.
        //
        return Demo.PingPrx.checkedCast(proxy).then(
            function(obj)
            {
                console.log("pinging server " + repetitions + " times (this may take a while)");
                start = new Date().getTime();
                return loop(
                    function() 
                    {
                        return obj.ice_ping();
                    },
                    repetitions
                ).then(
                    function()
                    {
                        //
                        // Write the results.
                        //
                        total = new Date().getTime() - start;
                        console.log("time for " + repetitions + " pings: " + total + "ms");
                        console.log("time per ping: " + (total / repetitions) + "ms");
                    });
            });
    }
).finally(
    function()
    {
        //
        // Destroy the communicator if required.
        //
        if(communicator)
        {
            return communicator.destroy();
        }
    }
).exception(
    function(ex)
    {
        //
        // Handle any exceptions above.
        //
        console.log(ex.toString());
        process.exit(1);
    });
}());