1package toml 2 3import ( 4 "bytes" 5 "encoding/json" 6 "io/ioutil" 7 "testing" 8 "time" 9 10 burntsushi "github.com/BurntSushi/toml" 11 yaml "gopkg.in/yaml.v2" 12) 13 14type benchmarkDoc struct { 15 Table struct { 16 Key string 17 Subtable struct { 18 Key string 19 } 20 Inline struct { 21 Name struct { 22 First string 23 Last string 24 } 25 Point struct { 26 X int64 27 U int64 28 } 29 } 30 } 31 String struct { 32 Basic struct { 33 Basic string 34 } 35 Multiline struct { 36 Key1 string 37 Key2 string 38 Key3 string 39 Continued struct { 40 Key1 string 41 Key2 string 42 Key3 string 43 } 44 } 45 Literal struct { 46 Winpath string 47 Winpath2 string 48 Quoted string 49 Regex string 50 Multiline struct { 51 Regex2 string 52 Lines string 53 } 54 } 55 } 56 Integer struct { 57 Key1 int64 58 Key2 int64 59 Key3 int64 60 Key4 int64 61 Underscores struct { 62 Key1 int64 63 Key2 int64 64 Key3 int64 65 } 66 } 67 Float struct { 68 Fractional struct { 69 Key1 float64 70 Key2 float64 71 Key3 float64 72 } 73 Exponent struct { 74 Key1 float64 75 Key2 float64 76 Key3 float64 77 } 78 Both struct { 79 Key float64 80 } 81 Underscores struct { 82 Key1 float64 83 Key2 float64 84 } 85 } 86 Boolean struct { 87 True bool 88 False bool 89 } 90 Datetime struct { 91 Key1 time.Time 92 Key2 time.Time 93 Key3 time.Time 94 } 95 Array struct { 96 Key1 []int64 97 Key2 []string 98 Key3 [][]int64 99 // TODO: Key4 not supported by go-toml's Unmarshal 100 Key5 []int64 101 Key6 []int64 102 } 103 Products []struct { 104 Name string 105 Sku int64 106 Color string 107 } 108 Fruit []struct { 109 Name string 110 Physical struct { 111 Color string 112 Shape string 113 Variety []struct { 114 Name string 115 } 116 } 117 } 118} 119 120func BenchmarkParseToml(b *testing.B) { 121 fileBytes, err := ioutil.ReadFile("benchmark.toml") 122 if err != nil { 123 b.Fatal(err) 124 } 125 b.ResetTimer() 126 for i := 0; i < b.N; i++ { 127 _, err := LoadReader(bytes.NewReader(fileBytes)) 128 if err != nil { 129 b.Fatal(err) 130 } 131 } 132} 133 134func BenchmarkUnmarshalToml(b *testing.B) { 135 bytes, err := ioutil.ReadFile("benchmark.toml") 136 if err != nil { 137 b.Fatal(err) 138 } 139 b.ResetTimer() 140 for i := 0; i < b.N; i++ { 141 target := benchmarkDoc{} 142 err := Unmarshal(bytes, &target) 143 if err != nil { 144 b.Fatal(err) 145 } 146 } 147} 148 149func BenchmarkUnmarshalBurntSushiToml(b *testing.B) { 150 bytes, err := ioutil.ReadFile("benchmark.toml") 151 if err != nil { 152 b.Fatal(err) 153 } 154 b.ResetTimer() 155 for i := 0; i < b.N; i++ { 156 target := benchmarkDoc{} 157 err := burntsushi.Unmarshal(bytes, &target) 158 if err != nil { 159 b.Fatal(err) 160 } 161 } 162} 163 164func BenchmarkUnmarshalJson(b *testing.B) { 165 bytes, err := ioutil.ReadFile("benchmark.json") 166 if err != nil { 167 b.Fatal(err) 168 } 169 b.ResetTimer() 170 for i := 0; i < b.N; i++ { 171 target := benchmarkDoc{} 172 err := json.Unmarshal(bytes, &target) 173 if err != nil { 174 b.Fatal(err) 175 } 176 } 177} 178 179func BenchmarkUnmarshalYaml(b *testing.B) { 180 bytes, err := ioutil.ReadFile("benchmark.yml") 181 if err != nil { 182 b.Fatal(err) 183 } 184 b.ResetTimer() 185 for i := 0; i < b.N; i++ { 186 target := benchmarkDoc{} 187 err := yaml.Unmarshal(bytes, &target) 188 if err != nil { 189 b.Fatal(err) 190 } 191 } 192} 193