github.com/minio/simdjson-go@v0.4.6-0.20231116094823-04d21cddf993/benchmarks_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package simdjson
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func benchmarkFromFile(b *testing.B, filename string) {
    24  	if !SupportedCPU() {
    25  		b.SkipNow()
    26  	}
    27  	msg := loadCompressed(b, filename)
    28  
    29  	b.Run("copy", func(b *testing.B) {
    30  		pj := &ParsedJson{}
    31  		b.SetBytes(int64(len(msg)))
    32  		b.ReportAllocs()
    33  		b.ResetTimer()
    34  		for i := 0; i < b.N; i++ {
    35  			// Reset tape
    36  			var err error
    37  			pj, err = Parse(msg, pj, WithCopyStrings(true))
    38  			if err != nil {
    39  				b.Fatal(err)
    40  			}
    41  		}
    42  	})
    43  	b.Run("nocopy", func(b *testing.B) {
    44  		pj := &ParsedJson{}
    45  		b.SetBytes(int64(len(msg)))
    46  		b.ReportAllocs()
    47  		b.ResetTimer()
    48  		for i := 0; i < b.N; i++ {
    49  			// Reset tape
    50  			var err error
    51  			pj, err = Parse(msg, pj, WithCopyStrings(false))
    52  			if err != nil {
    53  				b.Fatal(err)
    54  			}
    55  		}
    56  	})
    57  	b.Run("nocopy-par", func(b *testing.B) {
    58  		b.RunParallel(func(pb *testing.PB) {
    59  			pj := &ParsedJson{}
    60  			b.SetBytes(int64(len(msg)))
    61  			b.ReportAllocs()
    62  			b.ResetTimer()
    63  			for pb.Next() {
    64  				// Reset tape
    65  				var err error
    66  				pj, err = Parse(msg, pj, WithCopyStrings(false))
    67  				if err != nil {
    68  					b.Fatal(err)
    69  				}
    70  			}
    71  		})
    72  	})
    73  
    74  }
    75  
    76  func BenchmarkParseSmall(b *testing.B)  { benchmarkFromFile(b, "payload-small") }
    77  func BenchmarkParseMedium(b *testing.B) { benchmarkFromFile(b, "payload-medium") }
    78  func BenchmarkParseLarge(b *testing.B)  { benchmarkFromFile(b, "payload-large") }
    79  
    80  func BenchmarkJsonParserLarge(b *testing.B) {
    81  	largeFixture := loadCompressed(b, "payload-large")
    82  
    83  	b.Run("nocopy", func(b *testing.B) {
    84  		pj := &ParsedJson{}
    85  		b.SetBytes(int64(len(largeFixture)))
    86  		b.ReportAllocs()
    87  		b.ResetTimer()
    88  		var elem *Element
    89  		var ar *Array
    90  		var obj *Object
    91  		var onlyKeys = map[string]struct{}{
    92  			"id":   {},
    93  			"slug": {},
    94  		}
    95  		const checkErrs = false
    96  		for i := 0; i < b.N; i++ {
    97  			// Reset tape
    98  			var err error
    99  			pj, err = Parse(largeFixture, pj, WithCopyStrings(false))
   100  			if checkErrs && err != nil {
   101  				b.Fatal(err)
   102  			}
   103  			iter := pj.Iter()
   104  			elem, err = iter.FindElement(elem, "users")
   105  			if checkErrs && err != nil {
   106  				b.Fatal(err)
   107  			}
   108  			ar, err = elem.Iter.Array(ar)
   109  			if checkErrs && err != nil {
   110  				b.Fatal(err)
   111  			}
   112  			ar.ForEach(func(i Iter) {
   113  				elem, err = i.FindElement(elem, "username")
   114  				if checkErrs && err != nil {
   115  					b.Fatal(err)
   116  				}
   117  				_, _ = elem.Iter.StringBytes()
   118  			})
   119  
   120  			elem, err = iter.FindElement(elem, "topics", "topics")
   121  			if checkErrs && err != nil {
   122  				b.Fatal(err)
   123  			}
   124  			ar, err = elem.Iter.Array(ar)
   125  			if checkErrs && err != nil {
   126  				b.Fatal(err)
   127  			}
   128  			ar.ForEach(func(i Iter) {
   129  				if true {
   130  					// Use foreach...
   131  					obj, err = i.Object(obj)
   132  					if checkErrs && err != nil {
   133  						b.Fatal(err)
   134  					}
   135  					obj.ForEach(func(key []byte, i Iter) {
   136  						if string(key) == "id" {
   137  							_, err = i.Int()
   138  							if checkErrs && err != nil {
   139  								b.Fatal(err)
   140  							}
   141  						}
   142  						if string(key) == "slug" {
   143  							_, err = i.StringBytes()
   144  							if checkErrs && err != nil {
   145  								b.Fatal(err)
   146  							}
   147  						}
   148  
   149  					}, onlyKeys)
   150  				} else {
   151  					elem, err = i.FindElement(elem, "id")
   152  					if checkErrs && err != nil {
   153  						b.Fatal(err)
   154  					}
   155  					_, _ = elem.Iter.Int()
   156  					//b.Log(elem.Iter.Int())
   157  					elem, err = i.FindElement(elem, "slug")
   158  					if checkErrs && err != nil {
   159  						b.Fatal(err)
   160  					}
   161  					_, _ = elem.Iter.StringBytes()
   162  					//b.Log(elem.Iter.String())
   163  				}
   164  			})
   165  		}
   166  	})
   167  }