Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'jetty-util/src/test/java')
-rw-r--r--jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java68
1 files changed, 68 insertions, 0 deletions
diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java
index 780eaadda0..77372d8209 100644
--- a/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java
+++ b/jetty-util/src/test/java/org/eclipse/jetty/util/BufferUtilTest.java
@@ -19,15 +19,22 @@
package org.eclipse.jetty.util;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.util.Arrays;
+import java.util.Random;
+import org.eclipse.jetty.util.log.Log;
+import org.eclipse.jetty.util.log.Logger;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
+import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class BufferUtilTest
@@ -226,4 +233,65 @@ public class BufferUtilTest
Assert.assertEquals("Count of bytes",length,count);
}
+
+ private static final Logger LOG = Log.getLogger(BufferUtilTest.class);
+
+ @Test
+ @Ignore("Very simple microbenchmark to compare different writeTo implementations. Only for development thus " +
+ "ignored.")
+ public void testWriteToMicrobenchmark() throws IOException
+ {
+ int capacity = 1024 * 1024;
+ int iterations = 30;
+ byte[] bytes = new byte[capacity];
+ new Random().nextBytes(bytes);
+ ByteBuffer buffer = BufferUtil.allocate(capacity);
+ BufferUtil.append(buffer, bytes, 0, capacity);
+ long start = System.nanoTime();
+ for (int i = 0; i < iterations; i++)
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ long startRun = System.nanoTime();
+ BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out);
+ long elapsedRun = System.nanoTime() - startRun;
+ LOG.warn("run elapsed={}ms", elapsedRun / 1000);
+ assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true));
+ }
+ long elapsed = System.nanoTime() - start;
+ LOG.warn("elapsed={}ms average={}ms", elapsed / 1000, elapsed/iterations/1000);
+ }
+
+ @Test
+ public void testWriteToWithBufferThatDoesNotExposeArrayAndSmallContent() throws IOException
+ {
+ int capacity = BufferUtil.TEMP_BUFFER_SIZE/4;
+ testWriteToWithBufferThatDoesNotExposeArray(capacity);
+ }
+
+ @Test
+ public void testWriteToWithBufferThatDoesNotExposeArrayAndContentLengthMatchingTempBufferSize() throws IOException
+ {
+ int capacity = BufferUtil.TEMP_BUFFER_SIZE;
+ testWriteToWithBufferThatDoesNotExposeArray(capacity);
+ }
+
+ @Test
+ public void testWriteToWithBufferThatDoesNotExposeArrayAndContentSlightlyBiggerThanTwoTimesTempBufferSize()
+ throws
+ IOException
+ {
+ int capacity = BufferUtil.TEMP_BUFFER_SIZE*2+1024;
+ testWriteToWithBufferThatDoesNotExposeArray(capacity);
+ }
+
+ private void testWriteToWithBufferThatDoesNotExposeArray(int capacity) throws IOException
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ byte[] bytes = new byte[capacity];
+ new Random().nextBytes(bytes);
+ ByteBuffer buffer = BufferUtil.allocate(capacity);
+ BufferUtil.append(buffer, bytes, 0, capacity);
+ BufferUtil.writeTo(buffer.asReadOnlyBuffer(), out);
+ assertThat("Bytes in out equal bytes in buffer", Arrays.equals(bytes, out.toByteArray()), is(true));
+ }
}

Back to the top