From a0e760fbb1f5a9b169045ceba2dcf682d4736995 Mon Sep 17 00:00:00 2001 From: Praful Makani Date: Tue, 25 Aug 2020 22:41:22 +0530 Subject: [PATCH] feat: add jmh benchmark module (#686) * add jmh benchmark * fix typo for jar name * update README.md with additional instructions to run Benchmarks * update benchmarkMode to AverageTime and reduce iterations Co-authored-by: stephwang --- benchmark/README.md | 20 ++++ benchmark/pom.xml | 77 +++++++++++++++ .../QueryBenchmark.java | 96 +++++++++++++++++++ pom.xml | 6 ++ 4 files changed, 199 insertions(+) create mode 100644 benchmark/README.md create mode 100644 benchmark/pom.xml create mode 100644 benchmark/src/main/java/com.google.cloud.bigquery/QueryBenchmark.java diff --git a/benchmark/README.md b/benchmark/README.md new file mode 100644 index 000000000..c29bf0781 --- /dev/null +++ b/benchmark/README.md @@ -0,0 +1,20 @@ +Benchmarking +============ + +To build all of the benchmarks: +``` +# Run from benchmark directory +cd benchmark +mvn clean install +``` + +Set `GOOGLE_APPLICATION_CREDENTIALS`: +``` +export GOOGLE_APPLICATION_CREDENTIALS=path/to/service_account.json +``` + +To run a benchmark jar, run the following command +``` +# Run from project root directory + java -jar target/benchmark.jar +``` diff --git a/benchmark/pom.xml b/benchmark/pom.xml new file mode 100644 index 000000000..da9cd1863 --- /dev/null +++ b/benchmark/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + benchmark + + google-cloud-bigquery-parent + com.google.cloud + 1.116.11-SNAPSHOT + + + + UTF-8 + 1.23 + benchmark + + + + + com.google.cloud + google-cloud-bigquery + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + ${uberjar.name} + + + org.openjdk.jmh.Main + + + + + + *:* + + module-info.class + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + diff --git a/benchmark/src/main/java/com.google.cloud.bigquery/QueryBenchmark.java b/benchmark/src/main/java/com.google.cloud.bigquery/QueryBenchmark.java new file mode 100644 index 000000000..79aa484d8 --- /dev/null +++ b/benchmark/src/main/java/com.google.cloud.bigquery/QueryBenchmark.java @@ -0,0 +1,96 @@ +/* + * Copyright 2020 Google LLC + * + * 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 com.google.cloud.bigquery; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +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.annotations.Warmup; +import org.openjdk.jmh.infra.Blackhole; + +@Fork(value = 1) +@BenchmarkMode(Mode.AverageTime) +@Warmup(iterations = 5) +@Measurement(iterations = 5) +@State(Scope.Benchmark) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +public class QueryBenchmark { + + private static final String NYCYELLOWLIMIT1K = "SELECT * FROM `nyc-tlc.yellow.trips` LIMIT 10000"; + private static final String NYCYELLOWLIMIT10K = + "SELECT * FROM `nyc-tlc.yellow.trips` LIMIT 100000"; + private static final String NYCYELLOWLIMIT100K = + "SELECT * FROM `nyc-tlc.yellow.trips` LIMIT 1000000"; + private static final String WIKISAMPLESORDEREDLIMIT1K = + "SELECT title FROM `bigquery-public-data.samples.wikipedia` ORDER BY title LIMIT 1000"; + private static final String CURRENTTIMESTAMP = "SELECT CURRENT_TIMESTAMP() as ts"; + private static final String SESSIONUSER = "SELECT SESSION_USER() as ts"; + private static final String LITERALS = "SELECT 1 as i, 3.14 as pi"; + private static final String INVALIDQUERY = + "CREATE OR REPLACE SELECT * FROM UPDATE TABLE SET `nyc-tlc.yellow.trips`"; + + private BigQuery bigquery; + + @Setup + public void setUp() { + this.bigquery = BigQueryOptions.getDefaultInstance().getService(); + } + + @State(Scope.Benchmark) + public static class QueryParams { + + @Param({ + NYCYELLOWLIMIT1K, + NYCYELLOWLIMIT10K, + NYCYELLOWLIMIT100K, + WIKISAMPLESORDEREDLIMIT1K, + CURRENTTIMESTAMP, + SESSIONUSER, + LITERALS, + INVALIDQUERY + }) + public String queries; + } + + private void queryPerform(String queries, Blackhole blackhole) throws Exception { + TableResult result = + bigquery.query(QueryJobConfiguration.newBuilder(queries).setUseLegacySql(false).build()); + for (List row : result.iterateAll()) { + blackhole.consume(row.size()); + } + while (result.hasNextPage()) { + result = result.getNextPage(); + for (List row : result.iterateAll()) { + blackhole.consume(row.size()); + } + } + } + + @Benchmark + public void query(QueryParams queryParams, Blackhole blackhole) throws Exception { + queryPerform(queryParams.queries, blackhole); + } +} diff --git a/pom.xml b/pom.xml index f1a136c17..f831cbd5a 100644 --- a/pom.xml +++ b/pom.xml @@ -215,5 +215,11 @@ samples + + benchmark + + benchmark + +