From 2f6d416339864e336945474bbb44c90b60ea29bc Mon Sep 17 00:00:00 2001 From: Ariel Weisberg Date: Thu, 9 Apr 2015 12:09:46 +0100 Subject: [PATCH] follow up to CASSANDRA-8670, cleaning up benchmark patch by ariel; reviewed by benedict for CASSANDRA-8670 --- .../test/microbench/OutputStreamBench.java | 147 ++++++++++-------- .../io/util/BufferedDataOutputStreamTest.java | 9 +- .../io/util/NIODataInputStreamTest.java | 21 ++- 3 files changed, 94 insertions(+), 83 deletions(-) diff --git a/test/microbench/org/apache/cassandra/test/microbench/OutputStreamBench.java b/test/microbench/org/apache/cassandra/test/microbench/OutputStreamBench.java index b8136f7666..7a159a20fd 100644 --- a/test/microbench/org/apache/cassandra/test/microbench/OutputStreamBench.java +++ b/test/microbench/org/apache/cassandra/test/microbench/OutputStreamBench.java @@ -22,6 +22,7 @@ import org.apache.cassandra.io.util.BufferedDataOutputStreamPlus; import org.apache.cassandra.io.util.BufferedDataOutputStreamTest; import org.apache.cassandra.io.util.WrappedDataOutputStreamPlus; import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; import java.io.BufferedOutputStream; import java.io.IOException; @@ -40,66 +41,103 @@ import java.util.concurrent.TimeUnit; public class OutputStreamBench { - BufferedOutputStream hole = new BufferedOutputStream(new OutputStream() { + BufferedOutputStream hole; - @Override - public void write(int b) throws IOException - { + WrappedDataOutputStreamPlus streamA; + BufferedDataOutputStreamPlus streamB; + + byte foo; + + int foo1; + + long foo2; + + double foo3; + + float foo4; + + short foo5; + + char foo6; + + + String tinyM = BufferedDataOutputStreamTest.fourByte; + String smallM; + String largeM; + String tiny = "a"; + String small = "adsjglhnafsjk;gujfakyhgukafshgjkahfsgjkhafs;jkhausjkgaksfj;gafskdghajfsk;g"; + String large; + + @Setup + public void setUp(final Blackhole bh) { + StringBuilder sb = new StringBuilder(); + for (int ii = 0; ii < 11; ii++) { + sb.append(BufferedDataOutputStreamTest.fourByte); + sb.append(BufferedDataOutputStreamTest.threeByte); + sb.append(BufferedDataOutputStreamTest.twoByte); } - - @Override - public void write(byte b[]) throws IOException { - + smallM = sb.toString(); + + sb = new StringBuilder(); + while (sb.length() < 1024 * 12) { + sb.append(small); } + large = sb.toString(); - @Override - public void write(byte b[], int a, int c) throws IOException { - + sb = new StringBuilder(); + while (sb.length() < 1024 * 12) { + sb.append(smallM); } - }); + largeM = sb.toString(); - WrappedDataOutputStreamPlus streamA = new WrappedDataOutputStreamPlus(hole); + hole = new BufferedOutputStream(new OutputStream() { - BufferedDataOutputStreamPlus streamB = new BufferedDataOutputStreamPlus(new WritableByteChannel() { + @Override + public void write(int b) throws IOException + { + bh.consume(b); + } - @Override - public boolean isOpen() - { - // TODO Auto-generated method stub - return true; - } + @Override + public void write(byte b[]) throws IOException { + bh.consume(b); + } - @Override - public void close() throws IOException - { - // TODO Auto-generated method stub + @Override + public void write(byte b[], int a, int c) throws IOException { + bh.consume(b); + bh.consume(a); + bh.consume(c); + } + }); - } + streamA = new WrappedDataOutputStreamPlus(hole); - @Override - public int write(ByteBuffer src) throws IOException - { - int remaining = src.remaining(); - src.position(src.limit()); - return remaining; - } + streamB = new BufferedDataOutputStreamPlus(new WritableByteChannel() { - }, 8192); + @Override + public boolean isOpen() + { + return true; + } - public static byte foo; + @Override + public void close() throws IOException + { + } - public static int foo1; + @Override + public int write(ByteBuffer src) throws IOException + { + bh.consume(src); + int remaining = src.remaining(); + src.position(src.limit()); + return remaining; + } - public static long foo2; - - public static double foo3; - - public static float foo4; - - public static short foo5; - - public static char foo6; + }, 8192); + } @Benchmark public void testBOSByte() throws IOException @@ -161,27 +199,6 @@ public class OutputStreamBench streamB.writeChar(foo6); } - public static String tinyM = "𠝹"; - public static String smallM = "𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ𠝹㒨ƀ"; - public static String largeM; - public static String tiny = "a"; - public static String small = "adsjglhnafsjk;gujfakyhgukafshgjkahfsgjkhafs;jkhausjkgaksfj;gafskdghajfsk;g"; - public static String large; - - static { - StringBuilder sb = new StringBuilder(); - while (sb.length() < 1024 * 12) { - sb.append(small); - } - large = sb.toString(); - - sb = new StringBuilder(); - while (sb.length() < 1024 * 12) { - sb.append(smallM); - } - largeM = sb.toString(); - } - @Benchmark public void testMTinyStringBOS() throws IOException { streamA.writeUTF(tinyM); diff --git a/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java b/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java index f5b239cf34..d0819fea58 100644 --- a/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java +++ b/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java @@ -1,7 +1,6 @@ package org.apache.cassandra.io.util; import java.io.ByteArrayOutputStream; -import java.io.DataOutput; import java.io.IOException; import java.io.OutputStream; import java.io.UTFDataFormatException; @@ -13,8 +12,6 @@ import java.util.Random; import org.junit.Test; -import com.google.common.base.Throwables; - import static org.junit.Assert.*; public class BufferedDataOutputStreamTest @@ -175,9 +172,9 @@ public class BufferedDataOutputStreamTest } String simple = "foobar42"; - String twoByte = "ƀ"; - String threeByte = "㒨"; - String fourByte = "𠝹"; + public static final String twoByte = "\u0180"; + public static final String threeByte = "\u34A8"; + public static final String fourByte = "\uD841\uDF79"; @SuppressWarnings("unused") private void fuzzOnce() throws Exception diff --git a/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java b/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java index 410603617b..a19346b58d 100644 --- a/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java +++ b/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java @@ -234,18 +234,15 @@ public class NIODataInputStreamTest DataOutputStream daos = new DataOutputStream(baos); String simple = "foobar42"; - String twoByte = "ƀ"; - String threeByte = "㒨"; - String fourByte = "𠝹"; - assertEquals(2, twoByte.getBytes(Charsets.UTF_8).length); - assertEquals(3, threeByte.getBytes(Charsets.UTF_8).length); - assertEquals(4, fourByte.getBytes(Charsets.UTF_8).length); + assertEquals(2, BufferedDataOutputStreamTest.twoByte.getBytes(Charsets.UTF_8).length); + assertEquals(3, BufferedDataOutputStreamTest.threeByte.getBytes(Charsets.UTF_8).length); + assertEquals(4, BufferedDataOutputStreamTest.fourByte.getBytes(Charsets.UTF_8).length); daos.writeUTF(simple); - daos.writeUTF(twoByte); - daos.writeUTF(threeByte); - daos.writeUTF(fourByte); + daos.writeUTF(BufferedDataOutputStreamTest.twoByte); + daos.writeUTF(BufferedDataOutputStreamTest.threeByte); + daos.writeUTF(BufferedDataOutputStreamTest.fourByte); NIODataInputStream is = new NIODataInputStream(new ReadableByteChannel() { @@ -266,9 +263,9 @@ public class NIODataInputStreamTest }, 4096); assertEquals(simple, is.readUTF()); - assertEquals(twoByte, is.readUTF()); - assertEquals(threeByte, is.readUTF()); - assertEquals(fourByte, is.readUTF()); + assertEquals(BufferedDataOutputStreamTest.twoByte, is.readUTF()); + assertEquals(BufferedDataOutputStreamTest.threeByte, is.readUTF()); + assertEquals(BufferedDataOutputStreamTest.fourByte, is.readUTF()); } @Test