File: set_test.go

package info (click to toggle)
golang-github-ovn-org-libovsdb 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 1,440 kB
  • sloc: makefile: 52; sh: 14
file content (171 lines) | stat: -rw-r--r-- 5,568 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ovsdb

import (
	"encoding/json"
	"fmt"
	"reflect"
	"strings"
	"testing"
)

var testUUIDs = []string{
	"38d9fa08-8e97-4402-9347-a610773b91cb",
	"aab50e87-1410-4c44-8c43-58aed178c833",
	"445d365f-1e5b-44ee-86e7-41605858df83",
	"a132ac6f-8b95-483b-8595-5453703e0617",
	"5e617059-c157-47ff-a4ea-2bc3f163b198",
	"faceebeb-4b52-4721-a879-c9f70e3f58a6",
	"1ff23dbb-41d1-423f-acbc-94b06c508926",
	"7e191fdb-228d-4bf3-9db4-883c8705ac7e",
}

func benchmarkSetMarshalJSON(s interface{}, b *testing.B) {
	testSet, err := NewOvsSet(s)
	if err != nil {
		b.Fatal(err)
	}
	for n := 0; n < b.N; n++ {
		_, err := json.Marshal(testSet)
		if err != nil {
			b.Fatal(err)
		}
	}
}
func BenchmarkSetMarshalJSONString1(b *testing.B) { benchmarkSetMarshalJSON("foo", b) }
func BenchmarkSetMarshalJSONString2(b *testing.B) {
	benchmarkSetMarshalJSON([]string{"foo", "bar"}, b)
}
func BenchmarkSetMarshalJSONString3(b *testing.B) {
	benchmarkSetMarshalJSON([]string{"foo", "bar", "baz"}, b)
}
func BenchmarkSetMarshalJSONString5(b *testing.B) {
	benchmarkSetMarshalJSON([]string{"foo", "bar", "baz", "quux", "foofoo"}, b)
}
func BenchmarkSetMarshalJSONString8(b *testing.B) {
	benchmarkSetMarshalJSON([]string{"foo", "bar", "baz", "quux", "foofoo", "foobar", "foobaz", "fooquux"}, b)
}

func BenchmarkSetMarshalJSONInt1(b *testing.B) { benchmarkSetMarshalJSON(1, b) }
func BenchmarkSetMarshalJSONInt2(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1, 2}, b)
}
func BenchmarkSetMarshalJSONInt3(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1, 2, 3}, b)
}
func BenchmarkSetMarshalJSONInt5(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1, 2, 3, 4, 5}, b)
}
func BenchmarkSetMarshalJSONInt8(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1, 2, 3, 4, 5, 6, 7, 8}, b)
}

func BenchmarkSetMarshalJSONFloat1(b *testing.B) { benchmarkSetMarshalJSON(1.0, b) }
func BenchmarkSetMarshalJSONFloat2(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1.0, 2.0}, b)
}
func BenchmarkSetMarshalJSONFloat3(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1.0, 2.0, 3.0}, b)
}
func BenchmarkSetMarshalJSONFloat5(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1.0, 2.0, 3.0, 4.0, 5.0}, b)
}
func BenchmarkSetMarshalJSONFloat8(b *testing.B) {
	benchmarkSetMarshalJSON([]int{1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0}, b)
}

func BenchmarkSetMarshalJSONUUID1(b *testing.B) { benchmarkSetMarshalJSON(testUUIDs[0], b) }
func BenchmarkSetMarshalJSONUUID2(b *testing.B) {
	benchmarkSetMarshalJSON(testUUIDs[0:2], b)
}
func BenchmarkSetMarshalJSONUUID3(b *testing.B) {
	benchmarkSetMarshalJSON(testUUIDs[0:3], b)
}
func BenchmarkSetMarshalJSONUUID5(b *testing.B) {
	benchmarkSetMarshalJSON(testUUIDs[0:5], b)
}
func BenchmarkSetMarshalJSONUUID8(b *testing.B) {
	benchmarkSetMarshalJSON(testUUIDs, b)
}

func benchmarkSetUnmarshalJSON(data []byte, b *testing.B) {
	for n := 0; n < b.N; n++ {
		var s OvsSet
		err := json.Unmarshal(data, &s)
		if err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkSetUnmarshalJSONString1(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`"foo"`), b)
}

func BenchmarkSetUnmarshalJSONString2(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`[ "set", ["foo","bar"] ]`), b)
}

func BenchmarkSetUnmarshalJSONString3(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`[ "set", ["foo","bar","baz"] ]`), b)
}

func BenchmarkSetUnmarshalJSONString5(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`[ "set", ["foo","bar","baz","quuz","foofoo"] ]`), b)
}

func BenchmarkSetUnmarshalJSONString8(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`[ "set", ["foo","bar","baz","quuz","foofoo","foobar","foobaz","fooquuz"] ]`), b)
}

func BenchmarkSetUnmarshalJSONInt1(b *testing.B) { benchmarkSetUnmarshalJSON([]byte("1"), b) }
func BenchmarkSetUnmarshalJSONInt2(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1, 2]]`), b)
}
func BenchmarkSetUnmarshalJSONInt3(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1, 2, 3]]`), b)
}
func BenchmarkSetUnmarshalJSONInt5(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1, 2, 3, 4, 5]]`), b)
}
func BenchmarkSetUnmarshalJSONInt8(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1, 2, 3, 4, 5, 6, 7, 8]]`), b)
}

func BenchmarkSetUnmarshalJSONFloat1(b *testing.B) { benchmarkSetUnmarshalJSON([]byte(`1.0`), b) }
func BenchmarkSetUnmarshalJSONFloat2(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1.0, 2.0]]`), b)
}
func BenchmarkSetUnmarshalJSONFloat3(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1.0, 2.0, 3.0]]`), b)
}
func BenchmarkSetUnmarshalJSONFloat5(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1.0, 2.0, 3.0, 4.0, 5.0]]`), b)
}
func BenchmarkSetUnmarshalJSONFloat8(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`["set", [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]]`), b)
}

func BenchmarkSetUnmarshalJSONUUID1(b *testing.B) {
	benchmarkSetUnmarshalJSON([]byte(`"`+testUUIDs[0]+`"`), b)
}
func BenchmarkSetUnmarshalJSONUUID2(b *testing.B) {
	benchmarkSetUnmarshalJSON(setify(testUUIDs[0:2]), b)
}
func BenchmarkSetUnmarshalJSONUUID3(b *testing.B) {
	benchmarkSetUnmarshalJSON(setify(testUUIDs[0:3]), b)
}
func BenchmarkSetUnmarshalJSONUUID5(b *testing.B) {
	benchmarkSetUnmarshalJSON(setify(testUUIDs[0:5]), b)
}
func BenchmarkSetUnmarshalJSONUUID8(b *testing.B) {
	benchmarkSetUnmarshalJSON(setify(testUUIDs), b)
}

func setify(i interface{}) []byte {
	var s []string
	iv := reflect.ValueOf(i)
	for j := 0; j < iv.Len(); j++ {
		s = append(s, fmt.Sprintf("%v", iv.Index(j)))
	}
	return []byte(fmt.Sprintf(`[ "set", [ "%s" ]]`, strings.Join(s, `","`)))
}