beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bchamb...@apache.org
Subject [2/2] incubator-beam git commit: Add ElementByteSizeObserver default constructor
Date Tue, 02 Aug 2016 17:55:17 GMT
Add ElementByteSizeObserver default constructor

This enables constructing an observer without the Counter to simplify
testing and use with other ways of reporting byte size.

Once compatibility issues are resolved, the class will become abstract
with the Counter-specific functionality removed.


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/aae1b010
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/aae1b010
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/aae1b010

Branch: refs/heads/master
Commit: aae1b010e9da59dbd68ce24401f15561440f2bee
Parents: f13725f
Author: bchambers <bchambers@google.com>
Authored: Mon Aug 1 10:37:14 2016 -0700
Committer: bchambers <bchambers@google.com>
Committed: Tue Aug 2 09:55:39 2016 -0700

----------------------------------------------------------------------
 .../beam/sdk/testing/CoderProperties.java       | 65 +++++++++++++++++++-
 .../util/common/ElementByteSizeObserver.java    | 18 +++++-
 .../beam/sdk/coders/BigDecimalCoderTest.java    | 15 +----
 .../beam/sdk/coders/BigIntegerCoderTest.java    | 14 +----
 .../beam/sdk/coders/ByteArrayCoderTest.java     |  5 +-
 .../beam/sdk/util/common/CounterTestUtils.java  | 57 -----------------
 6 files changed, 88 insertions(+), 86 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/aae1b010/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
index c1fc856..e56f01f 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/testing/CoderProperties.java
@@ -36,11 +36,13 @@ import org.apache.beam.sdk.util.Serializer;
 import org.apache.beam.sdk.util.Structs;
 import org.apache.beam.sdk.util.UnownedInputStream;
 import org.apache.beam.sdk.util.UnownedOutputStream;
-
+import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Iterables;
+import com.google.common.io.ByteStreams;
 import com.google.common.io.CountingInputStream;
