覚えたら書く

IT関係のデベロッパとして日々覚えたことを書き残したいです。twitter: @yyoshikaw

Gson vs Genson vs Fastjson

JavaのJSONライブラリ(GsonとGenson)のベンチマークをとってみたばかりですが、


その時は対象になかったFastjsonというライブラリも含めて比較してみました。


(なぜか、またJacksonを含めていないです笑)


pom.xmlに追加した内容は以下の通り

<dependency>
  <groupId>com.owlike</groupId>
  <artifactId>genson</artifactId>
  <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.7</version>
</dependency>
<dependency>
    <groupId>net.arnx</groupId>
    <artifactId>jsonic</artifactId>
    <version>1.3.10</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.17</version>
</dependency>

JMHでの測定用のコードは以下です

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.google.gson.Gson;
import com.owlike.genson.Genson;

public class JsonBenchmark {

    static final Genson genson = new Genson();

    static final Gson gson = new Gson();

    private static final Person person = new Person();

    private static final String JSON_STR = "{\"age\":28,\"name\":\"Foo\"}";

    static {
        person.setName("Taro");
        person.setAge(30);
    }

    @Benchmark
    public void Gson_deserialize() {
        Person person = gson.fromJson(JSON_STR, Person.class);
    }

    @Benchmark
    public void Genson_deserialize() {
        Person person = genson.deserialize(JSON_STR, Person.class);
    }

    @Benchmark
    public void JSONIC_deserialize() {
        Person person = net.arnx.jsonic.JSON.decode(JSON_STR, Person.class);
    }

    @Benchmark
    public void FastJson_deserialize() {
        Person person = com.alibaba.fastjson.JSON.parseObject(JSON_STR, Person.class);
    }

    @Benchmark
    public void Gson_serialize() {
        String json = gson.toJson(person);
    }

    @Benchmark
    public void Genson_serialize() {
        String json = genson.serialize(person);
    }

    @Benchmark
    public void JSONIC_serialize() {
        String json = net.arnx.jsonic.JSON.encode(person);
    }

    @Benchmark
    public void FastJson_serialize() {
        String json = com.alibaba.fastjson.JSON.toJSONString(person);
    }

    static final class Person {

        private String name;

        private int age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                 .include(JsonBenchmark.class.getSimpleName())
                .warmupIterations(15)
                .forks(1)
                .mode(Mode.Throughput)
                .build();
        new Runner(opt).run();
    }

}

結果

# Run complete. Total time: 00:04:54

Benchmark                            Mode  Cnt        Score        Error  Units
JsonBenchmark.FastJson_deserialize  thrpt   20  2476427.743 ±  29939.747  ops/s
JsonBenchmark.FastJson_serialize    thrpt   20  1612380.250 ± 159789.875  ops/s
JsonBenchmark.Genson_deserialize    thrpt   20   367556.613 ±  40464.880  ops/s
JsonBenchmark.Genson_serialize      thrpt   20   696024.279 ±  56951.007  ops/s
JsonBenchmark.Gson_deserialize      thrpt   20   565349.579 ±  30805.237  ops/s
JsonBenchmark.Gson_serialize        thrpt   20   941140.108 ±  15984.176  ops/s
JsonBenchmark.JSONIC_deserialize    thrpt   20    88681.196 ±    540.533  ops/s
JsonBenchmark.JSONIC_serialize      thrpt   20   202211.000 ±  19496.492  ops/s

単純なパターンでしか試していないのであれですが、
この結果だけ見るとFastjsonがやたら速いということになります。
ここまでFastjsonが速いとなるともう少し複雑なJSON形式のデータでもベンチマークとってみたいところですね。
JSONICは相変わらず涙目。



関連エントリ