1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 func TestParseProfiles(t *testing.T) {
23 name: "parsing an empty file produces empty output",
28 name: "simple valid file produces expected output",
30 some/fancy/path:42.69,44.16 2 1`,
33 FileName: "some/fancy/path",
35 Blocks: []ProfileBlock{
37 StartLine: 42, StartCol: 69,
38 EndLine: 44, EndCol: 16,
46 name: "file with syntax characters in path produces expected output",
48 some fancy:path/some,file.go:42.69,44.16 2 1`,
51 FileName: "some fancy:path/some,file.go",
53 Blocks: []ProfileBlock{
55 StartLine: 42, StartCol: 69,
56 EndLine: 44, EndCol: 16,
64 name: "file with multiple blocks in one file produces expected output",
66 some/fancy/path:42.69,44.16 2 1
67 some/fancy/path:44.16,46.3 1 0`,
70 FileName: "some/fancy/path",
72 Blocks: []ProfileBlock{
74 StartLine: 42, StartCol: 69,
75 EndLine: 44, EndCol: 16,
79 StartLine: 44, StartCol: 16,
80 EndLine: 46, EndCol: 3,
88 name: "file with multiple files produces expected output",
90 another/fancy/path:44.16,46.3 1 0
91 some/fancy/path:42.69,44.16 2 1`,
94 FileName: "another/fancy/path",
96 Blocks: []ProfileBlock{
98 StartLine: 44, StartCol: 16,
99 EndLine: 46, EndCol: 3,
100 NumStmt: 1, Count: 0,
105 FileName: "some/fancy/path",
107 Blocks: []ProfileBlock{
109 StartLine: 42, StartCol: 69,
110 EndLine: 44, EndCol: 16,
111 NumStmt: 2, Count: 1,
118 name: "intertwined files are merged correctly",
120 some/fancy/path:42.69,44.16 2 1
121 another/fancy/path:47.2,47.13 1 1
122 some/fancy/path:44.16,46.3 1 0`,
125 FileName: "another/fancy/path",
127 Blocks: []ProfileBlock{
129 StartLine: 47, StartCol: 2,
130 EndLine: 47, EndCol: 13,
131 NumStmt: 1, Count: 1,
136 FileName: "some/fancy/path",
138 Blocks: []ProfileBlock{
140 StartLine: 42, StartCol: 69,
141 EndLine: 44, EndCol: 16,
142 NumStmt: 2, Count: 1,
145 StartLine: 44, StartCol: 16,
146 EndLine: 46, EndCol: 3,
147 NumStmt: 1, Count: 0,
154 name: "duplicate blocks are merged correctly",
156 some/fancy/path:42.69,44.16 2 4
157 some/fancy/path:42.69,44.16 2 3`,
160 FileName: "some/fancy/path",
162 Blocks: []ProfileBlock{
164 StartLine: 42, StartCol: 69,
165 EndLine: 44, EndCol: 16,
166 NumStmt: 2, Count: 7,
173 name: "an invalid mode line is an error",
178 name: "a missing field is an error",
180 some/fancy/path:42.69,44.16 2`,
184 name: "a missing path field is an error",
190 name: "a non-numeric count is an error",
196 name: "an empty path is an error",
202 name: "a negative count is an error",
204 some/fancy/path:42.69,44.16 2 -1`,
209 for _, tc := range tests {
210 t.Run(tc.name, func(t *testing.T) {
211 f, err := ioutil.TempFile("", "")
213 t.Fatalf("Failed to create a temp file: %v.", err)
219 n, err := f.WriteString(tc.input)
221 t.Fatalf("Failed to write to temp file: %v", err)
223 if n < len(tc.input) {
224 t.Fatalf("Didn't write enough bytes to temp file (wrote %d, expected %d).", n, len(tc.input))
226 if err := f.Sync(); err != nil {
227 t.Fatalf("Failed to sync temp file: %v", err)
230 result, err := ParseProfiles(f.Name())
233 t.Errorf("Unexpected error: %v", err)
238 t.Errorf("Expected an error, but got value %q", stringifyProfileArray(result))
240 if !reflect.DeepEqual(result, tc.output) {
241 t.Errorf("Mismatched results.\nExpected: %s\nActual: %s", stringifyProfileArray(tc.output), stringifyProfileArray(result))
247 func stringifyProfileArray(profiles []*Profile) string {
248 deref := make([]Profile, 0, len(profiles))
249 for _, p := range profiles {
250 deref = append(deref, *p)
252 return fmt.Sprintf("%#v", deref)
255 func BenchmarkParseLine(b *testing.B) {
256 const line = "k8s.io/kubernetes/cmd/kube-controller-manager/app/options/ttlafterfinishedcontroller.go:31.73,32.14 1 1"
257 b.SetBytes(int64(len(line)))
258 for n := 0; n < b.N; n++ {