|
| 1 | +/* |
| 2 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigquery.benchmark; |
| 18 | + |
| 19 | +import com.google.api.client.json.jackson.JacksonFactory; |
| 20 | +import com.google.cloud.bigquery.BigQuery; |
| 21 | +import com.google.cloud.bigquery.BigQueryOptions; |
| 22 | +import com.google.cloud.bigquery.FieldValue; |
| 23 | +import com.google.cloud.bigquery.QueryJobConfiguration; |
| 24 | +import com.google.cloud.bigquery.QueryResponse; |
| 25 | +import java.io.FileInputStream; |
| 26 | +import java.util.List; |
| 27 | +import org.threeten.bp.Clock; |
| 28 | +import org.threeten.bp.Duration; |
| 29 | +import org.threeten.bp.Instant; |
| 30 | + |
| 31 | +public class Benchmark { |
| 32 | + |
| 33 | + private static final double NS_PER_SECOND = 1000 * 1000 * 1000; |
| 34 | + |
| 35 | + private Benchmark() {} |
| 36 | + |
| 37 | + public static void main(String[] args) throws Exception { |
| 38 | + if (args.length < 1) { |
| 39 | + System.out.println("need path to queries.json"); |
| 40 | + return; |
| 41 | + } |
| 42 | + String[] requests = |
| 43 | + new JacksonFactory() |
| 44 | + .createJsonParser(new FileInputStream(args[0])) |
| 45 | + .parseAndClose(String[].class); |
| 46 | + |
| 47 | + Clock clock = Clock.systemUTC(); |
| 48 | + BigQuery bq = BigQueryOptions.getDefaultInstance().getService(); |
| 49 | + |
| 50 | + for (String request : requests) { |
| 51 | + if (request.isEmpty()) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + Instant start = clock.instant(); |
| 56 | + QueryResponse queryResponse = |
| 57 | + bq.query(QueryJobConfiguration.newBuilder(request).setUseLegacySql(false).build()); |
| 58 | + |
| 59 | + int rows = 0; |
| 60 | + int cols = 0; |
| 61 | + Duration firstByte = null; |
| 62 | + for (List<FieldValue> row : queryResponse.getResult().iterateAll()) { |
| 63 | + rows++; |
| 64 | + if (cols == 0) { |
| 65 | + cols = row.size(); |
| 66 | + firstByte = Duration.between(start, clock.instant()); |
| 67 | + } else if (cols != row.size()) { |
| 68 | + throw new IllegalStateException( |
| 69 | + String.format("expected %d cols, found %d", cols, row.size())); |
| 70 | + } |
| 71 | + } |
| 72 | + Duration total = Duration.between(start, clock.instant()); |
| 73 | + |
| 74 | + double firstByteSec = (double) (firstByte.getNano()) / NS_PER_SECOND + firstByte.getSeconds(); |
| 75 | + double totalSec = (double) (total.getNano()) / NS_PER_SECOND + total.getSeconds(); |
| 76 | + |
| 77 | + System.out.println( |
| 78 | + String.format( |
| 79 | + "query \"%s\": read %d rows, %d cols, first byte %f sec, total %f sec", |
| 80 | + request, rows, cols, firstByteSec, totalSec)); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments