Java中的性能

测试代码:

```bash
package com.company.benchmark;

import com.fasterxml.jackson.databind.JsonNode;
import com.company.util.JsonUtil;
import org.openjdk.jmh.annotations.*;
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 java.util.concurrent.TimeUnit;

/**
 * Created by emacsist on 2017/6/12.
 */
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class BenchmarkJSON {
    @Benchmark
    public void testJSON(){
        String json = "{\"id\":\"1\",\"user\":{\"id\":\"426b466fe00e698a2b718079afff962f\"},\"rule\":[\"50284\",\"56415\",\"56416\",\"56417\",\"56418\",\"56419\",\"56420\",\"56421\",\"56422\",\"56423\",\"57034\",\"57036\",\"9078\"],\"imp\":[{\"bidfloor\":800,\"ext\":{\"repeat\":1},\"feed\":{\"type\":0},\"id\":\"25263317556521\",\"bidfloorcur\":\"RMB\",\"tagid\":\"1000000002\"},{\"bidfloor\":100000,\"ext\":{\"repeat\":1},\"feed\":{\"type\":0},\"id\":\"25263317556522\",\"bidfloorcur\":\"RMB\",\"tagid\":\"1000000002\"}],\"app\":{\"name\":\"weibo\",\"id\":\"iamappidinwax\"},\"device\":{\"geo\":{},\"carrier\":\"\",\"model\":\"OPPO_OPPO A37t\",\"ua\":\"OPPO-OPPO A37t__weibo__6.11.1__android__android5.1\",\"ip\":\"113.227.98.236\",\"ext\":{},\"connectiontype\":2,\"os\":\"android\",\"osv\":\"5.1\"},\"dealid\":\"47cd7e122ade3d12e312c5eb6e68bc6e\",\"at\":2}";
        JsonNode node = JsonUtil.readTree(json);
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(BenchmarkJSON.class.getSimpleName())
                .forks(1)
                .warmupIterations(5)
                .measurementIterations(5)
                .threads(500)
                .build();

        new Runner(opt).run();

}

结果:

  Percentiles, ns/op:
      p(0.0000) =  58682.052 ns/op
     p(50.0000) =  61242.538 ns/op
     p(90.0000) =  62028.112 ns/op
     p(95.0000) =  62028.112 ns/op
     p(99.0000) =  62028.112 ns/op
     p(99.9000) =  62028.112 ns/op
     p(99.9900) =  62028.112 ns/op
     p(99.9990) =  62028.112 ns/op
     p(99.9999) =  62028.112 ns/op
    p(100.0000) =  62028.112 ns/op

单线程版本:即将上面的 threads 删除掉:

  Percentiles, ns/op:
      p(0.0000) = 346423.000 ns/op
     p(50.0000) = 663625.000 ns/op
     p(90.0000) = 749015.000 ns/op
     p(95.0000) = 749015.000 ns/op
     p(99.0000) = 749015.000 ns/op
     p(99.9000) = 749015.000 ns/op
     p(99.9900) = 749015.000 ns/op
     p(99.9990) = 749015.000 ns/op
     p(99.9999) = 749015.000 ns/op
    p(100.0000) = 749015.000 ns/op

Golang 中的性能

测试代码:

package test

import (
	"encoding/json"
	"testing"

	"github.com/pquerna/ffjson/ffjson"
	"github.com/company/golang-dsp-bid/bean"
)

func BenchmarkJson(b *testing.B) {
	j := `{"id":"1","user":{"id":"426b466fe00c698a2b718079efff962f"},"rule":["50284","56415","56416","56417","56418","56419","56420","56421","56422","56423","57034","57036","9078"],"imp":[{"bidfloor":100000,"ext":{"repeat":1},"feed":{"type":0},"id":"25263317556521","bidfloorcur":"RMB","tagid":"1000000002"},{"bidfloor":800,"ext":{"repeat":1},"feed":{"type":0},"id":"25263317556522","bidfloorcur":"RMB","tagid":"1000000002"}],"app":{"name":"weibo","id":"iamappidinwax"},"device":{"geo":{},"carrier":"","model":"OPPO_OPPO A37t","ua":"OPPO-OPPO A37t__weibo__6.11.1__android__android5.1","ip":"1.1.1.1","ext":{},"connectiontype":2,"os":"android","osv":"5.1"},"dealid":"47cd7e122ade3d12e312c5eb6e68bc6e","at":2}`
	for i := 0; i < b.N; i++ {
		var bidRequest bean.BidRequest
		e := json.Unmarshal([]byte(j), &bidRequest)
		if e != nil {
			panic(e)
		}
	}
}

func BenchmarkFFJsonFast(b *testing.B) {
    j := `{"id":"1","user":{"id":"426b466fe00c698a2b718079efff962f"},"rule":["50284","56415","56416","56417","56418","56419","56420","56421","56422","56423","57034","57036","9078"],"imp":[{"bidfloor":100000,"ext":{"repeat":1},"feed":{"type":0},"id":"25263317556521","bidfloorcur":"RMB","tagid":"1000000002"},{"bidfloor":800,"ext":{"repeat":1},"feed":{"type":0},"id":"25263317556522","bidfloorcur":"RMB","tagid":"1000000002"}],"app":{"name":"weibo","id":"iamappidinwax"},"device":{"geo":{},"carrier":"","model":"OPPO_OPPO A37t","ua":"OPPO-OPPO A37t__weibo__6.11.1__android__android5.1","ip":"1.1.1.1","ext":{},"connectiontype":2,"os":"android","osv":"5.1"},"dealid":"47cd7e122ade3d12e312c5eb6e68bc6e","at":2}`

	for i := 0; i < b.N; i++ {
		var bidRequest bean.BidRequest
		e := ffjson.UnmarshalFast([]byte(j), &bidRequest)
		if e != nil {
			panic(e)
		}
	}
}

func BenchmarkFFJson(b *testing.B) {
    j := `{"id":"1","user":{"id":"426b466fe00c698a2b718079efff962f"},"rule":["50284","56415","56416","56417","56418","56419","56420","56421","56422","56423","57034","57036","9078"],"imp":[{"bidfloor":100000,"ext":{"repeat":1},"feed":{"type":0},"id":"25263317556521","bidfloorcur":"RMB","tagid":"1000000002"},{"bidfloor":800,"ext":{"repeat":1},"feed":{"type":0},"id":"25263317556522","bidfloorcur":"RMB","tagid":"1000000002"}],"app":{"name":"weibo","id":"iamappidinwax"},"device":{"geo":{},"carrier":"","model":"OPPO_OPPO A37t","ua":"OPPO-OPPO A37t__weibo__6.11.1__android__android5.1","ip":"1.1.1.1","ext":{},"connectiontype":2,"os":"android","osv":"5.1"},"dealid":"47cd7e122ade3d12e312c5eb6e68bc6e","at":2}`
	for i := 0; i < b.N; i++ {
		var bidRequest bean.BidRequest
		e := ffjson.Unmarshal([]byte(j), &bidRequest)
		if e != nil {
			panic(e)
		}
	}
}

````

结果:


```bash
[15:38:13] emacsist:test git:(master*) $ go test -v  -bench=.
BenchmarkJson-4         	   50000	     31739 ns/op
BenchmarkFFJsonFast-4   	  100000	     21533 ns/op
BenchmarkFFJson-4       	  100000	     21390 ns/op
PASS
ok  	github.com/company/golang-dsp-bid/test	6.682s