aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus_test/src/tests/speed/JavaClient.java
blob: c7dfebe506a025495d19fb945eaf78c28dcaf2ee (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import com.yahoo.messagebus.*;
import com.yahoo.messagebus.test.*;
import com.yahoo.config.*;
import com.yahoo.messagebus.routing.*;
import com.yahoo.messagebus.network.*;
import com.yahoo.messagebus.network.rpc.*;
import java.util.Arrays;
import java.util.logging.*;

public class JavaClient implements ReplyHandler {

    private static Logger log = Logger.getLogger(JavaClient.class.getName());

    private static class Counts {
        public int okCnt = 0;
        public int failCnt = 0;
        Counts() {}
        Counts(int okCnt, int failCnt) {
            this.okCnt = okCnt;
            this.failCnt = failCnt;
        }
    }

    private SourceSession session;
    private Counts        counts = new Counts();
    private static long   mySeq  = 100000;

    public JavaClient(RPCMessageBus mb) {
        session = mb.getMessageBus().createSourceSession(this, new SourceSessionParams().setTimeout(30));
    }

    public synchronized Counts sample() {
        return new Counts(counts.okCnt, counts.failCnt);
    }

    public void send() {
        send(++mySeq);
    }

    public void send(long seq) {
        session.send(new MyMessage(seq), "test");
    }

    public void handleReply(Reply reply) {
        if ((reply.getProtocol() == SimpleProtocol.NAME)
            && (reply.getType() == SimpleProtocol.REPLY)
            && (((SimpleReply)reply).getValue().equals("OK")))
        {
            synchronized (this) {
                counts.okCnt++;
            }
        } else {
            synchronized (this) {
                counts.failCnt++;
            }
        }
        try {
            send();
        } catch (IllegalStateException ignore) {} // handle paranoia for shutdown source sessions
    }

    public void shutdown() {
        session.destroy();
    }

    public static void main(String[] args) {
        try {
	    RPCMessageBus mb = new RPCMessageBus(
                new MessageBusParams()
                .setRetryPolicy(new RetryTransientErrorsPolicy().setBaseDelay(0.1))
                .addProtocol(new SimpleProtocol()),
                new RPCNetworkParams()
                .setIdentity(new Identity("server/java"))
                .setSlobrokConfigId("file:slobrok.cfg"),
		"file:routing.cfg");
            JavaClient client = new JavaClient(mb);

            // let the system 'warm up'
            Thread.sleep(5000);

            // inject messages into the feedback loop
            for (int i = 0; i < 1024; ++i) {
                client.send(i);
            }

            // let the system 'warm up'
            Thread.sleep(5000);

            long start;
            long stop;
            Counts before;
            Counts after;

            start = System.currentTimeMillis();
            before = client.sample();
            Thread.sleep(10000); // Benchmark time
            stop = System.currentTimeMillis();
            after = client.sample();
            stop -= start;
            double time = (double)stop;
            double msgCnt = (double)(after.okCnt - before.okCnt);
            double throughput = (msgCnt / time) * 1000.0;
            System.out.printf("JAVA-CLIENT: %g msg/s\n", throughput);
            client.shutdown();
            mb.destroy();
            if (after.failCnt > before.failCnt) {
                System.err.printf("JAVA-CLIENT: FAILED (%d -> %d)\n",
                                  before.failCnt, after.failCnt);
                System.exit(1);
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, "JAVA-CLIENT: Failed", e);
            System.exit(1);
        }
    }

    private static class MyMessage extends SimpleMessage {

        final long seqId;

        MyMessage(long seqId) {
            super("message");
            this.seqId = seqId;
        }

        @Override
        public boolean hasSequenceId() {
            return true;
        }

        @Override
        public long getSequenceId() {
            return seqId;
        }
    }
}