|
| 1 | +/*! |
| 2 | + * Copyright 2014 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 | +const util = require('util'); |
| 18 | +const fs = require('fs'); |
| 19 | +const BigQuery = require('../src/index.js'); |
| 20 | + |
| 21 | +if (process.argv.length < 3) { |
| 22 | + throw util.format("need query file; usage: '%s %s <queries.json>'", process.argv[0], process.argv[1]); |
| 23 | +} |
| 24 | +var queryJson = fs.readFileSync(process.argv[2]); |
| 25 | +var queries = JSON.parse(queryJson); |
| 26 | +var client = BigQuery(); |
| 27 | + |
| 28 | +var doQuery = function(queryTxt) { |
| 29 | + var startMilli = new Date().getTime(); |
| 30 | + var numRows = 0; |
| 31 | + var numCols; |
| 32 | + var timeFirstByteMilli; |
| 33 | + |
| 34 | + var query = { |
| 35 | + query: queryTxt, |
| 36 | + useLegacySql: false |
| 37 | + }; |
| 38 | + |
| 39 | + client.createQueryStream(query) |
| 40 | + .on('error', function(err) { |
| 41 | + throw err; |
| 42 | + }) |
| 43 | + .on('data', function(row) { |
| 44 | + if (numRows == 0) { |
| 45 | + numCols = Object.keys(row).length; |
| 46 | + timeFirstByteMilli = new Date().getTime() - startMilli; |
| 47 | + } else if (numCols != Object.keys(row).length) { |
| 48 | + throw util.format("query %j: wrong number of columns, want %d got %d", |
| 49 | + queryTxt, |
| 50 | + numCols, |
| 51 | + Object.keys(row).length); |
| 52 | + } |
| 53 | + numRows++; |
| 54 | + }) |
| 55 | + .on('end', function() { |
| 56 | + timeTotalMilli = new Date().getTime() - startMilli; |
| 57 | + console.log(util.format("query %j: got %d rows, %d cols, first byte %d sec, total %d sec", |
| 58 | + queryTxt, |
| 59 | + numRows, |
| 60 | + numCols, |
| 61 | + timeFirstByteMilli/1000, |
| 62 | + timeTotalMilli/1000)); |
| 63 | + }); |
| 64 | +} |
| 65 | + |
| 66 | +for (queryTxt of queries) { |
| 67 | + doQuery(queryTxt); |
| 68 | +} |
0 commit comments