Skip to content

DefaultSubscriptionRegistry: Reduced thread contention #25298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spring-messaging/spring-messaging.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ dependencies {
testRuntime("com.sun.xml.bind:jaxb-core")
testRuntime("com.sun.xml.bind:jaxb-impl")
testRuntime("com.sun.activation:javax.activation")
testRuntime(project(":spring-context"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.messaging.simp.broker;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

import org.springframework.messaging.Message;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.MultiValueMap;

@BenchmarkMode(Mode.Throughput)
public class DefaultSubscriptionRegistryBenchmark {

@State(Scope.Benchmark)
public static class ServerState {
@Param("1000")
public int sessions;

@Param("10")
public int destinations;

@Param({"0", "1024"})
int cacheSizeLimit;

@Param({"none", "patternSubscriptions", "selectorHeaders"})
String specialization;

public DefaultSubscriptionRegistry registry;

public String[] destinationIds;

public String[] sessionIds;

public AtomicInteger uniqueIdGenerator;

public Message<?> findMessage;

@Setup(Level.Trial)
public void doSetup() {
this.findMessage = MessageBuilder.createMessage("", SimpMessageHeaderAccessor.create().getMessageHeaders());
this.uniqueIdGenerator = new AtomicInteger();

this.registry = new DefaultSubscriptionRegistry();
this.registry.setCacheLimit(this.cacheSizeLimit);
this.registry.setSelectorHeaderName("selectorHeaders".equals(this.specialization) ? "someSelector" : null);

this.destinationIds = IntStream.range(0, this.destinations)
.mapToObj(i -> "/some/destination/" + i)
.toArray(String[]::new);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There probably should be some pattern destinations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a benchmark for pattern destinations. Actually, there two places where I can put a pattern.

  1. new subscription can be a pattern destination
  2. other already registered subscriptions can be a pattern
    I will add a flag at both places.


this.sessionIds = IntStream.range(0, this.sessions)
.mapToObj(i -> "sessionId_" + i)
.toArray(String[]::new);

for (String sessionId : this.sessionIds) {
for (String destinationId : this.destinationIds) {
registerSubscriptions(sessionId, destinationId);
}
}
}

public void registerSubscriptions(String sessionId, String destination) {
if ("patternSubscriptions".equals(this.specialization)) {
destination = "/**/" + destination;
}
String subscriptionId = "subscription_" + this.uniqueIdGenerator.incrementAndGet();
this.registry.registerSubscription(subscribeMessage(sessionId, subscriptionId, destination));
}
}

@State(Scope.Thread)
public static class Requests {
@Param({"none", "sameDestination", "sameSession"})
String contention;

public String session;

public Message<?> subscribe;

public String findDestination;

public Message<?> unsubscribe;

@Setup(Level.Trial)
public void doSetup(ServerState serverState) {
int uniqueNumber = serverState.uniqueIdGenerator.incrementAndGet();

if ("sameDestination".equals(this.contention)) {
this.findDestination = serverState.destinationIds[0];
}
else {
this.findDestination = serverState.destinationIds[uniqueNumber % serverState.destinationIds.length];
}

if ("sameSession".equals(this.contention)) {
this.session = serverState.sessionIds[0];
}
else {
this.session = serverState.sessionIds[uniqueNumber % serverState.sessionIds.length];
}

String subscription = String.valueOf(uniqueNumber);
String subscribeDestination = "patternSubscriptions".equals(serverState.specialization) ?
"/**/" + this.findDestination : this.findDestination;
this.subscribe = subscribeMessage(this.session, subscription, subscribeDestination);

this.unsubscribe = unsubscribeMessage(this.session, subscription);
}
}

@State(Scope.Thread)
public static class FindRequest {
@Param({"none", "noSubscribers", "sameDestination"})
String contention;

public String destination;

@Setup(Level.Trial)
public void doSetup(ServerState serverState) {
switch (this.contention) {
case "noSubscribers":
this.destination = "someDestination_withNoSubscribers_" + serverState.uniqueIdGenerator.incrementAndGet();
break;
case "sameDestination":
this.destination = serverState.destinationIds[0];
break;
case "none":
int uniqueNumber = serverState.uniqueIdGenerator.getAndIncrement();
this.destination = serverState.destinationIds[uniqueNumber % serverState.destinationIds.length];
break;
default:
throw new IllegalStateException();
}
}
}

@Benchmark
public void registerUnregister(ServerState serverState, Requests request, Blackhole blackhole) {
serverState.registry.registerSubscription(request.subscribe);
blackhole.consume(serverState.registry.findSubscriptionsInternal(request.findDestination, serverState.findMessage));
serverState.registry.unregisterSubscription(request.unsubscribe);
blackhole.consume(serverState.registry.findSubscriptionsInternal(request.findDestination, serverState.findMessage));
}

@Benchmark
public MultiValueMap<String, String> find(ServerState serverState, FindRequest request) {
return serverState.registry.findSubscriptionsInternal(request.destination, serverState.findMessage);
}

public static Message<?> subscribeMessage(String sessionId, String subscriptionId, String dest) {
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.SUBSCRIBE);
accessor.setSessionId(sessionId);
accessor.setSubscriptionId(subscriptionId);
accessor.setDestination(dest);
accessor.setNativeHeader("someSelector", "true");
return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}

public static Message<?> unsubscribeMessage(String sessionId, String subscriptionId) {
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create(SimpMessageType.UNSUBSCRIBE);
accessor.setSessionId(sessionId);
accessor.setSubscriptionId(subscriptionId);
return MessageBuilder.createMessage("", accessor.getMessageHeaders());
}
}
Loading