Skip to content
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

Improve performance of assertion in StompSubProtocolHandler [SPR-14624] #19191

Closed
spring-projects-issues opened this issue Aug 24, 2016 · 1 comment

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Aug 24, 2016

Christoph Dreis opened SPR-14624 and commented

Hey,

I just noticed a rather costly assertion in the websocket layer - more explicitly in StompSubProtocolHandler.convertConnectAcktoStompConnected().

As the framework is now using Java 8 features, I wanted to add methods in Assert that can make use of suppliers. But you already did that ;-)

So I just used the new methods in an isolated test for a benchmark and the results show a crazy factor of 4300. I might be tricked by dead code elimination or some other compiler voodoo, because I don't trust those results.

@BenchmarkMode(Mode.Throughput)
@State(Scope.Thread)
public class MyBenchmark {

    @State(Scope.Thread)
    public static class TestState {
        public String testObject = "a";
        public StompHeaderAccessor headerAccessor;

        public TestState() {
            MultiValueMap<String, String> extHeaders = new LinkedMultiValueMap<>();
            extHeaders.add(StompHeaderAccessor.STOMP_LOGIN_HEADER, "joe");
            extHeaders.add(StompHeaderAccessor.STOMP_PASSCODE_HEADER, "joe123");
            headerAccessor = StompHeaderAccessor.create(StompCommand.CONNECT, extHeaders);
        }

    }

    @Benchmark
    public void testAssertNormal(TestState testState) {
        for (int i = 0; i < 1000; i++) {
            Assert.notNull(testState.testObject, "Original STOMP CONNECT not found in " + testState.headerAccessor);
        }
    }

    @Benchmark
    public void testAssertSupplier(TestState testState) {
        for (int i = 0; i < 1000; i++) {
            Assert.notNull(testState.testObject, () -> "Original STOMP CONNECT not found in " + testState.headerAccessor);
        }
    }

}
Benchmark Mode Cnt Score Error Units
MyBenchmark.testAssertNormal thrpt 30 966,149 12,945 ops/s
MyBenchmark.testAssertSupplier thrpt 30 4194886,453 72553,850 ops/s

Nevertheless, I changed the code to use the supplier methods in the attached PR.

Unfortunately, this will be only available in 5.x so I would appreciate a solution for 4.x that avoids the apparently rather costly assertion/string-concatenation of StompHeaderAccessor instances.

Best,
Christoph


Affects: 4.2.7, 4.3.2

Reference URL: #1138

Issue Links:

  • #19192 Fix assertions in StompHeaderAccessor
  • #19203 Improve performance of StompCommand.getMessageType()
  • #19313 Improve performance of StompEncoder.encode()

Referenced from: commits 0735e9b, 3811a59, 56b197b

Backported to: 4.2.8

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Aug 24, 2016

Juergen Hoeller commented

Good catch! It's also throwing the wrong exception type, actually, since this is really a state assertion, not an argument assertion. For the sake of consistency, I've turned this into a traditional if clause throwing an IllegalStateException.

Additionally, StompSubProtocolHandler contained several inefficient logging statements with String concatenation. I've revised all of those to be guarded accordingly and will backport those to 4.3.3 and 4.2.8 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants