|
| 1 | +package io.libp2p.pubsub.gossip; |
| 2 | + |
| 3 | +import io.libp2p.core.PeerId; |
| 4 | +import io.libp2p.core.multiformats.Multiaddr; |
| 5 | +import io.libp2p.core.multiformats.Protocol; |
| 6 | +import io.libp2p.pubsub.DefaultPubsubMessage; |
| 7 | +import io.libp2p.tools.schedulers.ControlledExecutorServiceImpl; |
| 8 | +import io.libp2p.tools.schedulers.TimeController; |
| 9 | +import io.libp2p.tools.schedulers.TimeControllerImpl; |
| 10 | +import org.openjdk.jmh.annotations.*; |
| 11 | +import org.openjdk.jmh.infra.Blackhole; |
| 12 | +import pubsub.pb.Rpc; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.function.Function; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.IntStream; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +@State(Scope.Thread) |
| 23 | +@Fork(5) |
| 24 | +@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS) |
| 25 | +@Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS) |
| 26 | +public class GossipScoreBenchmark { |
| 27 | + |
| 28 | + private final int peerCount = 5000; |
| 29 | + private final int connectedCount = 2000; |
| 30 | + private final int topicCount = 128; |
| 31 | + |
| 32 | + private final List<String> topics = IntStream |
| 33 | + .range(0, topicCount) |
| 34 | + .mapToObj(i -> "Topic-" + i) |
| 35 | + .collect(Collectors.toList()); |
| 36 | + |
| 37 | + private final List<PeerId> peerIds = Stream.generate(PeerId::random).limit(peerCount).collect(Collectors.toList()); |
| 38 | + private final List<Multiaddr> peerAddresses = IntStream |
| 39 | + .range(0, peerCount) |
| 40 | + .mapToObj(idx -> |
| 41 | + Multiaddr.empty() |
| 42 | + .withComponent(Protocol.IP4, new byte[]{(byte) (idx >>> 8 & 0xFF), (byte) (idx & 0xFF), 0, 0}) |
| 43 | + .withComponent(Protocol.TCP, new byte[]{0x23, 0x28})) |
| 44 | + .collect(Collectors.toList()); |
| 45 | + |
| 46 | + private final TimeController timeController = new TimeControllerImpl(); |
| 47 | + private final ControlledExecutorServiceImpl controlledExecutor = new ControlledExecutorServiceImpl(); |
| 48 | + private final GossipScoreParams gossipScoreParams; |
| 49 | + private final DefaultGossipScore score; |
| 50 | + |
| 51 | + public GossipScoreBenchmark() { |
| 52 | + Map<String, GossipTopicScoreParams> topicParamMap = topics.stream() |
| 53 | + .collect(Collectors.toMap(Function.identity(), __ -> new GossipTopicScoreParams())); |
| 54 | + GossipTopicsScoreParams gossipTopicsScoreParams = new GossipTopicsScoreParams(new GossipTopicScoreParams(), topicParamMap); |
| 55 | + |
| 56 | + gossipScoreParams = new GossipScoreParams(new GossipPeerScoreParams(), gossipTopicsScoreParams, 0, 0, 0, 0, 0); |
| 57 | + controlledExecutor.setTimeController(timeController); |
| 58 | + score = new DefaultGossipScore(gossipScoreParams, controlledExecutor, timeController::getTime); |
| 59 | + |
| 60 | + for (int i = 0; i < peerCount; i++) { |
| 61 | + PeerId peerId = peerIds.get(i); |
| 62 | + score.notifyConnected(peerId, peerAddresses.get(i)); |
| 63 | + for (String topic : topics) { |
| 64 | + notifyUnseenMessage(peerId, topic); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + for (int i = connectedCount; i < peerCount; i++) { |
| 69 | + score.notifyDisconnected(peerIds.get(i)); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private void notifyUnseenMessage(PeerId peerId, String topic) { |
| 74 | + Rpc.Message message = Rpc.Message.newBuilder() |
| 75 | + .addTopicIDs(topic) |
| 76 | + .build(); |
| 77 | + score.notifyUnseenValidMessage(peerId, new DefaultPubsubMessage(message)); |
| 78 | + } |
| 79 | + |
| 80 | + @Benchmark |
| 81 | + public void scoresDelay0(Blackhole bh) { |
| 82 | + for (int i = 0; i < connectedCount; i++) { |
| 83 | + double s = score.score(peerIds.get(i)); |
| 84 | + bh.consume(s); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Benchmark |
| 89 | + public void scoresDelay100(Blackhole bh) { |
| 90 | + timeController.addTime(100); |
| 91 | + |
| 92 | + for (int i = 0; i < connectedCount; i++) { |
| 93 | + double s = score.score(peerIds.get(i)); |
| 94 | + bh.consume(s); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Benchmark |
| 99 | + public void scoresDelay10000(Blackhole bh) { |
| 100 | + timeController.addTime(10000); |
| 101 | + |
| 102 | + for (int i = 0; i < connectedCount; i++) { |
| 103 | + double s = score.score(peerIds.get(i)); |
| 104 | + bh.consume(s); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Uncomment for debugging |
| 110 | + */ |
| 111 | +// public static void main(String[] args) { |
| 112 | +// GossipScoreBenchmark benchmark = new GossipScoreBenchmark(); |
| 113 | +// Blackhole blackhole = new Blackhole("Today's password is swordfish. I understand instantiating Blackholes directly is dangerous."); |
| 114 | +// benchmark.scoresDelay0(blackhole); |
| 115 | +// } |
| 116 | +} |
0 commit comments