github.com/vmware/govmomi@v0.43.0/vim25/types/json_bench_test.go (about) 1 /* 2 Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved. 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 types 18 19 import ( 20 "bytes" 21 "os" 22 "testing" 23 24 "github.com/vmware/govmomi/vim25/json" 25 ) 26 27 // BenchmarkDecodeVirtualMachineConfigInfo illustrates the performance 28 // difference between decoding a large vminfo object when the type 29 // discriminator is the first property in the object versus the last 30 // property. 31 func BenchmarkDecodeVirtualMachineConfigInfo(b *testing.B) { 32 33 testCases := []struct { 34 name string 35 path string 36 }{ 37 { 38 name: "vm info w type name first", 39 path: "./testdata/vminfo.json", 40 }, 41 { 42 name: "vm info w type name last", 43 path: "./testdata/vminfo-typename-at-end.json", 44 }, 45 } 46 47 for _, tc := range testCases { 48 tc := tc // capture the range variable 49 b.Run(tc.name, func(b *testing.B) { 50 buf, err := os.ReadFile(tc.path) 51 if err != nil { 52 b.Fatal(err) 53 } 54 55 b.ResetTimer() 56 57 for i := 0; i < b.N; i++ { 58 59 dec := json.NewDecoder(bytes.NewReader(buf)) 60 dec.SetDiscriminator( 61 "_typeName", "_value", 62 json.DiscriminatorToTypeFunc(TypeFunc()), 63 ) 64 65 var obj interface{} 66 if err := dec.Decode(&obj); err != nil { 67 b.Fatal(err) 68 } 69 } 70 }) 71 } 72 }