You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by be...@apache.org on 2015/04/09 13:10:43 UTC
[1/2] cassandra git commit: follow up to CASSANDRA-8670,
cleaning up benchmark
Repository: cassandra
Updated Branches:
refs/heads/trunk cbb982cc1 -> 2f6d41633
follow up to CASSANDRA-8670, cleaning up benchmark
patch by ariel; reviewed by benedict for CASSANDRA-8670
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/2f6d4163
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/2f6d4163
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/2f6d4163
Branch: refs/heads/trunk
Commit: 2f6d416339864e336945474bbb44c90b60ea29bc
Parents: 5c55b7d
Author: Ariel Weisberg <ar...@datastax.com>
Authored: Thu Apr 9 12:09:46 2015 +0100
Committer: Benedict Elliott Smith <be...@apache.org>
Committed: Thu Apr 9 12:10:27 2015 +0100
----------------------------------------------------------------------
.../test/microbench/OutputStreamBench.java | 149 +++++++++++--------
.../io/util/BufferedDataOutputStreamTest.java | 9 +-
.../io/util/NIODataInputStreamTest.java | 21 ++-
3 files changed, 95 insertions(+), 84 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/2f6d4163/test/microbench/org/apache/cassandra/test/microbench/OutputStreamBench.java
----------------------------------------------------------------------
diff --git a/test/microbench/org/apache/cassandra/test/microbench/OutputStreamBench.java b/test/microbench/org/apache/cassandra/test/microbench/OutputStreamBench.java
index b8136f7..7a159a2 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;
- @Override
- public void write(byte b[]) throws IOException {
+ byte foo;
- }
+ int foo1;
- @Override
- public void write(byte b[], int a, int c) throws IOException {
+ long foo2;
- }
- });
+ double foo3;
- WrappedDataOutputStreamPlus streamA = new WrappedDataOutputStreamPlus(hole);
+ float foo4;
- BufferedDataOutputStreamPlus streamB = new BufferedDataOutputStreamPlus(new WritableByteChannel() {
+ short foo5;
- @Override
- public boolean isOpen()
- {
- // TODO Auto-generated method stub
- return true;
- }
+ char foo6;
- @Override
- public void close() throws IOException
- {
- // TODO Auto-generated method stub
- }
+ String tinyM = BufferedDataOutputStreamTest.fourByte;
+ String smallM;
+ String largeM;
+ String tiny = "a";
+ String small = "adsjglhnafsjk;gujfakyhgukafshgjkahfsgjkhafs;jkhausjkgaksfj;gafskdghajfsk;g";
+ String large;
- @Override
- public int write(ByteBuffer src) throws IOException
- {
- int remaining = src.remaining();
- src.position(src.limit());
- return remaining;
+ @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);
}
+ smallM = sb.toString();
+
+ sb = new StringBuilder();
+ while (sb.length() < 1024 * 12) {
+ sb.append(small);
+ }
+ large = sb.toString();
- }, 8192);
-
- public static byte foo;
-
- public static int foo1;
-
- public static long foo2;
-
- public static double foo3;
-
- public static float foo4;
-
- public static short foo5;
+ sb = new StringBuilder();
+ while (sb.length() < 1024 * 12) {
+ sb.append(smallM);
+ }
+ largeM = sb.toString();
- public static char foo6;
+ hole = new BufferedOutputStream(new OutputStream() {
+
+ @Override
+ public void write(int b) throws IOException
+ {
+ bh.consume(b);
+ }
+
+ @Override
+ public void write(byte b[]) throws IOException {
+ bh.consume(b);
+ }
+
+ @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);
+
+ streamB = new BufferedDataOutputStreamPlus(new WritableByteChannel() {
+
+ @Override
+ public boolean isOpen()
+ {
+ return true;
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ }
+
+ @Override
+ public int write(ByteBuffer src) throws IOException
+ {
+ bh.consume(src);
+ int remaining = src.remaining();
+ src.position(src.limit());
+ return remaining;
+ }
+
+ }, 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);
http://git-wip-us.apache.org/repos/asf/cassandra/blob/2f6d4163/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java b/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java
index f5b239c..d0819fe 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
http://git-wip-us.apache.org/repos/asf/cassandra/blob/2f6d4163/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java b/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java
index 4106036..a19346b 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
[2/2] cassandra git commit: follow up to CASSANDRA-8670,
further optimising writeUTF
Posted by be...@apache.org.
follow up to CASSANDRA-8670, further optimising writeUTF
patch by benedict and ariel for CASSANDRA-8670
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/5c55b7da
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/5c55b7da
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/5c55b7da
Branch: refs/heads/trunk
Commit: 5c55b7dafb8383e3bf5cfebac2c011cc72a5baa2
Parents: cbb982c
Author: Benedict Elliott Smith <be...@apache.org>
Authored: Thu Apr 9 12:06:43 2015 +0100
Committer: Benedict Elliott Smith <be...@apache.org>
Committed: Thu Apr 9 12:10:27 2015 +0100
----------------------------------------------------------------------
.../io/util/UnbufferedDataOutputStreamPlus.java | 49 +++++++++++++-------
1 file changed, 31 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cassandra/blob/5c55b7da/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java b/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
index 6e33e60..70435a1 100644
--- a/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
+++ b/src/java/org/apache/cassandra/io/util/UnbufferedDataOutputStreamPlus.java
@@ -253,15 +253,16 @@ public abstract class UnbufferedDataOutputStreamPlus extends DataOutputStreamPlu
{
int length = str.length();
int utfCount = 0;
+ int maxSize = 2;
for (int i = 0 ; i < length ; i++)
{
int ch = str.charAt(i);
- if ((ch > 0) && (ch <= 127))
+ if ((ch > 0) & (ch <= 127))
utfCount += 1;
else if (ch <= 2047)
utfCount += 2;
else
- utfCount += 3;
+ utfCount += maxSize = 3;
}
if (utfCount > 65535)
@@ -289,33 +290,45 @@ public abstract class UnbufferedDataOutputStreamPlus extends DataOutputStreamPlu
else
{
int utfIndex = 2;
+ int offset = 0;
utfBytes[0] = (byte) (utfCount >> 8);
utfBytes[1] = (byte) utfCount;
- for (int charIndex = 0 ; charIndex < length ; charIndex++)
+
+ while (length > 0)
{
- if (utfIndex + 3 > bufferLength)
+ int charRunLength = (utfBytes.length - utfIndex) / maxSize;
+ if (charRunLength < 128 && charRunLength < length)
{
out.write(utfBytes, 0, utfIndex);
utfIndex = 0;
}
+ if (charRunLength > length)
+ charRunLength = length;
- char ch = str.charAt(charIndex);
- if ((ch > 0) && (ch <= 127))
- {
- utfBytes[utfIndex++] = (byte) ch;
- }
- else if (ch <= 2047)
+ for (int i = 0 ; i < charRunLength ; i++)
{
- utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (ch >> 6)));
- utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & ch));
- }
- else
- {
- utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (ch >> 12)));
- utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (ch >> 6)));
- utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & ch));
+ char ch = str.charAt(offset + i);
+ if ((ch > 0) & (ch <= 127))
+ {
+ utfBytes[utfIndex++] = (byte) ch;
+ }
+ else if (ch <= 2047)
+ {
+ utfBytes[utfIndex++] = (byte) (0xc0 | (0x1f & (ch >> 6)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & ch));
+ }
+ else
+ {
+ utfBytes[utfIndex++] = (byte) (0xe0 | (0x0f & (ch >> 12)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & (ch >> 6)));
+ utfBytes[utfIndex++] = (byte) (0x80 | (0x3f & ch));
+ }
}
+
+ offset += charRunLength;
+ length -= charRunLength;
}
+
out.write(utfBytes, 0, utfIndex);
}
}