Skip to content

Commit d05695b

Browse files
arpan14gcf-owl-bot[bot]
authored andcommitted
chore: minor improvements to default benchmarks. (googleapis#2993)
* chore: minor improvements to default benchmarks. * chore: lint issues fix. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent b72581c commit d05695b

File tree

2 files changed

+74
-48
lines changed

2 files changed

+74
-48
lines changed

google-cloud-spanner/src/test/java/com/google/cloud/spanner/AbstractLatencyBenchmark.java

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.cloud.spanner;
1818

19+
import com.google.common.base.MoreObjects;
1920
import java.time.Duration;
2021
import java.util.ArrayList;
2122
import java.util.Collections;
@@ -24,6 +25,52 @@
2425

2526
public abstract class AbstractLatencyBenchmark {
2627

28+
static final String SELECT_QUERY = "SELECT ID FROM FOO WHERE ID = @id";
29+
static final String UPDATE_QUERY = "UPDATE FOO SET BAR=1 WHERE ID = @id";
30+
static final String ID_COLUMN_NAME = "id";
31+
32+
/**
33+
* Used to determine how many concurrent requests are allowed. For ex - To simulate a low QPS
34+
* scenario, using 1 thread means there will be 1 request. Use a value > 1 to have concurrent
35+
* requests.
36+
*/
37+
static final int PARALLEL_THREADS =
38+
Integer.valueOf(
39+
MoreObjects.firstNonNull(System.getenv("SPANNER_TEST_JMH_NUM_PARALLEL_THREADS"), "30"));
40+
41+
/**
42+
* Total number of reads per test run for 1 thread. Increasing the value here will increase the
43+
* duration of the benchmark. For ex - With PARALLEL_THREADS = 2, TOTAL_READS_PER_RUN = 200, there
44+
* will be 400 read requests (200 on each thread).
45+
*/
46+
static final int TOTAL_READS_PER_RUN =
47+
Integer.valueOf(
48+
MoreObjects.firstNonNull(
49+
System.getenv("SPANNER_TEST_JMH_NUM_READS_PER_THREAD"), "48000"));
50+
51+
/**
52+
* Total number of writes per test run for 1 thread. Increasing the value here will increase the
53+
* duration of the benchmark. For ex - With PARALLEL_THREADS = 2, TOTAL_WRITES_PER_RUN = 200,
54+
* there will be 400 write requests (200 on each thread).
55+
*/
56+
static final int TOTAL_WRITES_PER_RUN =
57+
Integer.valueOf(
58+
MoreObjects.firstNonNull(
59+
System.getenv("SPANNER_TEST_JMH_NUM_WRITES_PER_THREAD"), "4000"));
60+
61+
/**
62+
* Number of requests which are used to initialise/warmup the benchmark. The latency number of
63+
* these runs are ignored from the final reported results.
64+
*/
65+
static final int WARMUP_REQUEST_COUNT = 1;
66+
67+
/**
68+
* Numbers of records in the sample table used in the benchmark. This is used in this benchmark to
69+
* randomly choose a primary key and ensure that the reads are randomly distributed. This is done
70+
* to ensure we don't end up reading/writing the same table record (leading to hot-spotting).
71+
*/
72+
static final int TOTAL_RECORDS = 1000000;
73+
2774
/** Utility to print latency numbers. It computes metrics such as Average, P50, P95 and P99. */
2875
public void printResults(List<Duration> results) {
2976
if (results == null) {
@@ -33,23 +80,23 @@ public void printResults(List<Duration> results) {
3380
Collections.sort(orderedResults);
3481
System.out.println();
3582
System.out.printf("Total number of queries: %d\n", orderedResults.size());
36-
System.out.printf("Avg: %fs\n", avg(results));
37-
System.out.printf("P50: %fs\n", percentile(50, orderedResults));
38-
System.out.printf("P95: %fs\n", percentile(95, orderedResults));
39-
System.out.printf("P99: %fs\n", percentile(99, orderedResults));
83+
System.out.printf("Avg: %fms\n", avg(results));
84+
System.out.printf("P50: %fms\n", percentile(50, orderedResults));
85+
System.out.printf("P95: %fms\n", percentile(95, orderedResults));
86+
System.out.printf("P99: %fms\n", percentile(99, orderedResults));
4087
}
4188

4289
private double percentile(int percentile, List<Duration> orderedResults) {
4390
int index = percentile * orderedResults.size() / 100;
4491
Duration value = orderedResults.get(index);
45-
Double convertedValue = convertDurationToFractionInSeconds(value);
92+
Double convertedValue = convertDurationToFractionInMilliSeconds(value);
4693
return convertedValue;
4794
}
4895

4996
/** Returns the average duration in seconds from a list of duration values. */
5097
private double avg(List<Duration> results) {
5198
return results.stream()
52-
.collect(Collectors.averagingDouble(this::convertDurationToFractionInSeconds));
99+
.collect(Collectors.averagingDouble(this::convertDurationToFractionInMilliSeconds));
53100
}
54101

55102
private double convertDurationToFractionInSeconds(Duration duration) {
@@ -59,4 +106,9 @@ private double convertDurationToFractionInSeconds(Duration duration) {
59106
double value = seconds + fraction;
60107
return value;
61108
}
109+
110+
private double convertDurationToFractionInMilliSeconds(Duration duration) {
111+
long nanoseconds = duration.toNanos();
112+
return nanoseconds / 1000000.0;
113+
}
62114
}

google-cloud-spanner/src/test/java/com/google/cloud/spanner/DefaultBenchmark.java

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static com.google.cloud.spanner.BenchmarkingUtilityScripts.collectResults;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNotNull;
2123

2224
import com.google.common.base.Stopwatch;
2325
import com.google.common.util.concurrent.ListenableFuture;
@@ -37,6 +39,7 @@
3739
import org.openjdk.jmh.annotations.Measurement;
3840
import org.openjdk.jmh.annotations.Mode;
3941
import org.openjdk.jmh.annotations.OutputTimeUnit;
42+
import org.openjdk.jmh.annotations.Param;
4043
import org.openjdk.jmh.annotations.Scope;
4144
import org.openjdk.jmh.annotations.Setup;
4245
import org.openjdk.jmh.annotations.State;
@@ -45,8 +48,8 @@
4548

4649
/**
4750
* Benchmarks for measuring existing latencies of various APIs using the Java Client. The benchmarks
48-
* are bound to the Maven profile `benchmark` and can be executed like this: <code> mvn clean test
49-
* -DskipTests -Pbenchmark -Dbenchmark.name=DefaultBenchmark
51+
* are bound to the Maven profile `benchmark` and can be executed like this: <code>
52+
* mvn clean test -DskipTests -Pbenchmark -Dbenchmark.name=DefaultBenchmark
5053
* </code> Test Table Schema :
5154
*
5255
* <p>CREATE TABLE FOO ( id INT64 NOT NULL, BAZ INT64, BAR INT64, ) PRIMARY KEY(id);
@@ -63,47 +66,9 @@
6366
@Fork(value = 1, warmups = 0)
6467
@Measurement(batchSize = 1, iterations = 1, timeUnit = TimeUnit.MILLISECONDS)
6568
@OutputTimeUnit(TimeUnit.SECONDS)
66-
@Warmup(iterations = 1)
69+
@Warmup(iterations = 0)
6770
public class DefaultBenchmark extends AbstractLatencyBenchmark {
6871

69-
private static final String SELECT_QUERY = "SELECT ID FROM FOO WHERE ID = @id";
70-
private static final String UPDATE_QUERY = "UPDATE FOO SET BAR=1 WHERE ID = @id";
71-
private static final String ID_COLUMN_NAME = "id";
72-
73-
/**
74-
* Used to determine how many concurrent requests are allowed. For ex - To simulate a low QPS
75-
* scenario, using 1 thread means there will be 1 request. Use a value > 1 to have concurrent
76-
* requests.
77-
*/
78-
private static final int PARALLEL_THREADS = 1;
79-
80-
/**
81-
* Total number of reads per test run for 1 thread. Increasing the value here will increase the
82-
* duration of the benchmark. For ex - With PARALLEL_THREADS = 2, TOTAL_READS_PER_RUN = 200, there
83-
* will be 400 read requests (200 on each thread).
84-
*/
85-
private static final int TOTAL_READS_PER_RUN = 12000;
86-
87-
/**
88-
* Total number of writes per test run for 1 thread. Increasing the value here will increase the
89-
* duration of the benchmark. For ex - With PARALLEL_THREADS = 2, TOTAL_WRITES_PER_RUN = 200,
90-
* there will be 400 write requests (200 on each thread).
91-
*/
92-
private static final int TOTAL_WRITES_PER_RUN = 4000;
93-
94-
/**
95-
* Number of requests which are used to initialise/warmup the benchmark. The latency number of
96-
* these runs are ignored from the final reported results.
97-
*/
98-
private static final int WARMUP_REQUEST_COUNT = 1;
99-
100-
/**
101-
* Numbers of records in the sample table used in the benchmark. This is used in this benchmark to
102-
* randomly choose a primary key and ensure that the reads are randomly distributed. This is done
103-
* to ensure we don't end up reading/writing the same table record (leading to hot-spotting).
104-
*/
105-
private static final int TOTAL_RECORDS = 1000000;
106-
10772
@State(Scope.Thread)
10873
@AuxCounters(org.openjdk.jmh.annotations.AuxCounters.Type.EVENTS)
10974
public static class BenchmarkState {
@@ -115,12 +80,20 @@ public static class BenchmarkState {
11580
private Spanner spanner;
11681
private DatabaseClientImpl client;
11782

83+
@Param({"100"})
84+
int minSessions;
85+
86+
@Param({"400"})
87+
int maxSessions;
88+
11889
@Setup(Level.Iteration)
11990
public void setup() throws Exception {
12091
SpannerOptions options =
12192
SpannerOptions.newBuilder()
12293
.setSessionPoolOption(
12394
SessionPoolOptions.newBuilder()
95+
.setMinSessions(minSessions)
96+
.setMaxSessions(maxSessions)
12497
.setWaitForMinSessions(org.threeten.bp.Duration.ofSeconds(20))
12598
.build())
12699
.setHost(SERVER_URL)
@@ -217,7 +190,8 @@ private java.time.Duration executeQuery(final BenchmarkState server) {
217190

218191
try (ResultSet rs = server.client.singleUse().executeQuery(getRandomisedReadStatement())) {
219192
while (rs.next()) {
220-
int count = rs.getColumnCount();
193+
assertEquals(1, rs.getColumnCount());
194+
assertNotNull(rs.getValue(0));
221195
}
222196
}
223197
return watch.elapsed();

0 commit comments

Comments
 (0)