BigQueryRunner { public void runQuery() throws InterruptedException { QueryJobConfiguration queryConfig = QueryJobConfiguration.newBuilder( "SELECT " + "CONCAT('https: + "view_count " + "FROM `bigquery-public-data.stackoverflow.posts_questions` " + "WHERE tags like '%google-bigquery%' " + "ORDER BY favorite_count DESC LIMIT 10") .setUseLegacySql(false) .build(); List<TimeSeries> timeSeriesList = new ArrayList<>(); long queryStartTime = System.currentTimeMillis(); JobId jobId = JobId.of(UUID.randomUUID().toString()); Job queryJob = bigquery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build()); queryJob = queryJob.waitFor(); if (queryJob == null) { throw new RuntimeException("Job no longer exists"); } else if (queryJob.getStatus().getError() != null) { throw new RuntimeException(queryJob.getStatus().getError().toString()); } TableResult result = queryJob.getQueryResults(); long queryEndTime = System.currentTimeMillis(); timeSeriesList.add(prepareMetric(QUERY_DURATION_METRIC, queryEndTime - queryStartTime)); timeSeriesList.add(prepareMetric(ROWS_RETURNED_METRIC, result.getTotalRows())); CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder() .setName(projectName) .addAllTimeSeries(timeSeriesList) .build(); createMetricsIfNeeded(); client.createTimeSeries(request); os.println("Done writing metrics."); mostRecentRunResult = result; } private  BigQueryRunner();   BigQueryRunner(MetricServiceClient metricsClient, BigQuery bigquery, PrintStream os); static synchronized BigQueryRunner getInstance(); static TableResult getMostRecentRunResult(); void runQuery(); List<TimeSeriesSummary> getTimeSeriesValues();  }
@Test public void testRun() throws Exception { app.runQuery(); String got = bout.toString(); assertThat(got).contains("Done writing metrics."); verify(metricsServiceStub).listMetricDescriptorsPagedCallable(); verify(metricsServiceStub, times(2)).createMetricDescriptorCallable(); verify(metricsServiceStub).createTimeSeriesCallable(); verify(createTimeSeriesCallable).call(createTimeSeriesRequest.capture()); CreateTimeSeriesRequest actual = createTimeSeriesRequest.getValue(); assertEquals(2, actual.getTimeSeriesCount()); assertThat(actual.getTimeSeries(0).getMetric().getType()).isEqualTo( "custom.googleapis.com/queryDuration"); assertThat(actual.getTimeSeries(0).getPoints(0).getValue().getInt64Value()).isGreaterThan(0L); assertThat(actual.getTimeSeries(1).getMetric().getType()).isEqualTo( "custom.googleapis.com/rowsReturned"); assertThat(actual.getTimeSeries(1).getPoints(0).getValue().getInt64Value()).isGreaterThan(0L); }