|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.appulse.encon.benchmark; |
| 18 | + |
| 19 | +import static io.appulse.encon.terms.Erlang.binary; |
| 20 | +import static java.lang.Boolean.TRUE; |
| 21 | +import static java.util.concurrent.TimeUnit.SECONDS; |
| 22 | +import static org.openjdk.jmh.annotations.Level.Trial; |
| 23 | +import static org.openjdk.jmh.annotations.Mode.Throughput; |
| 24 | +import static org.openjdk.jmh.annotations.Scope.Benchmark; |
| 25 | + |
| 26 | +import java.util.stream.IntStream; |
| 27 | + |
| 28 | +import io.appulse.encon.Node; |
| 29 | +import io.appulse.encon.Nodes; |
| 30 | +import io.appulse.encon.config.NodeConfig; |
| 31 | +import io.appulse.encon.config.ServerConfig; |
| 32 | +import io.appulse.encon.mailbox.Mailbox; |
| 33 | +import io.appulse.encon.terms.ErlangTerm; |
| 34 | +import io.appulse.encon.terms.type.ErlangPid; |
| 35 | + |
| 36 | +import org.openjdk.jmh.annotations.Benchmark; |
| 37 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 38 | +import org.openjdk.jmh.annotations.Measurement; |
| 39 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 40 | +import org.openjdk.jmh.annotations.Setup; |
| 41 | +import org.openjdk.jmh.annotations.State; |
| 42 | +import org.openjdk.jmh.annotations.TearDown; |
| 43 | +import org.openjdk.jmh.annotations.Threads; |
| 44 | +import org.openjdk.jmh.annotations.Warmup; |
| 45 | +import org.openjdk.jmh.infra.Blackhole; |
| 46 | +import org.openjdk.jmh.infra.ThreadParams; |
| 47 | + |
| 48 | +/** |
| 49 | + * |
| 50 | + * @since 1.6.3 |
| 51 | + * @author Artem Labazin |
| 52 | + */ |
| 53 | +@State(Benchmark) |
| 54 | +@OutputTimeUnit(SECONDS) |
| 55 | +@Warmup(iterations = 10) |
| 56 | +@BenchmarkMode(Throughput) |
| 57 | +@Measurement(iterations = 20) |
| 58 | +public class Encon_Node2NodeBenchmarks_4WorkersThreads { |
| 59 | + |
| 60 | + Node serverNode; |
| 61 | + |
| 62 | + Mailbox serverMailbox; |
| 63 | + |
| 64 | + ErlangPid serverMailboxPid; |
| 65 | + |
| 66 | + Thread serverThread; |
| 67 | + |
| 68 | + ErlangTerm data; |
| 69 | + |
| 70 | + Node clientNode; |
| 71 | + |
| 72 | + Mailbox[] clientMailboxes; |
| 73 | + |
| 74 | + @Setup(Trial) |
| 75 | + public void setup () throws Exception { |
| 76 | + serverNode = Nodes.singleNode("node-server-" + System.nanoTime(), NodeConfig.builder() |
| 77 | + .shortName(TRUE) |
| 78 | + .server(ServerConfig.builder() |
| 79 | + .bossThreads(1) |
| 80 | + .workerThreads(4) |
| 81 | + .build() |
| 82 | + ) |
| 83 | + .build() |
| 84 | + ); |
| 85 | + |
| 86 | + serverMailbox = serverNode.mailbox().build(); |
| 87 | + serverMailboxPid = serverMailbox.getPid(); |
| 88 | + data = binary(new byte[] { 1, 2, 3, 4, 5 }); |
| 89 | + |
| 90 | + serverThread = new Thread(() -> { |
| 91 | + try { |
| 92 | + while (!java.lang.Thread.interrupted()) { |
| 93 | + ErlangTerm payload = serverMailbox.receive().getBody(); |
| 94 | + serverMailbox.send(payload.asPid(), data); |
| 95 | + } |
| 96 | + } catch (Throwable ex) { |
| 97 | + } |
| 98 | + }); |
| 99 | + serverThread.start(); |
| 100 | + |
| 101 | + clientNode = Nodes.singleNode("node-client-" + System.nanoTime(), NodeConfig.builder().shortName(TRUE) |
| 102 | + .server(ServerConfig.builder() |
| 103 | + .bossThreads(1) |
| 104 | + .workerThreads(8) |
| 105 | + .build() |
| 106 | + ) |
| 107 | + .build()); |
| 108 | + |
| 109 | + clientMailboxes = IntStream.range(0, 8) |
| 110 | + .boxed() |
| 111 | + .map(it -> clientNode.mailbox().build()) |
| 112 | + .toArray(Mailbox[]::new); |
| 113 | + } |
| 114 | + |
| 115 | + @TearDown(Trial) |
| 116 | + public void tearDown () { |
| 117 | + for (Mailbox mailbox : clientMailboxes) { |
| 118 | + mailbox.close(); |
| 119 | + } |
| 120 | + clientNode.close(); |
| 121 | + |
| 122 | + serverMailbox.close(); |
| 123 | + serverNode.close(); |
| 124 | + |
| 125 | + serverThread.interrupt(); |
| 126 | + } |
| 127 | + |
| 128 | + @Threads(1) |
| 129 | + @Benchmark |
| 130 | + public void client_1 (ThreadParams thredParams, Blackhole blackHole) throws Exception { |
| 131 | + Mailbox mailbox = clientMailboxes[0]; |
| 132 | + mailbox.send(serverMailboxPid, mailbox.getPid()); |
| 133 | + blackHole.consume(mailbox.receive()); |
| 134 | + } |
| 135 | + |
| 136 | + @Threads(2) |
| 137 | + @Benchmark |
| 138 | + public void clients_2 (ThreadParams thredParams, Blackhole blackHole) throws Exception { |
| 139 | + Mailbox mailbox = clientMailboxes[thredParams.getThreadIndex()]; |
| 140 | + mailbox.send(serverMailboxPid, mailbox.getPid()); |
| 141 | + blackHole.consume(mailbox.receive()); |
| 142 | + } |
| 143 | + |
| 144 | + @Threads(4) |
| 145 | + @Benchmark |
| 146 | + public void clients_4 (ThreadParams thredParams, Blackhole blackHole) throws Exception { |
| 147 | + Mailbox mailbox = clientMailboxes[thredParams.getThreadIndex()]; |
| 148 | + mailbox.send(serverMailboxPid, mailbox.getPid()); |
| 149 | + blackHole.consume(mailbox.receive()); |
| 150 | + } |
| 151 | + |
| 152 | + @Threads(8) |
| 153 | + @Benchmark |
| 154 | + public void clients_8 (ThreadParams thredParams, Blackhole blackHole) throws Exception { |
| 155 | + Mailbox mailbox = clientMailboxes[thredParams.getThreadIndex()]; |
| 156 | + mailbox.send(serverMailboxPid, mailbox.getPid()); |
| 157 | + blackHole.consume(mailbox.receive()); |
| 158 | + } |
| 159 | +} |
0 commit comments