[
https://issues.apache.org/jira/browse/BEAM-5716?focusedWorklogId=159117&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-159117
]
ASF GitHub Bot logged work on BEAM-5716:
----------------------------------------
Author: ASF GitHub Bot
Created on: 26/Oct/18 10:38
Start Date: 26/Oct/18 10:38
Worklog Time Spent: 10m
Work Description: lgajowy commented on a change in pull request #6725:
[BEAM-5716] Reorganize testing modules
URL: https://github.com/apache/beam/pull/6725#discussion_r228481467
##########
File path:
sdks/java/testing/test-utils/src/main/java/org/apache/beam/sdk/testutils/metrics/MetricsReader.java
##########
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.testutils.metrics;
+
+import java.util.NoSuchElementException;
+import org.apache.beam.sdk.PipelineResult;
+import org.apache.beam.sdk.metrics.DistributionResult;
+import org.apache.beam.sdk.metrics.MetricNameFilter;
+import org.apache.beam.sdk.metrics.MetricQueryResults;
+import org.apache.beam.sdk.metrics.MetricResult;
+import org.apache.beam.sdk.metrics.MetricsFilter;
+import org.joda.time.Duration;
+import org.slf4j.LoggerFactory;
+
+/** Provides methods for querying metrics from {@link PipelineResult} per
namespace. */
+public class MetricsReader {
+
+ private static final org.slf4j.Logger LOG =
LoggerFactory.getLogger(MetricsReader.class);
+
+ private enum DistributionType {
+ MIN,
+ MAX
+ }
+
+ private final PipelineResult result;
+
+ private final String namespace;
+
+ public MetricsReader(PipelineResult result, String namespace) {
+ this.result = result;
+ this.namespace = namespace;
+ }
+
+ /**
+ * Return the current value for a long counter, or a default value if can't
be retrieved. Note
+ * this uses only attempted metrics because some runners don't support
committed metrics.
+ */
+ public long getCounterMetric(String name, long defaultValue) {
+ MetricQueryResults metrics =
+ result
+ .metrics()
+ .queryMetrics(
+ MetricsFilter.builder()
+ .addNameFilter(MetricNameFilter.named(namespace, name))
+ .build());
+ Iterable<MetricResult<Long>> counters = metrics.getCounters();
+ try {
+ MetricResult<Long> metricResult = counters.iterator().next();
+ return metricResult.getAttempted();
+ } catch (NoSuchElementException e) {
+ LOG.error("Failed to get metric {}, from namespace {}", name, namespace);
+ }
+ return defaultValue;
+ }
+
+ /**
+ * Return start time metric by counting the difference between "now" and min
value from a
+ * distribution metric.
+ */
+ public long getStartTimeMetric(long now, String name) {
+ return this.getTimestampMetric(now, this.getDistributionMetric(name,
DistributionType.MIN, -1));
+ }
+
+ /**
+ * Return end time metric by counting the difference between "now" and MAX
value from a
+ * distribution metric.
+ */
+ public long getEndTimeMetric(long now, String name) {
+ return this.getTimestampMetric(now, this.getDistributionMetric(name,
DistributionType.MAX, -1));
+ }
+
+ /**
+ * Return the current value for a long counter, or a default value if can't
be retrieved. Note
+ * this uses only attempted metrics because some runners don't support
committed metrics.
+ */
+ private long getDistributionMetric(String name, DistributionType distType,
long defaultValue) {
+ MetricQueryResults metrics =
+ result
+ .metrics()
+ .queryMetrics(
+ MetricsFilter.builder()
+ .addNameFilter(MetricNameFilter.named(namespace, name))
+ .build());
+ Iterable<MetricResult<DistributionResult>> distributions =
metrics.getDistributions();
+ try {
+ MetricResult<DistributionResult> distributionResult =
distributions.iterator().next();
Review comment:
done
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 159117)
Time Spent: 2.5h (was: 2h 20m)
> Move testing utilities to a common place
> ----------------------------------------
>
> Key: BEAM-5716
> URL: https://issues.apache.org/jira/browse/BEAM-5716
> Project: Beam
> Issue Type: Task
> Components: testing
> Reporter: Lukasz Gajowy
> Assignee: Lukasz Gajowy
> Priority: Major
> Time Spent: 2.5h
> Remaining Estimate: 0h
>
> This was raised on the devlist here:
> [https://lists.apache.org/thread.html/19412a35c06d41b378c7e0116557b6eccbd7c289d86998e8ab3fadcf@%3Cdev.beam.apache.org%3E]
> Short version: there are some testing utilities from nexmark module that
> could be reused in load test (and possibly elsewhere). Other than that, both
> Nexmark and Load-tests should be stored in a common module together with the
> utils, as they all serve similar causes (testing).
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)