|
| 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 | +'use strict'; |
| 18 | + |
| 19 | +const async = require('async'); |
| 20 | +const fs = require('fs'); |
| 21 | +const BigQuery = require('../src/index.js'); |
| 22 | +const env = require('../../../system-test/env.js'); |
| 23 | + |
| 24 | +if (process.argv.length < 3) { |
| 25 | + throw new Error(`need query file; ` + |
| 26 | + `usage: '${process.argv[0]} ${process.argv[1]} <queries.json>'`); |
| 27 | +} |
| 28 | + |
| 29 | +var queryJson = fs.readFileSync(process.argv[2]); |
| 30 | +var queries = JSON.parse(queryJson); |
| 31 | +var client = new BigQuery(env); |
| 32 | + |
| 33 | +var doQuery = function(queryTxt, callback) { |
| 34 | + var startMilli = new Date().getTime(); |
| 35 | + var numRows = 0; |
| 36 | + var numCols; |
| 37 | + var timeFirstByteMilli; |
| 38 | + |
| 39 | + var query = { |
| 40 | + query: queryTxt, |
| 41 | + useLegacySql: false |
| 42 | + }; |
| 43 | + |
| 44 | + client |
| 45 | + .createQueryStream(query) |
| 46 | + .on('error', callback) |
| 47 | + .on('data', function(row) { |
| 48 | + if (numRows === 0) { |
| 49 | + numCols = Object.keys(row).length; |
| 50 | + timeFirstByteMilli = new Date().getTime() - startMilli; |
| 51 | + } else if (numCols !== Object.keys(row).length) { |
| 52 | + this.end(); |
| 53 | + |
| 54 | + var receivedCols = Object.keys(row).length; |
| 55 | + var error = new Error( |
| 56 | + `query "${queryTxt}": ` + |
| 57 | + `wrong number of columns, want ${numCols} got ${receivedCols}` |
| 58 | + ); |
| 59 | + |
| 60 | + callback(error); |
| 61 | + } |
| 62 | + numRows++; |
| 63 | + }) |
| 64 | + .on('end', function() { |
| 65 | + var timeTotalMilli = new Date().getTime() - startMilli; |
| 66 | + |
| 67 | + console.log( |
| 68 | + `query ${queryTxt}:`, |
| 69 | + `got ${numRows} rows,`, |
| 70 | + `${numCols} cols,`, |
| 71 | + `first byte ${timeFirstByteMilli / 1000} sec,`, |
| 72 | + `total ${timeTotalMilli / 1000} sec` |
| 73 | + ); |
| 74 | + |
| 75 | + callback(null); |
| 76 | + }); |
| 77 | +}; |
| 78 | + |
| 79 | +async.eachSeries(queries, doQuery, err => { |
| 80 | + if (err) { |
| 81 | + console.error(err); |
| 82 | + } |
| 83 | +}); |
0 commit comments