Source file src/encoding/json/bench_test.go

Documentation: encoding/json

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Large data benchmark.
     6  // The JSON data is a summary of agl's changes in the
     7  // go, webkit, and chromium open source projects.
     8  // We benchmark converting between the JSON form
     9  // and in-memory data structures.
    10  
    11  package json
    12  
    13  import (
    14  	"bytes"
    15  	"compress/gzip"
    16  	"fmt"
    17  	"internal/testenv"
    18  	"io/ioutil"
    19  	"os"
    20  	"reflect"
    21  	"runtime"
    22  	"strings"
    23  	"sync"
    24  	"testing"
    25  )
    26  
    27  type codeResponse struct {
    28  	Tree     *codeNode `json:"tree"`
    29  	Username string    `json:"username"`
    30  }
    31  
    32  type codeNode struct {
    33  	Name     string      `json:"name"`
    34  	Kids     []*codeNode `json:"kids"`
    35  	CLWeight float64     `json:"cl_weight"`
    36  	Touches  int         `json:"touches"`
    37  	MinT     int64       `json:"min_t"`
    38  	MaxT     int64       `json:"max_t"`
    39  	MeanT    int64       `json:"mean_t"`
    40  }
    41  
    42  var codeJSON []byte
    43  var codeStruct codeResponse
    44  
    45  func codeInit() {
    46  	f, err := os.Open("testdata/code.json.gz")
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  	defer f.Close()
    51  	gz, err := gzip.NewReader(f)
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  	data, err := ioutil.ReadAll(gz)
    56  	if err != nil {
    57  		panic(err)
    58  	}
    59  
    60  	codeJSON = data
    61  
    62  	if err := Unmarshal(codeJSON, &codeStruct); err != nil {
    63  		panic("unmarshal code.json: " + err.Error())
    64  	}
    65  
    66  	if data, err = Marshal(&codeStruct); err != nil {
    67  		panic("marshal code.json: " + err.Error())
    68  	}
    69  
    70  	if !bytes.Equal(data, codeJSON) {
    71  		println("different lengths", len(data), len(codeJSON))
    72  		for i := 0; i < len(data) && i < len(codeJSON); i++ {
    73  			if data[i] != codeJSON[i] {
    74  				println("re-marshal: changed at byte", i)
    75  				println("orig: ", string(codeJSON[i-10:i+10]))
    76  				println("new: ", string(data[i-10:i+10]))
    77  				break
    78  			}
    79  		}
    80  		panic("re-marshal code.json: different result")
    81  	}
    82  }
    83  
    84  func BenchmarkCodeEncoder(b *testing.B) {
    85  	b.ReportAllocs()
    86  	if codeJSON == nil {
    87  		b.StopTimer()
    88  		codeInit()
    89  		b.StartTimer()
    90  	}
    91  	b.RunParallel(func(pb *testing.PB) {
    92  		enc := NewEncoder(ioutil.Discard)
    93  		for pb.Next() {
    94  			if err := enc.Encode(&codeStruct); err != nil {
    95  				b.Fatal("Encode:", err)
    96  			}
    97  		}
    98  	})
    99  	b.SetBytes(int64(len(codeJSON)))
   100  }
   101  
   102  func BenchmarkCodeMarshal(b *testing.B) {
   103  	b.ReportAllocs()
   104  	if codeJSON == nil {
   105  		b.StopTimer()
   106  		codeInit()
   107  		b.StartTimer()
   108  	}
   109  	b.RunParallel(func(pb *testing.PB) {
   110  		for pb.Next() {
   111  			if _, err := Marshal(&codeStruct); err != nil {
   112  				b.Fatal("Marshal:", err)
   113  			}
   114  		}
   115  	})
   116  	b.SetBytes(int64(len(codeJSON)))
   117  }
   118  
   119  func benchMarshalBytes(n int) func(*testing.B) {
   120  	sample := []byte("hello world")
   121  	// Use a struct pointer, to avoid an allocation when passing it as an
   122  	// interface parameter to Marshal.
   123  	v := &struct {
   124  		Bytes []byte
   125  	}{
   126  		bytes.Repeat(sample, (n/len(sample))+1)[:n],
   127  	}
   128  	return func(b *testing.B) {
   129  		for i := 0; i < b.N; i++ {
   130  			if _, err := Marshal(v); err != nil {
   131  				b.Fatal("Marshal:", err)
   132  			}
   133  		}
   134  	}
   135  }
   136  
   137  func BenchmarkMarshalBytes(b *testing.B) {
   138  	b.ReportAllocs()
   139  	// 32 fits within encodeState.scratch.
   140  	b.Run("32", benchMarshalBytes(32))
   141  	// 256 doesn't fit in encodeState.scratch, but is small enough to
   142  	// allocate and avoid the slower base64.NewEncoder.
   143  	b.Run("256", benchMarshalBytes(256))
   144  	// 4096 is large enough that we want to avoid allocating for it.
   145  	b.Run("4096", benchMarshalBytes(4096))
   146  }
   147  
   148  func BenchmarkCodeDecoder(b *testing.B) {
   149  	b.ReportAllocs()
   150  	if codeJSON == nil {
   151  		b.StopTimer()
   152  		codeInit()
   153  		b.StartTimer()
   154  	}
   155  	b.RunParallel(func(pb *testing.PB) {
   156  		var buf bytes.Buffer
   157  		dec := NewDecoder(&buf)
   158  		var r codeResponse
   159  		for pb.Next() {
   160  			buf.Write(codeJSON)
   161  			// hide EOF
   162  			buf.WriteByte('\n')
   163  			buf.WriteByte('\n')
   164  			buf.WriteByte('\n')
   165  			if err := dec.Decode(&r); err != nil {
   166  				b.Fatal("Decode:", err)
   167  			}
   168  		}
   169  	})
   170  	b.SetBytes(int64(len(codeJSON)))
   171  }
   172  
   173  func BenchmarkUnicodeDecoder(b *testing.B) {
   174  	b.ReportAllocs()
   175  	j := []byte(`"\uD83D\uDE01"`)
   176  	b.SetBytes(int64(len(j)))
   177  	r := bytes.NewReader(j)
   178  	dec := NewDecoder(r)
   179  	var out string
   180  	b.ResetTimer()
   181  	for i := 0; i < b.N; i++ {
   182  		if err := dec.Decode(&out); err != nil {
   183  			b.Fatal("Decode:", err)
   184  		}
   185  		r.Seek(0, 0)
   186  	}
   187  }
   188  
   189  func BenchmarkDecoderStream(b *testing.B) {
   190  	b.ReportAllocs()
   191  	b.StopTimer()
   192  	var buf bytes.Buffer
   193  	dec := NewDecoder(&buf)
   194  	buf.WriteString(`"` + strings.Repeat("x", 1000000) + `"` + "\n\n\n")
   195  	var x interface{}
   196  	if err := dec.Decode(&x); err != nil {
   197  		b.Fatal("Decode:", err)
   198  	}
   199  	ones := strings.Repeat(" 1\n", 300000) + "\n\n\n"
   200  	b.StartTimer()
   201  	for i := 0; i < b.N; i++ {
   202  		if i%300000 == 0 {
   203  			buf.WriteString(ones)
   204  		}
   205  		x = nil
   206  		if err := dec.Decode(&x); err != nil || x != 1.0 {
   207  			b.Fatalf("Decode: %v after %d", err, i)
   208  		}
   209  	}
   210  }
   211  
   212  func BenchmarkCodeUnmarshal(b *testing.B) {
   213  	b.ReportAllocs()
   214  	if codeJSON == nil {
   215  		b.StopTimer()
   216  		codeInit()
   217  		b.StartTimer()
   218  	}
   219  	b.RunParallel(func(pb *testing.PB) {
   220  		for pb.Next() {
   221  			var r codeResponse
   222  			if err := Unmarshal(codeJSON, &r); err != nil {
   223  				b.Fatal("Unmarshal:", err)
   224  			}
   225  		}
   226  	})
   227  	b.SetBytes(int64(len(codeJSON)))
   228  }
   229  
   230  func BenchmarkCodeUnmarshalReuse(b *testing.B) {
   231  	b.ReportAllocs()
   232  	if codeJSON == nil {
   233  		b.StopTimer()
   234  		codeInit()
   235  		b.StartTimer()
   236  	}
   237  	b.RunParallel(func(pb *testing.PB) {
   238  		var r codeResponse
   239  		for pb.Next() {
   240  			if err := Unmarshal(codeJSON, &r); err != nil {
   241  				b.Fatal("Unmarshal:", err)
   242  			}
   243  		}
   244  	})
   245  	b.SetBytes(int64(len(codeJSON)))
   246  }
   247  
   248  func BenchmarkUnmarshalString(b *testing.B) {
   249  	b.ReportAllocs()
   250  	data := []byte(`"hello, world"`)
   251  	b.RunParallel(func(pb *testing.PB) {
   252  		var s string
   253  		for pb.Next() {
   254  			if err := Unmarshal(data, &s); err != nil {
   255  				b.Fatal("Unmarshal:", err)
   256  			}
   257  		}
   258  	})
   259  }
   260  
   261  func BenchmarkUnmarshalFloat64(b *testing.B) {
   262  	b.ReportAllocs()
   263  	data := []byte(`3.14`)
   264  	b.RunParallel(func(pb *testing.PB) {
   265  		var f float64
   266  		for pb.Next() {
   267  			if err := Unmarshal(data, &f); err != nil {
   268  				b.Fatal("Unmarshal:", err)
   269  			}
   270  		}
   271  	})
   272  }
   273  
   274  func BenchmarkUnmarshalInt64(b *testing.B) {
   275  	b.ReportAllocs()
   276  	data := []byte(`3`)
   277  	b.RunParallel(func(pb *testing.PB) {
   278  		var x int64
   279  		for pb.Next() {
   280  			if err := Unmarshal(data, &x); err != nil {
   281  				b.Fatal("Unmarshal:", err)
   282  			}
   283  		}
   284  	})
   285  }
   286  
   287  func BenchmarkIssue10335(b *testing.B) {
   288  	b.ReportAllocs()
   289  	j := []byte(`{"a":{ }}`)
   290  	b.RunParallel(func(pb *testing.PB) {
   291  		var s struct{}
   292  		for pb.Next() {
   293  			if err := Unmarshal(j, &s); err != nil {
   294  				b.Fatal(err)
   295  			}
   296  		}
   297  	})
   298  }
   299  
   300  func BenchmarkUnmapped(b *testing.B) {
   301  	b.ReportAllocs()
   302  	j := []byte(`{"s": "hello", "y": 2, "o": {"x": 0}, "a": [1, 99, {"x": 1}]}`)
   303  	b.RunParallel(func(pb *testing.PB) {
   304  		var s struct{}
   305  		for pb.Next() {
   306  			if err := Unmarshal(j, &s); err != nil {
   307  				b.Fatal(err)
   308  			}
   309  		}
   310  	})
   311  }
   312  
   313  func BenchmarkTypeFieldsCache(b *testing.B) {
   314  	b.ReportAllocs()
   315  	var maxTypes int = 1e6
   316  	if testenv.Builder() != "" {
   317  		maxTypes = 1e3 // restrict cache sizes on builders
   318  	}
   319  
   320  	// Dynamically generate many new types.
   321  	types := make([]reflect.Type, maxTypes)
   322  	fs := []reflect.StructField{{
   323  		Type:  reflect.TypeOf(""),
   324  		Index: []int{0},
   325  	}}
   326  	for i := range types {
   327  		fs[0].Name = fmt.Sprintf("TypeFieldsCache%d", i)
   328  		types[i] = reflect.StructOf(fs)
   329  	}
   330  
   331  	// clearClear clears the cache. Other JSON operations, must not be running.
   332  	clearCache := func() {
   333  		fieldCache = sync.Map{}
   334  	}
   335  
   336  	// MissTypes tests the performance of repeated cache misses.
   337  	// This measures the time to rebuild a cache of size nt.
   338  	for nt := 1; nt <= maxTypes; nt *= 10 {
   339  		ts := types[:nt]
   340  		b.Run(fmt.Sprintf("MissTypes%d", nt), func(b *testing.B) {
   341  			nc := runtime.GOMAXPROCS(0)
   342  			for i := 0; i < b.N; i++ {
   343  				clearCache()
   344  				var wg sync.WaitGroup
   345  				for j := 0; j < nc; j++ {
   346  					wg.Add(1)
   347  					go func(j int) {
   348  						for _, t := range ts[(j*len(ts))/nc : ((j+1)*len(ts))/nc] {
   349  							cachedTypeFields(t)
   350  						}
   351  						wg.Done()
   352  					}(j)
   353  				}
   354  				wg.Wait()
   355  			}
   356  		})
   357  	}
   358  
   359  	// HitTypes tests the performance of repeated cache hits.
   360  	// This measures the average time of each cache lookup.
   361  	for nt := 1; nt <= maxTypes; nt *= 10 {
   362  		// Pre-warm a cache of size nt.
   363  		clearCache()
   364  		for _, t := range types[:nt] {
   365  			cachedTypeFields(t)
   366  		}
   367  		b.Run(fmt.Sprintf("HitTypes%d", nt), func(b *testing.B) {
   368  			b.RunParallel(func(pb *testing.PB) {
   369  				for pb.Next() {
   370  					cachedTypeFields(types[0])
   371  				}
   372  			})
   373  		})
   374  	}
   375  }
   376  

View as plain text