Skip to content

Commit

Permalink
Merge branch 'master' into commons_io
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Peshin committed Apr 3, 2024
2 parents 8fc4014 + e268a24 commit 96b0a43
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
69 changes: 69 additions & 0 deletions src/test/java/benchmarks/HandlebarsOptimizedTemplateBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (C) 2024 Thomas Akehurst
*
* 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
*
* http://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 benchmarks;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.matching.MockRequest.mockRequest;
import static com.github.tomakehurst.wiremock.stubbing.ServeEventFactory.newPostMatchServeEvent;

import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
import com.github.tomakehurst.wiremock.extension.responsetemplating.ResponseTemplateTransformer;
import com.github.tomakehurst.wiremock.matching.MockRequest;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.github.tomakehurst.wiremock.testsupport.ExtensionFactoryUtils;
import org.openjdk.jmh.annotations.*;

@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 2)
@Fork(1)
@Measurement(iterations = 5)
public class HandlebarsOptimizedTemplateBenchmark {

@State(Scope.Benchmark)
public static class HandlebarsOptimizedTemplateBenchmarkState {
private ResponseTemplateTransformer transformer;

@Setup
public void setup() {
transformer = ExtensionFactoryUtils.buildTemplateTransformer(true);
}
}

@Benchmark
@Threads(50)
public boolean transform(HandlebarsOptimizedTemplateBenchmarkState state) {

String result =
transform(
"{{#each (range 100000 199999) as |index|}}Line {{index}}\n{{/each}}",
state.transformer);

boolean hasCorrectStart = result.startsWith("Line 100000\nLine 100001\nLine 100002\n");
boolean hasCorrectLength = result.length() == 1_200_000;
return hasCorrectStart && hasCorrectLength;
}

private String transform(String responseBodyTemplate, ResponseTemplateTransformer transformer) {
final ResponseDefinitionBuilder responseDefinitionBuilder =
aResponse().withBody(responseBodyTemplate);
final StubMapping stub = get("/").willReturn(responseDefinitionBuilder).build();
final MockRequest request = mockRequest();
ServeEvent serveEvent = newPostMatchServeEvent(request, responseDefinitionBuilder, stub);
return transformer.transform(serveEvent).getBody();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2023 Thomas Akehurst
* Copyright (C) 2016-2024 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,8 +21,6 @@
import static java.time.temporal.ChronoUnit.DAYS;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;

import com.github.jknack.handlebars.Helper;
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
Expand All @@ -36,8 +34,6 @@
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.github.tomakehurst.wiremock.testsupport.ExtensionFactoryUtils;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import java.time.Duration;
import java.time.Instant;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;
Expand Down Expand Up @@ -1034,18 +1030,6 @@ public void canParseLocalYear() {
assertThat(result, is(expected));
}

@Test
public void canHandleALargeTemplateReasonablyFast() {
String template = "{{#each (range 100000 199999) as |index|}}Line {{index}}\n{{/each}}";
Instant start = Instant.now();
String result = transform(template);
Duration timeTaken = Duration.between(start, Instant.now());

assertThat(result.substring(0, 100), startsWith("Line 100000\nLine 100001\nLine 100002\n"));
assertThat(result.length(), equalTo(1_200_000));
assertThat(timeTaken, lessThan(Duration.ofSeconds(5)));
}

private Integer transformToInt(String responseBodyTemplate) {
return Integer.parseInt(transform(responseBodyTemplate));
}
Expand Down

0 comments on commit 96b0a43

Please sign in to comment.