| 1 | // Copyright 2014 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package report |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "fmt" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "regexp" |
| 23 | "runtime" |
| 24 | "slices" |
| 25 | "strings" |
| 26 | "testing" |
| 27 | "time" |
| 28 | |
| 29 | "github.com/google/pprof/internal/binutils" |
| 30 | "github.com/google/pprof/internal/graph" |
| 31 | "github.com/google/pprof/internal/proftest" |
| 32 | "github.com/google/pprof/profile" |
| 33 | ) |
| 34 | |
| 35 | type testcase struct { |
| 36 | rpt *Report |
| 37 | want string |
| 38 | } |
| 39 | |
| 40 | func TestSource(t *testing.T) { |
| 41 | const path = "testdata/" |
| 42 | |
| 43 | sampleValue1 := func(v []int64) int64 { |
| 44 | return v[1] |
| 45 | } |
| 46 | |
| 47 | for _, tc := range []testcase{ |
| 48 | { |
| 49 | rpt: New( |
| 50 | testProfile.Copy(), |
| 51 | &Options{ |
| 52 | OutputFormat: List, |
| 53 | Symbol: regexp.MustCompile(`.`), |
| 54 | TrimPath: "/some/path", |
| 55 | |
| 56 | SampleValue: sampleValue1, |
| 57 | SampleUnit: testProfile.SampleType[1].Unit, |
| 58 | }, |
| 59 | ), |
| 60 | want: path + "source.rpt", |
| 61 | }, |
| 62 | { |
| 63 | rpt: New( |
| 64 | testProfile.Copy(), |
| 65 | &Options{ |
| 66 | OutputFormat: Dot, |
| 67 | CallTree: true, |
| 68 | Symbol: regexp.MustCompile(`.`), |
| 69 | TrimPath: "/some/path", |
| 70 | |
| 71 | SampleValue: sampleValue1, |
| 72 | SampleUnit: testProfile.SampleType[1].Unit, |
| 73 | }, |
| 74 | ), |
| 75 | want: path + "source.dot", |
| 76 | }, |
| 77 | } { |
| 78 | var b bytes.Buffer |
| 79 | if err := Generate(&b, tc.rpt, &binutils.Binutils{}); err != nil { |
| 80 | t.Fatalf("%s: %v", tc.want, err) |
| 81 | } |
| 82 | |
| 83 | gold, err := os.ReadFile(tc.want) |
| 84 | if err != nil { |
| 85 | t.Fatalf("%s: %v", tc.want, err) |
| 86 | } |
| 87 | if runtime.GOOS == "windows" { |
| 88 | if tc.rpt.options.OutputFormat == Dot { |
| 89 | // The .dot test has the paths inside strings, so \ must be escaped. |
| 90 | gold = bytes.ReplaceAll(gold, []byte("testdata/"), []byte(`testdata\\`)) |
| 91 | } else { |
| 92 | gold = bytes.ReplaceAll(gold, []byte("testdata/"), []byte(`testdata\`)) |
| 93 | } |
| 94 | } |
| 95 | if string(b.String()) != string(gold) { |
| 96 | d, err := proftest.Diff(gold, b.Bytes()) |
| 97 | if err != nil { |
| 98 | t.Fatalf("%s: %v", "source", err) |
| 99 | } |
| 100 | t.Error("source" + "\n" + string(d) + "\n" + "gold:\n" + tc.want) |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // TestFilter ensures that commands with a regexp filter argument return an |
| 106 | // error if there are no results. |
| 107 | func TestFilter(t *testing.T) { |
| 108 | const filter = "doesNotExist" |
| 109 | |
| 110 | tests := []struct { |
| 111 | name string |
| 112 | format int |
| 113 | }{ |
| 114 | { |
| 115 | name: "list", |
| 116 | format: List, |
| 117 | }, |
| 118 | { |
| 119 | name: "disasm", |
| 120 | format: Dis, |
| 121 | }, |
| 122 | { |
| 123 | // N.B. Tree with a Symbol is "peek". |
| 124 | name: "peek", |
| 125 | format: Tree, |
| 126 | }, |
| 127 | } |
| 128 | for _, tc := range tests { |
| 129 | t.Run(tc.name, func(t *testing.T) { |
| 130 | rpt := New(testProfile.Copy(), &Options{ |
| 131 | OutputFormat: tc.format, |
| 132 | Symbol: regexp.MustCompile(filter), |
| 133 | SampleValue: func(v []int64) int64 { return v[1] }, |
| 134 | SampleUnit: testProfile.SampleType[1].Unit, |
| 135 | }) |
| 136 | |
| 137 | var buf bytes.Buffer |
| 138 | err := Generate(&buf, rpt, &binutils.Binutils{}) |
| 139 | if err == nil { |
| 140 | t.Fatalf("Generate got nil, want error; buf = %s", buf.String()) |
| 141 | } |
| 142 | if !strings.Contains(err.Error(), filter) { |
| 143 | t.Errorf("Error got %v, want it to contain %q", err, filter) |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // testM contains mappings for fake profiles used in tests. |
| 150 | var testM = []*profile.Mapping{ |
| 151 | { |
| 152 | ID: 1, |
| 153 | HasFunctions: true, |
| 154 | HasFilenames: true, |
| 155 | HasLineNumbers: true, |
| 156 | HasInlineFrames: true, |
| 157 | }, |
| 158 | } |
| 159 | |
| 160 | // testF contains functions for fake profiles used in tests. |
| 161 | var testF = []*profile.Function{ |
| 162 | { |
| 163 | ID: 1, |
| 164 | Name: "main", |
| 165 | Filename: "testdata/source1", |
| 166 | }, |
| 167 | { |
| 168 | ID: 2, |
| 169 | Name: "foo", |
| 170 | Filename: "testdata/source1", |
| 171 | }, |
| 172 | { |
| 173 | ID: 3, |
| 174 | Name: "bar", |
| 175 | Filename: "testdata/source1", |
| 176 | }, |
| 177 | { |
| 178 | ID: 4, |
| 179 | Name: "tee", |
| 180 | Filename: "/some/path/testdata/source2", |
| 181 | }, |
| 182 | } |
| 183 | |
| 184 | // testL contains locations for fake profiles used in tests. |
| 185 | var testL = []*profile.Location{ |
| 186 | { |
| 187 | ID: 1, |
| 188 | Mapping: testM[0], |
| 189 | Line: []profile.Line{ |
| 190 | { |
| 191 | Function: testF[0], |
| 192 | Line: 2, |
| 193 | Column: 2, |
| 194 | }, |
| 195 | }, |
| 196 | }, |
| 197 | { |
| 198 | ID: 2, |
| 199 | Mapping: testM[0], |
| 200 | Line: []profile.Line{ |
| 201 | { |
| 202 | Function: testF[1], |
| 203 | Line: 4, |
| 204 | Column: 4, |
| 205 | }, |
| 206 | }, |
| 207 | }, |
| 208 | { |
| 209 | ID: 3, |
| 210 | Mapping: testM[0], |
| 211 | Line: []profile.Line{ |
| 212 | { |
| 213 | Function: testF[2], |
| 214 | Line: 10, |
| 215 | }, |
| 216 | }, |
| 217 | }, |
| 218 | { |
| 219 | ID: 4, |
| 220 | Mapping: testM[0], |
| 221 | Line: []profile.Line{ |
| 222 | { |
| 223 | Function: testF[3], |
| 224 | Line: 2, |
| 225 | }, |
| 226 | }, |
| 227 | }, |
| 228 | { |
| 229 | ID: 5, |
| 230 | Mapping: testM[0], |
| 231 | Line: []profile.Line{ |
| 232 | { |
| 233 | Function: testF[3], |
| 234 | Line: 8, |
| 235 | }, |
| 236 | }, |
| 237 | }, |
| 238 | { |
| 239 | ID: 6, |
| 240 | Mapping: testM[0], |
| 241 | Line: []profile.Line{ |
| 242 | { |
| 243 | Function: testF[3], |
| 244 | Line: 7, |
| 245 | }, |
| 246 | { |
| 247 | Function: testF[2], |
| 248 | Line: 6, |
| 249 | }, |
| 250 | }, |
| 251 | }, |
| 252 | } |
| 253 | |
| 254 | // testSample returns a profile sample with specified value and stack. |
| 255 | // Note: callees come first in sample stacks. |
| 256 | func testSample(value int64, locs ...*profile.Location) *profile.Sample { |
| 257 | return &profile.Sample{ |
| 258 | Value: []int64{value}, |
| 259 | Location: locs, |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // makeTestProfile returns a profile with specified samples that uses testL/testF/testM |
| 264 | // (defined in report_test.go). |
| 265 | func makeTestProfile(samples ...*profile.Sample) *profile.Profile { |
| 266 | return &profile.Profile{ |
| 267 | SampleType: []*profile.ValueType{{Type: "samples", Unit: "count"}}, |
| 268 | Sample: samples, |
| 269 | Location: testL, |
| 270 | Function: testF, |
| 271 | Mapping: testM, |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // testProfile contains a fake profile used in tests. |
| 276 | // Various report methods modify profiles so tests should operate on testProfile.Copy(). |
| 277 | var testProfile = &profile.Profile{ |
| 278 | PeriodType: &profile.ValueType{Type: "cpu", Unit: "millisecond"}, |
| 279 | Period: 10, |
| 280 | DurationNanos: 10e9, |
| 281 | SampleType: []*profile.ValueType{ |
| 282 | {Type: "samples", Unit: "count"}, |
| 283 | {Type: "cpu", Unit: "cycles"}, |
| 284 | }, |
| 285 | Sample: []*profile.Sample{ |
| 286 | { |
| 287 | Location: []*profile.Location{testL[0]}, |
| 288 | Value: []int64{1, 1}, |
| 289 | }, |
| 290 | { |
| 291 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 292 | Value: []int64{1, 10}, |
| 293 | }, |
| 294 | { |
| 295 | Location: []*profile.Location{testL[4], testL[2], testL[0]}, |
| 296 | Value: []int64{1, 100}, |
| 297 | }, |
| 298 | { |
| 299 | Location: []*profile.Location{testL[3], testL[0]}, |
| 300 | Value: []int64{1, 1000}, |
| 301 | }, |
| 302 | { |
| 303 | Location: []*profile.Location{testL[4], testL[3], testL[0]}, |
| 304 | Value: []int64{1, 10000}, |
| 305 | }, |
| 306 | }, |
| 307 | Location: testL, |
| 308 | Function: testF, |
| 309 | Mapping: testM, |
| 310 | } |
| 311 | |
| 312 | func TestDisambiguation(t *testing.T) { |
| 313 | parent1 := &graph.Node{Info: graph.NodeInfo{Name: "parent1"}} |
| 314 | parent2 := &graph.Node{Info: graph.NodeInfo{Name: "parent2"}} |
| 315 | child1 := &graph.Node{Info: graph.NodeInfo{Name: "child"}, Function: parent1} |
| 316 | child2 := &graph.Node{Info: graph.NodeInfo{Name: "child"}, Function: parent2} |
| 317 | child3 := &graph.Node{Info: graph.NodeInfo{Name: "child"}, Function: parent1} |
| 318 | sibling := &graph.Node{Info: graph.NodeInfo{Name: "sibling"}, Function: parent1} |
| 319 | |
| 320 | n := []*graph.Node{parent1, parent2, child1, child2, child3, sibling} |
| 321 | |
| 322 | wanted := map[*graph.Node]string{ |
| 323 | parent1: "parent1", |
| 324 | parent2: "parent2", |
| 325 | child1: "child [1/2]", |
| 326 | child2: "child [2/2]", |
| 327 | child3: "child [1/2]", |
| 328 | sibling: "sibling", |
| 329 | } |
| 330 | |
| 331 | g := &graph.Graph{Nodes: n} |
| 332 | |
| 333 | names := getDisambiguatedNames(g) |
| 334 | |
| 335 | for node, want := range wanted { |
| 336 | if got := names[node]; got != want { |
| 337 | t.Errorf("name %s, got %s, want %s", node.Info.Name, got, want) |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | func TestFunctionMap(t *testing.T) { |
| 343 | |
| 344 | fm := make(functionMap) |
| 345 | nodes := []graph.NodeInfo{ |
| 346 | {Name: "fun1"}, |
| 347 | {Name: "fun2", File: "filename"}, |
| 348 | {Name: "fun1"}, |
| 349 | {Name: "fun2", File: "filename2"}, |
| 350 | } |
| 351 | |
| 352 | want := []struct { |
| 353 | wantFunction profile.Function |
| 354 | wantAdded bool |
| 355 | }{ |
| 356 | {profile.Function{ID: 1, Name: "fun1"}, true}, |
| 357 | {profile.Function{ID: 2, Name: "fun2", Filename: "filename"}, true}, |
| 358 | {profile.Function{ID: 1, Name: "fun1"}, false}, |
| 359 | {profile.Function{ID: 3, Name: "fun2", Filename: "filename2"}, true}, |
| 360 | } |
| 361 | |
| 362 | for i, tc := range nodes { |
| 363 | gotFunc, gotAdded := fm.findOrAdd(tc) |
| 364 | if got, want := gotFunc, want[i].wantFunction; *got != want { |
| 365 | t.Errorf("%d: got %v, want %v", i, got, want) |
| 366 | } |
| 367 | if got, want := gotAdded, want[i].wantAdded; got != want { |
| 368 | t.Errorf("%d: got %v, want %v", i, got, want) |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | func TestLegendActiveFilters(t *testing.T) { |
| 374 | activeFilterInput := []string{ |
| 375 | "focus=123|456|789|101112|131415|161718|192021|222324|252627|282930|313233|343536|363738|acbdefghijklmnop", |
| 376 | "show=short filter", |
| 377 | } |
| 378 | expectedLegendActiveFilter := []string{ |
| 379 | "Active filters:", |
| 380 | " focus=123|456|789|101112|131415|161718|192021|222324|252627|282930|313233|343536…", |
| 381 | " show=short filter", |
| 382 | } |
| 383 | legendActiveFilter := legendActiveFilters(activeFilterInput) |
| 384 | if len(legendActiveFilter) != len(expectedLegendActiveFilter) { |
| 385 | t.Errorf("wanted length %v got length %v", len(expectedLegendActiveFilter), len(legendActiveFilter)) |
| 386 | } |
| 387 | for i := range legendActiveFilter { |
| 388 | if legendActiveFilter[i] != expectedLegendActiveFilter[i] { |
| 389 | t.Errorf("%d: want \"%v\", got \"%v\"", i, expectedLegendActiveFilter[i], legendActiveFilter[i]) |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | func TestComputeTotal(t *testing.T) { |
| 395 | p1 := testProfile.Copy() |
| 396 | p1.Sample = []*profile.Sample{ |
| 397 | { |
| 398 | Location: []*profile.Location{testL[0]}, |
| 399 | Value: []int64{1, 1}, |
| 400 | }, |
| 401 | { |
| 402 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 403 | Value: []int64{1, 10}, |
| 404 | }, |
| 405 | { |
| 406 | Location: []*profile.Location{testL[4], testL[2], testL[0]}, |
| 407 | Value: []int64{1, 100}, |
| 408 | }, |
| 409 | } |
| 410 | |
| 411 | p2 := testProfile.Copy() |
| 412 | p2.Sample = []*profile.Sample{ |
| 413 | { |
| 414 | Location: []*profile.Location{testL[0]}, |
| 415 | Value: []int64{1, 1}, |
| 416 | }, |
| 417 | { |
| 418 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 419 | Value: []int64{1, -10}, |
| 420 | }, |
| 421 | { |
| 422 | Location: []*profile.Location{testL[4], testL[2], testL[0]}, |
| 423 | Value: []int64{1, 100}, |
| 424 | }, |
| 425 | } |
| 426 | |
| 427 | p3 := testProfile.Copy() |
| 428 | p3.Sample = []*profile.Sample{ |
| 429 | { |
| 430 | Location: []*profile.Location{testL[0]}, |
| 431 | Value: []int64{10000, 1}, |
| 432 | }, |
| 433 | { |
| 434 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 435 | Value: []int64{-10, 3}, |
| 436 | Label: map[string][]string{"pprof::base": {"true"}}, |
| 437 | }, |
| 438 | { |
| 439 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 440 | Value: []int64{1000, -10}, |
| 441 | }, |
| 442 | { |
| 443 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 444 | Value: []int64{-9000, 3}, |
| 445 | Label: map[string][]string{"pprof::base": {"true"}}, |
| 446 | }, |
| 447 | { |
| 448 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 449 | Value: []int64{-1, 3}, |
| 450 | Label: map[string][]string{"pprof::base": {"true"}}, |
| 451 | }, |
| 452 | { |
| 453 | Location: []*profile.Location{testL[4], testL[2], testL[0]}, |
| 454 | Value: []int64{100, 100}, |
| 455 | }, |
| 456 | { |
| 457 | Location: []*profile.Location{testL[2], testL[1], testL[0]}, |
| 458 | Value: []int64{100, 3}, |
| 459 | Label: map[string][]string{"pprof::base": {"true"}}, |
| 460 | }, |
| 461 | } |
| 462 | |
| 463 | testcases := []struct { |
| 464 | desc string |
| 465 | prof *profile.Profile |
| 466 | value, meanDiv func(v []int64) int64 |
| 467 | wantTotal int64 |
| 468 | }{ |
| 469 | { |
| 470 | desc: "no diff base, all positive values, index 1", |
| 471 | prof: p1, |
| 472 | value: func(v []int64) int64 { |
| 473 | return v[0] |
| 474 | }, |
| 475 | wantTotal: 3, |
| 476 | }, |
| 477 | { |
| 478 | desc: "no diff base, all positive values, index 2", |
| 479 | prof: p1, |
| 480 | value: func(v []int64) int64 { |
| 481 | return v[1] |
| 482 | }, |
| 483 | wantTotal: 111, |
| 484 | }, |
| 485 | { |
| 486 | desc: "no diff base, some negative values", |
| 487 | prof: p2, |
| 488 | value: func(v []int64) int64 { |
| 489 | return v[1] |
| 490 | }, |
| 491 | wantTotal: 111, |
| 492 | }, |
| 493 | { |
| 494 | desc: "diff base, some negative values", |
| 495 | prof: p3, |
| 496 | value: func(v []int64) int64 { |
| 497 | return v[0] |
| 498 | }, |
| 499 | wantTotal: 9111, |
| 500 | }, |
| 501 | } |
| 502 | |
| 503 | for _, tc := range testcases { |
| 504 | t.Run(tc.desc, func(t *testing.T) { |
| 505 | if gotTotal := computeTotal(tc.prof, tc.value, tc.meanDiv); gotTotal != tc.wantTotal { |
| 506 | t.Errorf("got total %d, want %v", gotTotal, tc.wantTotal) |
| 507 | } |
| 508 | }) |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | func TestPrintAssemblyErrorMessage(t *testing.T) { |
| 513 | profile := readProfile(filepath.Join("testdata", "sample.cpu"), t) |
| 514 | |
| 515 | for _, tc := range []struct { |
| 516 | desc string |
| 517 | symbol string |
| 518 | want string |
| 519 | }{ |
| 520 | { |
| 521 | desc: "no matched symbol in binary", |
| 522 | symbol: "symbol-not-exist", |
| 523 | want: "no matches found for regexp symbol-not-exist", |
| 524 | }, |
| 525 | { |
| 526 | desc: "no matched address in binary", |
| 527 | symbol: "0xffffaaaa", |
| 528 | want: "no matches found for address 0xffffaaaa", |
| 529 | }, |
| 530 | { |
| 531 | desc: "matched address in binary but not in the profile", |
| 532 | symbol: "0x400000", |
| 533 | want: "address 0x400000 found in binary, but the corresponding symbols do not have samples in the profile", |
| 534 | }, |
| 535 | } { |
| 536 | rpt := New( |
| 537 | profile.Copy(), |
| 538 | &Options{ |
| 539 | OutputFormat: List, |
| 540 | Symbol: regexp.MustCompile(tc.symbol), |
| 541 | SampleValue: func(v []int64) int64 { |
| 542 | return v[1] |
| 543 | }, |
| 544 | SampleUnit: profile.SampleType[1].Unit, |
| 545 | }, |
| 546 | ) |
| 547 | |
| 548 | if err := PrintAssembly(os.Stdout, rpt, &binutils.Binutils{}, -1); err == nil || err.Error() != tc.want { |
| 549 | t.Errorf(`Got "%v", want %q`, err, tc.want) |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | func TestDocURL(t *testing.T) { |
| 555 | type testCase struct { |
| 556 | input string |
| 557 | want string |
| 558 | } |
| 559 | for name, c := range map[string]testCase{ |
| 560 | "empty": {"", ""}, |
| 561 | "http": {"http://example.com/pprof-help", "http://example.com/pprof-help"}, |
| 562 | "https": {"https://example.com/pprof-help", "https://example.com/pprof-help"}, |
| 563 | "relative": {"/foo", ""}, |
| 564 | "nonhttp": {"mailto:[email protected]", ""}, |
| 565 | } { |
| 566 | t.Run(name, func(t *testing.T) { |
| 567 | profile := testProfile.Copy() |
| 568 | profile.DocURL = c.input |
| 569 | rpt := New(profile, &Options{ |
| 570 | OutputFormat: Dot, |
| 571 | Symbol: regexp.MustCompile(`.`), |
| 572 | TrimPath: "/some/path", |
| 573 | SampleValue: func(v []int64) int64 { return v[1] }, |
| 574 | SampleUnit: testProfile.SampleType[1].Unit, |
| 575 | }) |
| 576 | if got := rpt.DocURL(); got != c.want { |
| 577 | t.Errorf("bad doc URL %q, expecting %q", got, c.want) |
| 578 | } |
| 579 | }) |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | func TestDocURLInLabels(t *testing.T) { |
| 584 | const url = "http://example.com/pprof-help" |
| 585 | profile := testProfile.Copy() |
| 586 | profile.DocURL = url |
| 587 | rpt := New(profile, &Options{ |
| 588 | OutputFormat: Text, |
| 589 | Symbol: regexp.MustCompile(`.`), |
| 590 | TrimPath: "/some/path", |
| 591 | SampleValue: func(v []int64) int64 { return v[1] }, |
| 592 | SampleUnit: testProfile.SampleType[1].Unit, |
| 593 | }) |
| 594 | |
| 595 | labels := fmt.Sprintf("%v", ProfileLabels(rpt)) |
| 596 | if !strings.Contains(labels, url) { |
| 597 | t.Errorf("expected URL %q not found in %s", url, labels) |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | func TestProfileLabels(t *testing.T) { |
| 602 | // Force the local timezone to UTC for the duration of this function to get a |
| 603 | // predictable result out of timezone printing. |
| 604 | defer func(prev *time.Location) { time.Local = prev }(time.Local) |
| 605 | time.Local = time.UTC |
| 606 | |
| 607 | profile := testProfile.Copy() |
| 608 | profile.TimeNanos = time.Unix(131, 0).UnixNano() |
| 609 | rpt := New(profile, &Options{ |
| 610 | SampleValue: func(v []int64) int64 { return v[1] }, |
| 611 | }) |
| 612 | |
| 613 | const want = "Time: 1970-01-01 00:02:11 UTC" |
| 614 | if labels := ProfileLabels(rpt); !slices.Contains(labels, want) { |
| 615 | t.Errorf("wanted to find a label containing %q, but found none in %v", want, labels) |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | func BenchmarkReportNewTrimmedGraph(b *testing.B) { |
| 620 | data := proftest.LargeProfile(b) |
| 621 | prof, err := profile.Parse(bytes.NewBuffer(data)) |
| 622 | if err != nil { |
| 623 | b.Fatal(err) |
| 624 | } |
| 625 | rep := NewDefault(prof, Options{}) |
| 626 | |
| 627 | b.ReportAllocs() |
| 628 | b.ResetTimer() |
| 629 | for i := 0; i < b.N; i++ { |
| 630 | g, _, _, _ := rep.newTrimmedGraph() |
| 631 | if g == nil { |
| 632 | b.Fatal("empty graph") |
| 633 | } |
| 634 | } |
| 635 | } |