Skip to content

Commit 825f7dd

Browse files
authored
chore(bigquery-jdbc): update perf client to run custom queries (#13521)
1 parent b59e07a commit 825f7dd

5 files changed

Lines changed: 90 additions & 25 deletions

File tree

java-bigquery-jdbc/tools/client/JDBCClient.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static void main(String[] args) throws Exception {
2323
String driverClass = "com.google.cloud.bigquery.jdbc.BigQueryDriver";
2424
String action = null;
2525
String query = null;
26+
String queryFile = null;
2627
boolean noOutput = false;
2728
int generateRows = 0;
2829
int generateCols = 5;
@@ -53,6 +54,7 @@ public static void main(String[] args) throws Exception {
5354
case "driver-class": driverClass = val; break;
5455
case "action": action = val; break;
5556
case "query": query = val; break;
57+
case "query-file": queryFile = val; break;
5658
case "no-output": noOutput = true; break;
5759
case "generate-rows": generateRows = Integer.parseInt(val); break;
5860
case "generate-cols": generateCols = Integer.parseInt(val); break;
@@ -88,11 +90,19 @@ public static void main(String[] args) throws Exception {
8890
System.out.println("Connection successful.\n");
8991

9092
if ("query".equals(action)) {
91-
if (generateRows > 0) {
93+
if (query == null && queryFile != null) {
94+
try {
95+
query = readQueryFromFile(queryFile);
96+
} catch (Exception e) {
97+
System.err.println("Error reading query from file: " + e.getMessage());
98+
System.exit(1);
99+
}
100+
}
101+
if (query == null && generateRows > 0) {
92102
query = generateDataQuery(generateRows, generateCols);
93103
}
94104
if (query == null) {
95-
System.err.println("Error: --query or --generate-rows is required when action is 'query'");
105+
System.err.println("Error: --query, --query-file, or --generate-rows is required when action is 'query'");
96106
System.exit(1);
97107
}
98108
warmup(conn);
@@ -122,6 +132,10 @@ private static void warmup(Connection conn) {
122132
System.out.println("Warmup complete.\n");
123133
}
124134

135+
private static String readQueryFromFile(String path) throws Exception {
136+
return new String(java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(path)), java.nio.charset.StandardCharsets.UTF_8);
137+
}
138+
125139
private static String generateDataQuery(int rows, int cols) {
126140
int N = (int) Math.ceil(Math.sqrt(rows));
127141
String idxExpr = "(i - 1) * " + N + " + j";

java-bigquery-jdbc/tools/client/Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@ PARAMS = ProjectId=bigquery-devtools-drivers;OAuthType=0;OAuthServiceAcctEmail=;
2727
# Additional connection parameters
2828
EXTRA_PARAMS ?=
2929

30+
METHOD ?= getTables
31+
3032
ROWS ?= 10
3133
COLS ?= 5
32-
METHOD ?= getTables
3334

3435
OUTPUT ?= false
3536
QUERY ?= SELECT 1
37+
QUERY_FILE ?=
38+
39+
ifneq ($(QUERY_FILE),)
40+
QUERY_FLAG = --query-file "$(QUERY_FILE)"
41+
else
42+
QUERY_FLAG = --query "$(QUERY)"
43+
endif
3644

3745
ifeq ($(OUTPUT),false)
3846
OUTPUT_FLAG = --no-output
@@ -53,7 +61,7 @@ COMMON_FLAGS = --url "$(URL);$(DEFAULT_PARAMS);$(PARAMS);$(EXTRA_PARAMS)" \
5361
--driver-class "$(DRIVER_CLASS)"
5462

5563
query: classes
56-
$(J) $(JFR_FLAGS) -cp .:$(DRIVER_JAR) JDBCClient --action query $(COMMON_FLAGS) --query "$(QUERY)" $(OUTPUT_FLAG) $(EXTRA_ARGS)
64+
$(J) $(JFR_FLAGS) -cp .:$(DRIVER_JAR) JDBCClient --action query $(COMMON_FLAGS) $(QUERY_FLAG) $(OUTPUT_FLAG) $(EXTRA_ARGS)
5765

5866
query-generated: classes
5967
$(J) $(JFR_FLAGS) -cp .:$(DRIVER_JAR) JDBCClient --action query $(COMMON_FLAGS) $(OUTPUT_FLAG) --generate-rows $(ROWS) --generate-cols $(COLS) $(EXTRA_ARGS)

java-bigquery-jdbc/tools/perf/Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# Defaults
44
ITERATIONS ?= 5
5+
QUERY ?=
6+
QUERY_FILE ?=
57
ROWS ?= 1000
68
COLS ?= 5
79
VERSION ?= $(shell sed -n 's/.*<version>\([^<]*\)<\/version>.*/\1/p' ../../pom.xml | head -n 1)
@@ -25,7 +27,18 @@ HTAPI_OPTS = EnableHighThroughputAPI=1;HighThroughputActivationRatio=0;HighThrou
2527
RUN_PERF = python3 run_perf.py
2628

2729
# Common flags for run_perf.py
28-
COMMON_FLAGS = -n $(ITERATIONS) --generate-rows $(ROWS) --generate-cols $(COLS) --jar1 $(JAR1) --class1 $(CLASS1)
30+
COMMON_FLAGS = -n $(ITERATIONS) --jar1 $(JAR1) --class1 $(CLASS1)
31+
32+
ifeq ($(QUERY)$(QUERY_FILE),)
33+
COMMON_FLAGS += --generate-rows $(ROWS) --generate-cols $(COLS)
34+
else
35+
ifneq ($(QUERY),)
36+
COMMON_FLAGS += --query "$(QUERY)"
37+
endif
38+
ifneq ($(QUERY_FILE),)
39+
COMMON_FLAGS += --query-file "$(QUERY_FILE)"
40+
endif
41+
endif
2942

3043
ifneq ($(JAR2),)
3144
COMMON_FLAGS += --jar2 $(JAR2) --class2 $(CLASS2)

java-bigquery-jdbc/tools/perf/README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,32 @@ The easiest way to run tests is using the provided `Makefile`. It defines target
2424
The Makefile uses the following defaults which can be overridden:
2525

2626
- `ITERATIONS`: 5
27-
- `ROWS`: 1000
28-
- `COLS`: 5
27+
- `ROWS`: 1000 (default, used if no query/query_file is specified)
28+
- `COLS`: 5 (default, used if no query/query_file is specified)
29+
- `QUERY`: Optional custom query to run
30+
- `QUERY_FILE`: Optional path to a SQL file containing the query to run
2931
- `JAR1`: `../../drivers/google-cloud-bigquery-jdbc-0.9.0-all.jar`
3032
- `PROJECT_ID`: `bigquery-devtools-drivers`
3133
- `CREDENTIALS`: Value of `$GOOGLE_APPLICATION_CREDENTIALS`
3234

3335
### Examples
3436

35-
#### Run REST API tests with defaults
37+
#### Run REST API tests with defaults (generates 1000 rows, 5 columns)
3638

3739
```bash
3840
make run-rest
3941
```
4042

41-
#### Run HTAPI tests with custom iterations and rows
43+
#### Run REST API tests with custom generated data size
4244

4345
```bash
44-
make run-htapi ITERATIONS=3 ROWS=50000
46+
make run-rest ROWS=50000 COLS=10
47+
```
48+
49+
#### Run HTAPI tests with custom iterations and query
50+
51+
```bash
52+
make run-htapi ITERATIONS=3 QUERY="SELECT * FROM my_dataset.my_table LIMIT 50000"
4553
```
4654

4755
#### Compare two drivers
@@ -64,14 +72,21 @@ For more control, you can run `run_perf.py` directly.
6472
- `--class1`: Class name for the first driver (default: `com.google.cloud.bigquery.jdbc.BigQueryDriver`).
6573
- `--class2`: Class name for the second driver (default: `com.google.cloud.bigquery.jdbc.BigQueryDriver`).
6674
- `-n`, `--iterations`: Number of iterations to run (default: 5).
67-
- `--generate-rows`: Number of rows to generate via query (default: 0).
68-
- `--generate-cols`: Number of columns to generate via query (default: 5).
69-
- `--query`: A specific query to run (if not using generated data).
75+
- `--query`: The query to run.
76+
- `--query-file`: Path to a SQL file containing the query to run.
77+
- `--generate-rows`: Number of rows to generate (default: 0, used if no query/query_file is specified).
78+
- `--generate-cols`: Number of columns to generate (default: 5).
7079
- `--output-md`: Append results as a markdown table to this file.
7180
- `--filter-metrics`: Comma-separated list of metrics to include in the markdown table.
7281

7382
### Examples
7483

84+
#### Run a single driver with a custom query
85+
86+
```bash
87+
python3 run_perf.py --url "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;ProjectId=my-project;OAuthType=3" --jar1 path/to/driver.jar --query "SELECT * FROM my_dataset.my_table LIMIT 1000" -n 3
88+
```
89+
7590
#### Run a single driver with generated data
7691

7792
```bash

java-bigquery-jdbc/tools/perf/run_perf.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# Base directory of the script
2222
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
2323

24-
def run_test(url, driver_jar, driver_class, query=None, generate_rows=0, generate_cols=5, no_output=True):
24+
def run_test(url, driver_jar, driver_class, query=None, query_file=None, generate_rows=0, generate_cols=5, no_output=True):
2525
# Base client folder is tools/client. Relative to tools/perf it is ../client.
2626
client_dir = os.path.join(os.path.dirname(BASE_DIR), "client")
2727

@@ -41,7 +41,9 @@ def run_test(url, driver_jar, driver_class, query=None, generate_rows=0, generat
4141

4242
if query:
4343
cmd.extend(["--query", query])
44-
if generate_rows > 0:
44+
elif query_file:
45+
cmd.extend(["--query-file", query_file])
46+
elif generate_rows > 0:
4547
cmd.extend(["--generate-rows", str(generate_rows)])
4648
cmd.extend(["--generate-cols", str(generate_cols)])
4749
if no_output:
@@ -227,26 +229,38 @@ def main():
227229
parser.add_argument("--class1", default="com.google.cloud.bigquery.jdbc.BigQueryDriver", help="Class name for first driver")
228230
parser.add_argument("--class2", default="com.google.cloud.bigquery.jdbc.BigQueryDriver", help="Class name for second driver")
229231
parser.add_argument("-n", "--iterations", type=int, default=5, help="Number of iterations to run (default 5)")
232+
parser.add_argument("--query", help="Query to run")
233+
parser.add_argument("--query-file", help="Path to a SQL file containing the query to run")
230234
parser.add_argument("--generate-rows", type=int, default=0, help="Number of rows to generate")
231235
parser.add_argument("--generate-cols", type=int, default=5, help="Number of columns to generate")
232-
parser.add_argument("--query", help="Query to run (if not using generated data)")
233236
parser.add_argument("--output-md", help="Append markdown table to this file containing the results")
234237
parser.add_argument("--filter-metrics", help="Comma-separated list of metrics to include in markdown tables")
235238

236239
args = parser.parse_args()
237240

241+
query = args.query
242+
query_file = args.query_file
243+
generate_rows = args.generate_rows
244+
generate_cols = args.generate_cols
245+
246+
if not query and not query_file and generate_rows == 0:
247+
generate_rows = 1000
248+
generate_cols = 5
249+
238250
print("=" * 70)
239251
print(f"JDBC Performance Runner")
240252
print(f"URL : {args.url}")
241253
print(f"Iterations : {args.iterations}")
242254
print(f"Jar 1 : {args.jar1} ({args.class1})")
243255
if args.jar2:
244256
print(f"Jar 2 : {args.jar2} ({args.class2})")
245-
if args.generate_rows > 0:
246-
print(f"Generate Rows: {args.generate_rows}")
247-
print(f"Generate Cols: {args.generate_cols}")
248-
elif args.query:
249-
print(f"Query : {args.query}")
257+
if query:
258+
print(f"Query : {query}")
259+
elif query_file:
260+
print(f"Query File : {query_file}")
261+
elif generate_rows > 0:
262+
print(f"Generate Rows: {generate_rows}")
263+
print(f"Generate Cols: {generate_cols}")
250264
print("=" * 70)
251265

252266
driver_results = {}
@@ -270,9 +284,10 @@ def main():
270284
url=args.url,
271285
driver_jar=driver_jar,
272286
driver_class=driver_class,
273-
query=args.query,
274-
generate_rows=args.generate_rows,
275-
generate_cols=args.generate_cols,
287+
query=query,
288+
query_file=query_file,
289+
generate_rows=generate_rows,
290+
generate_cols=generate_cols,
276291
no_output=True
277292
)
278293
if res:
@@ -284,7 +299,7 @@ def main():
284299
base_label=base_label,
285300
new_label=new_label,
286301
diff_label=diff_label,
287-
spec_name=f"Rows: {args.generate_rows}, Cols: {args.generate_cols}" if args.generate_rows > 0 else args.query,
302+
spec_name=query if query else (f"File: {query_file}" if query_file else f"Rows: {generate_rows}, Cols: {generate_cols}"),
288303
output_md=args.output_md,
289304
filter_metrics=args.filter_metrics
290305
)

0 commit comments

Comments
 (0)