+import com.google.common.io.CountingOutputStream;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -362,4 +364,65 @@ public class CoderProperties {
       throws CoderException, IOException {
     return decode(coder, context, encode(coder, context, value));
   }
+
+  /**
+   * A utility method that passes the given (unencoded) elements through
+   * coder's registerByteSizeObserver() and encode() methods, and confirms
+   * they are mutually consistent. This is useful for testing coder
+   * implementations.
+   */
+  public static <T> void testByteCount(Coder<T> coder, Coder.Context context,
T[] elements)
+      throws Exception {
+    TestElementByteSizeObserver observer = new TestElementByteSizeObserver();
+
+    CountingOutputStream os = new CountingOutputStream(ByteStreams.nullOutputStream());
+    for (T elem : elements) {
+      coder.registerByteSizeObserver(elem, observer, context);
+      coder.encode(elem, os, context);
+      observer.advance();
+    }
+    long expectedLength = os.getCount();
+
+    assertEquals(expectedLength, observer.getSum());
+    assertEquals(elements.length, observer.getCount());
+  }
+
+  /**
+   * An {@link ElementByteSizeObserver} that records the observed element sizes for testing
+   * purposes.
+   */
+  public static class TestElementByteSizeObserver extends ElementByteSizeObserver {
+
+    private long currentSum = 0;
+    private long count = 0;
+
+    @Override
+    protected void reportElementSize(long elementByteSize) {
+      count++;
+      currentSum += elementByteSize;
+    }
+
+    public double getMean() {
+      return ((double) currentSum) / count;
+    }
+
+    public long getSum() {
+      return currentSum;
+    }
+
+    public long getCount() {
+      return count;
+    }
+
+    public void reset() {
+      currentSum = 0;
+      count = 0;
+    }
+
+    public long getSumAndReset() {
+      long returnValue = currentSum;
+      reset();
+      return returnValue;
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/aae1b010/sdks/java/core/src/main/java/org/apache/beam/sdk/util/common/ElementByteSizeObserver.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/common/ElementByteSizeObserver.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/common/ElementByteSizeObserver.java
index 55d8b07..53dd4b9 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/common/ElementByteSizeObserver.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/common/ElementByteSizeObserver.java
@@ -19,6 +19,7 @@ package org.apache.beam.sdk.util.common;
 
 import java.util.Observable;
 import java.util.Observer;
+import javax.annotation.Nullable;
 
 /**
  * An observer that gets notified when additional bytes are read
@@ -27,16 +28,30 @@ import java.util.Observer;
  * count to the specified counter, and prepares for the next element.
  */
 public class ElementByteSizeObserver implements Observer {
+  @Nullable
   private final Counter<Long> counter;
   private boolean isLazy = false;
   private long totalSize = 0;
   private double scalingFactor = 1.0;
 
+  public ElementByteSizeObserver() {
+    this.counter = null;
+  }
+
   public ElementByteSizeObserver(Counter<Long> counter) {
     this.counter = counter;
   }
 
   /**
+   * Called to report element byte size.
+   */
+  protected void reportElementSize(long elementByteSize) {
+    if (counter != null) {
+      counter.addValue(elementByteSize);
+    }
+  };
+
+  /**
    * Sets byte counting for the current element as lazy. That is, the
    * observer will get notified of the element's byte count only as
    * element's pieces are being processed or iterated over.
@@ -85,8 +100,7 @@ public class ElementByteSizeObserver implements Observer {
    * size to the counter, and prepares the observer for the next element.
    */
   public void advance() {
-    counter.addValue(totalSize);
-
+    reportElementSize(totalSize);
     totalSize = 0;
     isLazy = false;
   }

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/aae1b010/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigDecimalCoderTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigDecimalCoderTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigDecimalCoderTest.java
index 033f076..a5e3bc2 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigDecimalCoderTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigDecimalCoderTest.java
@@ -21,20 +21,14 @@ import static org.hamcrest.Matchers.equalTo;
 import static org.junit.Assert.assertThat;
 
 import org.apache.beam.sdk.testing.CoderProperties;
+import org.apache.beam.sdk.testing.CoderProperties.TestElementByteSizeObserver;
 import org.apache.beam.sdk.util.CoderUtils;
-import org.apache.beam.sdk.util.common.Counter;
-import org.apache.beam.sdk.util.common.Counter.AggregationKind;
-import org.apache.beam.sdk.util.common.CounterName;
-import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
-
 import com.google.common.collect.ImmutableList;
-
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
-
 import java.math.BigDecimal;
 import java.util.List;
 
@@ -112,15 +106,12 @@ public class BigDecimalCoderTest {
 
   @Test
   public void testGetEncodedElementByteSize() throws Exception {
-    Counter<Long> counter = Counter.longs(CounterName.named("dummy"), AggregationKind.SUM);
-    ElementByteSizeObserver observer = new ElementByteSizeObserver(counter);
+    TestElementByteSizeObserver observer = new TestElementByteSizeObserver();
     for (BigDecimal value : TEST_VALUES) {
       TEST_CODER.registerByteSizeObserver(value, observer, Coder.Context.OUTER);
       observer.advance();
-      assertThat(
-          counter.getAggregate(),
+      assertThat(observer.getSumAndReset(),
           equalTo((long) CoderUtils.encodeToByteArray(TEST_CODER, value).length));
-      counter.resetToValue(0L);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/aae1b010/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigIntegerCoderTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigIntegerCoderTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigIntegerCoderTest.java
index 3934b03..d042378 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigIntegerCoderTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/BigIntegerCoderTest.java
@@ -21,20 +21,14 @@ import static org.hamcrest.Matchers.equalTo;
 import static org.junit.Assert.assertThat;
 
 import org.apache.beam.sdk.testing.CoderProperties;
+import org.apache.beam.sdk.testing.CoderProperties.TestElementByteSizeObserver;
 import org.apache.beam.sdk.util.CoderUtils;
-import org.apache.beam.sdk.util.common.Counter;
-import org.apache.beam.sdk.util.common.Counter.AggregationKind;
-import org.apache.beam.sdk.util.common.CounterName;
-import org.apache.beam.sdk.util.common.ElementByteSizeObserver;
-
 import com.google.common.collect.ImmutableList;
-
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
-
 import java.math.BigInteger;
 import java.util.List;
 
@@ -87,15 +81,13 @@ public class BigIntegerCoderTest {
 
   @Test
   public void testGetEncodedElementByteSize() throws Exception {
-    Counter<Long> counter = Counter.longs(CounterName.named("dummy"), AggregationKind.SUM);
-    ElementByteSizeObserver observer = new ElementByteSizeObserver(counter);
+    TestElementByteSizeObserver observer = new TestElementByteSizeObserver();
     for (BigInteger value : TEST_VALUES) {
       TEST_CODER.registerByteSizeObserver(value, observer, Coder.Context.OUTER);
       observer.advance();
       assertThat(
-          counter.getAggregate(),
+          observer.getSumAndReset(),
           equalTo((long) CoderUtils.encodeToByteArray(TEST_CODER, value).length));
-      counter.resetToValue(0L);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/aae1b010/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/ByteArrayCoderTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/ByteArrayCoderTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/ByteArrayCoderTest.java
index 1215940..aca0a83 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/ByteArrayCoderTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/coders/ByteArrayCoderTest.java
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertThat;
 
 import org.apache.beam.sdk.testing.CoderProperties;
 import org.apache.beam.sdk.util.CoderUtils;
-import org.apache.beam.sdk.util.common.CounterTestUtils;
 
 import org.junit.Rule;
 import org.junit.Test;
@@ -59,10 +58,10 @@ public class ByteArrayCoderTest {
 
   @Test
   public void testRegisterByteSizeObserver() throws Exception {
-    CounterTestUtils.testByteCount(ByteArrayCoder.of(), Coder.Context.OUTER,
+    CoderProperties.testByteCount(ByteArrayCoder.of(), Coder.Context.OUTER,
                                    new byte[][]{{ 0xa, 0xb, 0xc }});
 
-    CounterTestUtils.testByteCount(ByteArrayCoder.of(), Coder.Context.NESTED,
+    CoderProperties.testByteCount(ByteArrayCoder.of(), Coder.Context.NESTED,
                                    new byte[][]{{ 0xa, 0xb, 0xc }, {}, {}, { 0xd, 0xe },
{}});
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/aae1b010/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/CounterTestUtils.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/CounterTestUtils.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/CounterTestUtils.java
deleted file mode 100644
index 7ed07a1..0000000
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/util/common/CounterTestUtils.java
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * 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.util.common;
-
-import static org.apache.beam.sdk.util.common.Counter.AggregationKind.MEAN;
-
-import org.apache.beam.sdk.coders.Coder;
-import org.apache.beam.sdk.util.common.Counter.CounterMean;
-
-import org.junit.Assert;
-
-import java.io.ByteArrayOutputStream;
-
-/**
- * Utilities for testing {@link Counter}s.
- */
-public class CounterTestUtils {
-  /**
-   * A utility method that passes the given (unencoded) elements through
-   * coder's registerByteSizeObserver() and encode() methods, and confirms
-   * they are mutually consistent. This is useful for testing coder
-   * implementations.
-   */
-  public static <T> void testByteCount(Coder<T> coder, Coder.Context context,
T[] elements)
-      throws Exception {
-    Counter<Long> meanByteCount = Counter.longs("meanByteCount", MEAN);
-    ElementByteSizeObserver observer = new ElementByteSizeObserver(meanByteCount);
-
-    ByteArrayOutputStream os = new ByteArrayOutputStream();
-    for (T elem : elements) {
-      coder.registerByteSizeObserver(elem, observer, context);
-      coder.encode(elem, os, context);
-      observer.advance();
-    }
-    long expectedLength = os.toByteArray().length;
-
-    CounterMean<Long> mean = meanByteCount.getMean();
-
-    Assert.assertEquals(expectedLength, mean.getAggregate().longValue());
-    Assert.assertEquals(elements.length, mean.getCount());
-  }
-}


Mime
View raw message