|
| 1 | +package com.jsoniter.benchmark.skip_multi_levels; |
| 2 | + |
| 3 | +import com.jsoniter.benchmark.All; |
| 4 | +import org.apache.thrift.TDeserializer; |
| 5 | +import org.apache.thrift.TException; |
| 6 | +import org.apache.thrift.TSerializer; |
| 7 | +import org.apache.thrift.protocol.TCompactProtocol; |
| 8 | +import org.junit.Test; |
| 9 | +import org.openjdk.jmh.Main; |
| 10 | +import org.openjdk.jmh.annotations.*; |
| 11 | +import org.openjdk.jmh.infra.BenchmarkParams; |
| 12 | +import org.openjdk.jmh.infra.Blackhole; |
| 13 | +import org.openjdk.jmh.runner.RunnerException; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | + |
| 21 | +/* |
| 22 | +Benchmark Mode Cnt Score Error Units |
| 23 | +DeserThrift.deser avgt 5 140635.170 ± 2962.132 ns/op |
| 24 | + */ |
| 25 | +@State(Scope.Thread) |
| 26 | +public class DeserThrift { |
| 27 | + |
| 28 | + private TSerializer serializer; |
| 29 | + private TDeserializer deserializer; |
| 30 | + private byte[] testData; |
| 31 | + |
| 32 | + @Setup(Level.Trial) |
| 33 | + public void benchSetup(BenchmarkParams params) throws TException { |
| 34 | + ThriftTestWriteObject testObject = new ThriftTestWriteObject(); |
| 35 | + testObject.setField1(Arrays.asList("1", "2", "3", "4", "5", |
| 36 | + "1", "2", "3", "4", "5", |
| 37 | + "1", "2", "3", "4", "5")); |
| 38 | + HashMap<String, List<String>> field2 = new HashMap<>(); |
| 39 | + field2.put("1", Arrays.asList("1", "2", "3", "4", "5")); |
| 40 | + field2.put("2", Arrays.asList("1", "2", "3", "4", "5")); |
| 41 | + field2.put("3", Arrays.asList("1", "2", "3", "4", "5")); |
| 42 | + testObject.setField2(field2); |
| 43 | + testObject.setField3("x-3"); |
| 44 | + serializer = new TSerializer(new TCompactProtocol.Factory()); |
| 45 | + deserializer = new TDeserializer(new TCompactProtocol.Factory()); |
| 46 | + testData = serializer.serialize(testObject); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void test() throws TException { |
| 51 | +// byte[] output = new TSerializer(new TCompactProtocol.Factory()).serialize(testObject); |
| 52 | +// System.out.println(output.length); |
| 53 | + } |
| 54 | + |
| 55 | + @Benchmark |
| 56 | + @BenchmarkMode(Mode.AverageTime) |
| 57 | + @OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 58 | + public void deser(Blackhole bh) throws TException { |
| 59 | + for (int i = 0; i < 1000; i++) { |
| 60 | + ThriftTestReadObject obj = new ThriftTestReadObject(); |
| 61 | + deserializer.deserialize(obj, testData); |
| 62 | + bh.consume(obj); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public static void main(String[] args) throws IOException, RunnerException { |
| 67 | + All.loadJMH(); |
| 68 | + Main.main(new String[]{ |
| 69 | + "with_long_string.DeserThrift", |
| 70 | + "-i", "5", |
| 71 | + "-wi", "5", |
| 72 | + "-f", "1", |
| 73 | + }); |
| 74 | + } |
| 75 | +} |
0 commit comments