Skip to content

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

@spring-projects-issues

Description

@spring-projects-issues

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:

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

Backported to: 4.2.8

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions