File: ScaleTest.java

package info (click to toggle)
libjgroups-java 2.12.2.Final-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 8,724 kB
  • sloc: java: 109,098; xml: 9,423; sh: 174; makefile: 4
file content (129 lines) | stat: -rw-r--r-- 3,795 bytes parent folder | download | duplicates (4)
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
package org.jgroups.tests;

import org.jgroups.*;
import org.jgroups.blocks.*;
import org.jgroups.util.Util;

import java.lang.reflect.Method;
import java.text.NumberFormat;
import java.util.Date;

/**
 * @author Bela Ban
 */
public class ScaleTest {
    JChannel ch;
    String props="udp.xml";
    boolean server=true;
    RpcDispatcher disp;

    static NumberFormat f;

    static {
        f=NumberFormat.getNumberInstance();
        f.setGroupingUsed(false);
        f.setMaximumFractionDigits(2);
    }



    public ScaleTest(String props, boolean server) {
        this.props=props;
        this.server=server;
    }

    public Address getAddress() {
        return ch.getAddress();
    }

    public void start() throws ChannelException {
        ch=new JChannel(props);
        disp=new RpcDispatcher(ch, null, new MembershipListenerAdapter() {
            public void viewAccepted(View new_view) {
                System.out.println("view=" + new_view);
            }
        }, this);
        ch.connect("ScaleTest-Cluster");

        if(!server) {
            loop();
            Util.close(ch);
        }
        else
            System.out.println("ScaleTest started at " + new Date() + ", ready to server requests");
    }

    private void loop() {
        while(true) {
            int input=Util.keyPress(prompt());
            switch(input) {
                case '1':
                    View view=ch.getView();
                    if(view.size() > 10)
                        System.out.println(view.getViewId() + ": " + view.size() + " members");
                    else
                        System.out.println(view + " (" + view.size() + " members)");
                    break;
                case '2':
                    try {
                        invokeRpcs();
                    }
                    catch(Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case '3':
                    return;
            }
        }
    }

    private void invokeRpcs() throws Exception {
        Method method=MethodCall.findMethod(ScaleTest.class, "getAddress", null);
        MethodCall call=new MethodCall(method);
        RequestOptions opts=new RequestOptions().setMode(GroupRequest.GET_ALL)
                .setTimeout(5000).setFlags(Message.DONT_BUNDLE);
        int num_msgs=Util.readIntFromStdin("Number of RPCs: ");
        int print=num_msgs / 10;
        System.out.println("Invoking " + num_msgs + " RPCs:");
        long start=System.currentTimeMillis();
        for(int i=0; i < num_msgs; i++) {
            disp.callRemoteMethods(null, call, opts);
            if(print > 0 && i % print == 0)
                System.out.println("invoking RPC #" + i);
        }
        long diff=System.currentTimeMillis() - start;
        double rpcs_per_sec=num_msgs / (diff / 1000.0);
        System.out.println("Invoked " + num_msgs + " in " + diff + " ms: " + f.format(rpcs_per_sec) + " RPCs / sec");
    }

    private static String prompt() {
        return "\n[1] View [2] Send RPCs [3] Exit";
    }


    public static void main(String[] args) throws ChannelException {
        String props="udp.xml";
        boolean server=true;

        for(int i=0; i < args.length; i++) {
            if(args[i].equals("-props")) {
                props=args[++i];
                continue;
            }
            if(args[i].equals("-server")) {
                server=Boolean.parseBoolean(args[++i]);
                continue;
            }
            help();
            return;
        }

        ScaleTest test=new ScaleTest(props, server);
        test.start();
    }

    static void help() {
        System.out.println("ScaleTest [-props properties] [-server (true | false)]");
    }
}