You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

86 lines
1.8 KiB

  1. // Copyright 2017 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //+build ignore
  15. package main
  16. import (
  17. "context"
  18. "encoding/json"
  19. "flag"
  20. "io/ioutil"
  21. "log"
  22. "time"
  23. "cloud.google.com/go/bigquery"
  24. "google.golang.org/api/iterator"
  25. )
  26. func main() {
  27. flag.Parse()
  28. ctx := context.Background()
  29. c, err := bigquery.NewClient(ctx, flag.Arg(0))
  30. if err != nil {
  31. log.Fatal(err)
  32. }
  33. queriesJSON, err := ioutil.ReadFile(flag.Arg(1))
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. var queries []string
  38. if err := json.Unmarshal(queriesJSON, &queries); err != nil {
  39. log.Fatal(err)
  40. }
  41. for _, q := range queries {
  42. doQuery(ctx, c, q)
  43. }
  44. }
  45. func doQuery(ctx context.Context, c *bigquery.Client, qt string) {
  46. startTime := time.Now()
  47. q := c.Query(qt)
  48. it, err := q.Read(ctx)
  49. if err != nil {
  50. log.Fatal(err)
  51. }
  52. numRows, numCols := 0, 0
  53. var firstByte time.Duration
  54. for {
  55. var values []bigquery.Value
  56. err := it.Next(&values)
  57. if err == iterator.Done {
  58. break
  59. }
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. if numRows == 0 {
  64. numCols = len(values)
  65. firstByte = time.Since(startTime)
  66. } else if numCols != len(values) {
  67. log.Fatalf("got %d columns, want %d", len(values), numCols)
  68. }
  69. numRows++
  70. }
  71. log.Printf("query %q: %d rows, %d cols, first byte %f sec, total %f sec",
  72. qt, numRows, numCols, firstByte.Seconds(), time.Since(startTime).Seconds())
  73. }