Created
April 12, 2014 18:09
-
-
Save buchgr/10548901 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 The Netty Project | |
* | |
* The Netty Project 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 benchmark; | |
import io.netty.buffer.BipartiteByteBuf; | |
import io.netty.buffer.ByteBuf; | |
import io.netty.buffer.ByteBufAllocator; | |
import io.netty.buffer.CompositeByteBuf; | |
import io.netty.buffer.UnpooledByteBufAllocator; | |
import org.openjdk.jmh.annotations.GenerateMicroBenchmark; | |
import org.openjdk.jmh.annotations.Level; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.nio.ByteBuffer; | |
import java.nio.channels.Channels; | |
import java.nio.channels.GatheringByteChannel; | |
import java.nio.channels.WritableByteChannel; | |
import java.util.Random; | |
@State(Scope.Thread) | |
public class BipartiteVsCompositeBenchmark { | |
private ByteBuf buf1; | |
private ByteBuf buf2; | |
@Param({ "00000", "00250", "00500", "01000", "04000", "08000", "32000", "64000" }) | |
public int size; | |
@Setup(Level.Trial) | |
public void setup() { | |
Random rnd = new Random(); | |
buf1 = UnpooledByteBufAllocator.DEFAULT.heapBuffer(size); | |
for (int i=0; i < size / 4; i++) { | |
buf1.writeInt(rnd.nextInt()); | |
} | |
buf2 = UnpooledByteBufAllocator.DEFAULT.heapBuffer(size); | |
for (int i=0; i < size / 4; i++) { | |
buf2.writeInt(rnd.nextInt()); | |
} | |
} | |
@GenerateMicroBenchmark | |
public void socketWriteCompositeByteBuf() { | |
TestGatheringByteChannel channel = new TestGatheringByteChannel(); | |
try { | |
CompositeByteBuf buf = ByteBufAllocator.DEFAULT.compositeBuffer(2); | |
buf.addComponent(buf1); | |
buf.addComponent(buf2); | |
buf.getBytes(0, channel, size); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@GenerateMicroBenchmark | |
public void socketWriteBipartiteByteBuf() { | |
TestGatheringByteChannel channel = new TestGatheringByteChannel(); | |
try { | |
BipartiteByteBuf buf = ByteBufAllocator.DEFAULT.bipartiteBuffer(); | |
buf.part1(buf1).part2(buf2); | |
buf.getBytes(0, channel, size); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
static final class TestGatheringByteChannel implements GatheringByteChannel { | |
private final ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
private final WritableByteChannel channel = Channels.newChannel(out); | |
private final int limit; | |
TestGatheringByteChannel(int limit) { | |
this.limit = limit; | |
} | |
TestGatheringByteChannel() { | |
this(Integer.MAX_VALUE); | |
} | |
@Override | |
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException { | |
long written = 0; | |
for (; offset < length; offset++) { | |
written += write(srcs[offset]); | |
if (written >= limit) { | |
break; | |
} | |
} | |
return written; | |
} | |
@Override | |
public long write(ByteBuffer[] srcs) throws IOException { | |
return write(srcs, 0, srcs.length); | |
} | |
@Override | |
public int write(ByteBuffer src) throws IOException { | |
int oldLimit = src.limit(); | |
if (limit < src.remaining()) { | |
src.limit(src.position() + limit); | |
} | |
int w = channel.write(src); | |
src.limit(oldLimit); | |
return w; | |
} | |
@Override | |
public boolean isOpen() { | |
return channel.isOpen(); | |
} | |
@Override | |
public void close() throws IOException { | |
channel.close(); | |
} | |
public byte[] writtenBytes() { | |
return out.toByteArray(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See update in: https://gist.github.com/shipilev/10605333/revisions