-
Notifications
You must be signed in to change notification settings - Fork 7
/
gen.py
executable file
·328 lines (275 loc) · 8.96 KB
/
gen.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python3
"""
Copyright 2022
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
"""generate
Generates several of the go source files.
"""
import subprocess
_HEADER = """/*
Copyright 2022
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// !! Generated code -- do not modify !!
//
package mem_test
"""
_INSTRUMENTED_TYPES = [
{
"type": "int",
"nonz": "nonZeroRandInt(_int_size)",
},
{
"type": "int8",
"nonz": "int8(nonZeroRandInt(8))",
},
{
"type": "int16",
"nonz": "int16(nonZeroRandInt(16))",
},
{
"type": "int32",
"nonz": "int32(nonZeroRandInt(32))",
},
{
"type": "int64",
"nonz": "int64(nonZeroRandInt(64))",
"nwln": 2,
},
{
"type": "uint",
"nonz": "uint(nonZeroRandInt(_int_size))",
},
{
"type": "uint8",
"nonz": "uint8(nonZeroRandInt(8))",
},
{
"type": "uint16",
"nonz": "uint16(nonZeroRandInt(16))",
},
{
"type": "uint32",
"nonz": "uint32(nonZeroRandInt(32))",
},
{
"type": "uint64",
"nonz": "uint64(nonZeroRandInt(64))",
"nwln": 2,
},
{
"type": "float32",
"nonz": "float32(nonZeroRandInt(32))",
},
{
"type": "float64",
"nonz": "float64(nonZeroRandInt(64))",
"nwln": 2,
},
{
"type": "complex64",
"nonz": "complex(float32(nonZeroRandInt(32)), float32(nonZeroRandInt(32)))",
},
{
"type": "complex128",
"nonz": "complex(float64(nonZeroRandInt(64)), float64(nonZeroRandInt(64)))",
"nwln": 2,
},
{
"type": "byte",
"nonz": "byte(nonZeroRandInt(8))",
},
{
"type": "bool",
"nonz": "nonConstBoolTrue()",
},
{
"type": "rune",
"nonz": "rune(nonZeroRandInt(32))",
},
{
"type": "string",
"nonz": "nonZeroString(50)",
"nwln": 2,
},
{
"name": "struct_int32_int32",
"type": "struct{ a, b int32 }",
"nonz": "{ a: int32(nonZeroRandInt(32)), b: int32(nonZeroRandInt(32)) }",
},
{
"name": "struct_int32_int64",
"type": "struct { a int32; b int64 }",
"nonz": "{ a: int32(nonZeroRandInt(32)), b: int64(nonZeroRandInt(64)) }",
},
{
"name": "struct_array_bytes_7",
"type": "struct{ a [7]byte }",
"nonz": "{ a: [7]byte{byte(nonZeroRandInt(8)), byte(nonZeroRandInt(8)), byte(nonZeroRandInt(8)), byte(nonZeroRandInt(8)), byte(nonZeroRandInt(8)), byte(nonZeroRandInt(8)), byte(nonZeroRandInt(8)) }}",
},
{
"name": "struct_byte_7",
"type": "struct{ a, b, c, d, e, f, g byte }",
"nonz": "{ a: byte(nonZeroRandInt(8)), b: byte(nonZeroRandInt(8)), c: byte(nonZeroRandInt(8)), d: byte(nonZeroRandInt(8)), e: byte(nonZeroRandInt(8)), f: byte(nonZeroRandInt(8)), g: byte(nonZeroRandInt(8)) }",
"nwln": 2,
},
]
_TYPES_TEST_PATH = "types_test.go"
_MEM_TEST_PATH = "bench_test.go"
_is_struct = lambda t: t["type"].startswith("struct")
_non_struct_types = [t for t in _INSTRUMENTED_TYPES if not _is_struct(t)]
_struct_types = [t for t in _INSTRUMENTED_TYPES if _is_struct(t)]
def go_fmt(p):
# Format the file.
subprocess.run(
["go", "fmt", p],
capture_output=True,
check=True,
)
def gen_types():
def _print(
f,
it,
is_zero=False,
is_struct=False,
is_wrapped=False,
is_benchmark_global=False,
):
t = it["type"]
# Define the variable' name.
f.write("\t_")
if is_wrapped:
f.write("struct_")
f.write(t if "name" not in it else it["name"])
if not is_zero:
f.write("_n")
if is_benchmark_global:
f.write("_benchmark")
f.write(" ")
# The variables type declaration depends on whether this is a non-zero
# value and if the type is a struct.
if not is_zero and not is_benchmark_global and (is_struct or is_wrapped):
f.write(" = ")
# Define the variable' type.
if is_wrapped:
f.write("struct{ a ")
f.write(t)
if is_wrapped:
f.write(" }")
if not is_zero:
# Define the variable's value.
if not is_struct and not is_wrapped:
f.write(" = ")
if is_wrapped:
f.write("{ a: ")
f.write(it["nonz"])
if is_wrapped:
f.write(" }")
f.write("\n" * (1 if "nwln" not in it else it["nwln"]))
with open(_TYPES_TEST_PATH, "w") as f:
f.write(_HEADER)
f.write("var (\n")
# Define the variables for the zero values for the non-struct types.
for it in _non_struct_types:
_print(f, it, is_zero=True)
# Define the variables for the zero values for the non-struct types wrapped
# by a struct.
for it in _non_struct_types:
_print(f, it, is_zero=True, is_wrapped=True)
# Define the variables for the zero-values for the struct types.
for it in _struct_types:
_print(f, it, is_zero=True, is_struct=True)
# Define the variables for the non-zero values for the non-struct types.
for it in _non_struct_types:
_print(f, it)
# Define the variables for the non-zero values for the non-struct types
# wrapped by a struct.
for it in _non_struct_types:
_print(f, it, is_wrapped=True)
# Define the variables for the non-zero values for the struct types.
for it in _struct_types:
_print(f, it, is_struct=True)
# Define the variables for the zero values for the non-struct types
# used by the typed benchmarks.
for it in _non_struct_types:
_print(f, it, is_zero=True, is_benchmark_global=True)
# Define the variables for the zero values for the non-struct types
# wrapped by a struct and used by the typed benchmarks.
for it in _non_struct_types:
_print(f, it, is_zero=True, is_wrapped=True, is_benchmark_global=True)
# Define the variables for the zero-values for the struct types
# used by the typed benchmarks.
for it in _struct_types:
_print(f, it, is_zero=True, is_struct=True, is_benchmark_global=True)
f.write(")\n\n")
go_fmt(_TYPES_TEST_PATH)
def gen_bench():
s = """
b.Run("{0}", func(b *testing.B) {{
b.Logf("real(T)=%T", _{0})
b.Run("0", func(b *testing.B) {{
b.Run("h", func(b *testing.B) {{
for j := 0; j < b.N; j++ {{
_i = _{0}
}}
}})
b.Run("s", func(b *testing.B) {{
for j := 0; j < b.N; j++ {{
_{0}_benchmark = _{0}
}}
}})
}})
b.Run("n", func(b *testing.B) {{
b.Run("h", func(b *testing.B) {{
for j := 0; j < b.N; j++ {{
_i = _{0}_n
}}
}})
b.Run("s", func(b *testing.B) {{
for j := 0; j < b.N; j++ {{
_{0}_benchmark = _{0}_n
}}
}})
}})
}})
"""
def _print(f, it, is_wrapped=False):
t = it["type"] if "name" not in it else it["name"]
if is_wrapped:
t = "struct_" + t
f.write(s.format(t))
with open(_MEM_TEST_PATH, "w") as f:
f.write(_HEADER)
f.write('\nimport "testing"\n')
f.write("func BenchmarkMem(b *testing.B) {\n")
# Benchmarks for the non-struct types.
for it in _non_struct_types:
_print(f, it)
# Benchmarks for the non-struct types wrapped by a struct.
for it in _non_struct_types:
_print(f, it, is_wrapped=True)
# Benchmarks for the struct types.
for it in _struct_types:
_print(f, it)
f.write("}\n")
go_fmt(_MEM_TEST_PATH)
gen_types()
gen_bench()