Skip to content

Commit

Permalink
Jackson 性能验证
Browse files Browse the repository at this point in the history
  • Loading branch information
hank-whu committed Jul 28, 2018
1 parent 6f7ead1 commit 3c7316d
Showing 1 changed file with 62 additions and 2 deletions.
@@ -1,5 +1,8 @@
package rpc.turbo.benchmark.serialization;

import java.nio.ByteBuffer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
Expand All @@ -12,6 +15,15 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.esotericsoftware.kryo.io.ByteBufferInputStream;
import com.esotericsoftware.kryo.io.ByteBufferOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;
Expand All @@ -25,10 +37,32 @@
public class JacksonBenchmark {
public static final int CONCURRENCY = Runtime.getRuntime().availableProcessors();

private static final ObjectMapper mapper;

static {
mapper = new ObjectMapper();

mapper.registerModule(new Jdk8Module());

JavaTimeModule javaTimeModule = new JavaTimeModule();
// Hack time module to allow 'Z' at the end of string (i.e. javascript json's)
javaTimeModule.addDeserializer(LocalDateTime.class,
new LocalDateTimeDeserializer(DateTimeFormatter.ISO_DATE_TIME));
mapper.registerModule(javaTimeModule);

mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

mapper.registerModule(new AfterburnerModule());
}

ByteBufAllocator allocator = new UnpooledByteBufAllocator(true);
ByteBuf userBuffer = allocator.directBuffer(1024 * 1024 * 8, 1024 * 1024 * 8);
ByteBuf listBuffer = allocator.directBuffer(1024 * 1024 * 8, 1024 * 1024 * 8);

ByteBuffer nioBuffer = ByteBuffer.allocateDirect(1024 * 1024 * 8);
ByteBufferOutputStream nioOutputStream = new ByteBufferOutputStream(nioBuffer);
ByteBufferInputStream nioInputStream = new ByteBufferInputStream(nioBuffer);

private final JacksonMapper jacksonMapper = new JacksonMapper();

private final UserService userService = new UserServiceServerImpl();
Expand Down Expand Up @@ -81,12 +115,38 @@ public void serializeUserList() throws Exception {
jacksonMapper.write(listBuffer, userPage);
}

@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void serializeUser2() throws Exception {
nioBuffer.clear();
userBuffer.clear();

mapper.writeValue(nioOutputStream, user);

nioBuffer.flip();
userBuffer.writeBytes(nioBuffer);
}

@Benchmark
@BenchmarkMode({ Mode.Throughput })
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void serializeUserList2() throws Exception {
nioBuffer.clear();
listBuffer.clear();

mapper.writeValue(nioOutputStream, userPage);

nioBuffer.flip();
listBuffer.writeBytes(nioBuffer);
}

public static void main(String[] args) throws Exception {

Options opt = new OptionsBuilder()//
.include(JacksonBenchmark.class.getName())//
.warmupIterations(3)//
.measurementIterations(3)//
.warmupIterations(10)//
.measurementIterations(10)//
.threads(CONCURRENCY)//
.forks(1)//
.build();
Expand Down

0 comments on commit 3c7316d

Please sign in to comment.