aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/LatencyTest.java
blob: bd54c5654ee6819577e9c0479f2feae3ced67b85 (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
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.jrt;

import java.time.Duration;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Logger;

import static com.yahoo.jrt.CryptoUtils.createTestTlsContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class LatencyTest {
    private static final Logger log = Logger.getLogger(LatencyTest.class.getName());

    private static class Network implements AutoCloseable {
        private final Supervisor server;
        private final Supervisor client;
        private final Acceptor acceptor;
        public Network(CryptoEngine crypto, int threads, boolean dropEmpty) throws ListenFailedException {
            server = new Supervisor(new Transport("server", crypto, threads));
            client = new Supervisor(new Transport("client", crypto, threads));
            server.setDropEmptyBuffers(dropEmpty);
            client.setDropEmptyBuffers(dropEmpty);
            server.addMethod(new Method("inc", "i", "i", this::rpc_inc));
            acceptor = server.listen(new Spec(0));
        }
        public Network(CryptoEngine crypto, int threads) throws ListenFailedException { this(crypto, threads, false); }
        public Target connect() {
            return client.connect(new Spec("localhost", acceptor.port()));
        }
        private void rpc_inc(Request req) {
            req.returnValues().add(new Int32Value(req.parameters().get(0).asInt32() + 1));
        }
        public void close() {
            acceptor.shutdown().join();
            client.transport().shutdown().join();
            server.transport().shutdown().join();
        }
    }

    private static class Client {

        public static class Result {
            public final double latency;
            public final double throughput;

            public Result(double ms, double cnt) {
                latency = ms;
                throughput = cnt;
            }

            public Result(Result[] results) {
                double ms = 0.0;
                double cnt = 0.0;
                for (Result r: results) {
                    ms += r.latency;
                    cnt += r.throughput;
                }
                latency = (ms / results.length);
                throughput = cnt;
            }
        }

        private final boolean reconnect;
        private final Network network;
        private final CyclicBarrier barrier;
        private final CountDownLatch latch;
        private final Throwable[] issues;
        private final Result[] results;

        private enum State { WARMUP, BENCHMARK, COOLDOWN }
        private long warmupEnd = 0;
        private long benchmarkEnd = 0;
        private long cooldownEnd = 0;

        private long addSecs(long ns, double s) {
            return ns + (long)(s * 1000_000_000);
        }

        private void setupBenchmark(double warmup, double benchmark, double cooldown) {
            final long now = System.nanoTime();
            warmupEnd = addSecs(now, warmup);
            benchmarkEnd = addSecs(warmupEnd, benchmark);
            cooldownEnd = addSecs(benchmarkEnd, cooldown);
        }

        private void run(int threadId) {
            try {
                barrier.await();
                State state = State.WARMUP;
                int value = 0;
                long t1 = 0;
                long t2 = 0;
                int s1 = 0;
                int s2 = 0;
                double minLatency = 1.0e99;
                Target target = network.connect();
                for (long t = System.nanoTime();
                     state != State.COOLDOWN || t < cooldownEnd;
                     t = System.nanoTime())
                {
                    if ((state == State.WARMUP) && (t >= warmupEnd)) {
                        t1 = t;
                        s1 = value;
                        state = State.BENCHMARK;
                    }
                    if ((state == State.BENCHMARK) && (t >= benchmarkEnd)) {
                        t2 = t;
                        s2 = value;
                        state = State.COOLDOWN;
                    }
                    if (reconnect) {
                        target.close();
                        target = network.connect();
                    }
                    Request req = new Request("inc");
                    req.parameters().add(new Int32Value(value));
                    target.invokeSync(req, Duration.ofSeconds(60));
                    long duration = System.nanoTime() - t;
                    assertTrue(req.checkReturnTypes("i"));
                    assertEquals(value + 1, req.returnValues().get(0).asInt32());
                    ++value;
                    double latency = (duration / 1000_000.0);
                    if (latency < minLatency) {
                        minLatency = latency;
                    }
                }
                target.close();
                double benchTime = (t2 - t1) / 1000_000_000.0;
                results[threadId] = new Result(minLatency, (s2 - s1) / benchTime);
            } catch (Throwable issue) {
                issues[threadId] = issue;
            } finally {
                latch.countDown();
            }
        }

        public Client(boolean reconnect, Network network, int numThreads,
                      double warmup, double benchmark, double cooldown)
        {
            this.reconnect = reconnect;
            this.network = network;
            this.barrier = new CyclicBarrier(numThreads, ()->setupBenchmark(warmup, benchmark, cooldown));
            this.latch = new CountDownLatch(numThreads);
            this.issues = new Throwable[numThreads];
            this.results = new Result[numThreads];
        }
        public Client(boolean reconnect, Network network, int numThreads) {
            this(reconnect, network, numThreads, 0.1, 0.5, 0.1);
        }

        public void measureLatency(String prefix) throws Throwable {
            for (int i = 0; i < results.length; ++i) {
                final int threadId = i;
                new Thread(()->run(threadId)).start();
            }
            latch.await();
            for (Throwable issue: issues) {
                if (issue != null) {
                    throw(issue);
                }
            }
            Result result = new Result(results);
            log.info(prefix + "latency: " + result.latency + " ms, throughput: " + result.throughput + " req/s");
        }
    }

    @org.junit.Test
    public void testNullCryptoLatency() throws Throwable {
        try (Network network = new Network(new NullCryptoEngine(), 1)) {
            new Client(false, network, 1).measureLatency("[null crypto, no reconnect] ");
            new Client(true, network, 1).measureLatency("[null crypto, reconnect] ");
        }
    }

    @org.junit.Test
    public void testTlsCryptoLatency() throws Throwable {
        try (Network network = new Network(new TlsCryptoEngine(createTestTlsContext()), 1)) {
            new Client(false, network, 1).measureLatency("[tls crypto, no reconnect] ");
            new Client(true, network, 1).measureLatency("[tls crypto, reconnect] ");
        }
    }

    @org.junit.Test
    public void testTlsCryptoWithDropEmptyBuffersLatency() throws Throwable {
        try (Network network = new Network(new TlsCryptoEngine(createTestTlsContext()), 1, true)) {
            new Client(false, network, 1).measureLatency("[tls crypto, drop empty, no reconnect] ");
        }
    }

    @org.junit.Test
    public void testTransportThreadScaling() throws Throwable {
        try (Network network = new Network(new NullCryptoEngine(), 1)) {
            new Client(false, network, 64).measureLatency("[64 clients, 1/1 transport] ");
        }
        try (Network network = new Network(new NullCryptoEngine(), 4)) {
            new Client(false, network, 64).measureLatency("[64 clients, 4/4 transport] ");
        }
    }
}