| 1 | // Copyright 2018 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 profile |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "fmt" |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | |
| 23 | "github.com/google/pprof/internal/proftest" |
| 24 | ) |
| 25 | |
| 26 | func TestMapMapping(t *testing.T) { |
| 27 | pm := &profileMerger{ |
| 28 | p: &Profile{}, |
| 29 | mappings: make(map[mappingKey]*Mapping), |
| 30 | mappingsByID: make(map[uint64]mapInfo), |
| 31 | } |
| 32 | for _, tc := range []struct { |
| 33 | desc string |
| 34 | m1 Mapping |
| 35 | m2 Mapping |
| 36 | wantMerged bool |
| 37 | }{ |
| 38 | { |
| 39 | desc: "same file name", |
| 40 | m1: Mapping{ |
| 41 | ID: 1, |
| 42 | File: "test-file-1", |
| 43 | }, |
| 44 | m2: Mapping{ |
| 45 | ID: 2, |
| 46 | File: "test-file-1", |
| 47 | }, |
| 48 | wantMerged: true, |
| 49 | }, |
| 50 | { |
| 51 | desc: "same build ID", |
| 52 | m1: Mapping{ |
| 53 | ID: 3, |
| 54 | BuildID: "test-build-id-1", |
| 55 | }, |
| 56 | m2: Mapping{ |
| 57 | ID: 4, |
| 58 | BuildID: "test-build-id-1", |
| 59 | }, |
| 60 | wantMerged: true, |
| 61 | }, |
| 62 | { |
| 63 | desc: "same fake mapping", |
| 64 | m1: Mapping{ |
| 65 | ID: 5, |
| 66 | }, |
| 67 | m2: Mapping{ |
| 68 | ID: 6, |
| 69 | }, |
| 70 | wantMerged: true, |
| 71 | }, |
| 72 | { |
| 73 | desc: "different start", |
| 74 | m1: Mapping{ |
| 75 | ID: 7, |
| 76 | Start: 0x1000, |
| 77 | Limit: 0x2000, |
| 78 | BuildID: "test-build-id-2", |
| 79 | }, |
| 80 | m2: Mapping{ |
| 81 | ID: 8, |
| 82 | Start: 0x3000, |
| 83 | Limit: 0x4000, |
| 84 | BuildID: "test-build-id-2", |
| 85 | }, |
| 86 | wantMerged: true, |
| 87 | }, |
| 88 | { |
| 89 | desc: "different file name", |
| 90 | m1: Mapping{ |
| 91 | ID: 9, |
| 92 | File: "test-file-2", |
| 93 | }, |
| 94 | m2: Mapping{ |
| 95 | ID: 10, |
| 96 | File: "test-file-3", |
| 97 | }, |
| 98 | }, |
| 99 | { |
| 100 | desc: "different build id", |
| 101 | m1: Mapping{ |
| 102 | ID: 11, |
| 103 | BuildID: "test-build-id-3", |
| 104 | }, |
| 105 | m2: Mapping{ |
| 106 | ID: 12, |
| 107 | BuildID: "test-build-id-4", |
| 108 | }, |
| 109 | }, |
| 110 | { |
| 111 | desc: "different size", |
| 112 | m1: Mapping{ |
| 113 | ID: 13, |
| 114 | Start: 0x1000, |
| 115 | Limit: 0x3000, |
| 116 | BuildID: "test-build-id-5", |
| 117 | }, |
| 118 | m2: Mapping{ |
| 119 | ID: 14, |
| 120 | Start: 0x1000, |
| 121 | Limit: 0x5000, |
| 122 | BuildID: "test-build-id-5", |
| 123 | }, |
| 124 | }, |
| 125 | { |
| 126 | desc: "different offset", |
| 127 | m1: Mapping{ |
| 128 | ID: 15, |
| 129 | Offset: 1, |
| 130 | BuildID: "test-build-id-6", |
| 131 | }, |
| 132 | m2: Mapping{ |
| 133 | ID: 16, |
| 134 | Offset: 2, |
| 135 | BuildID: "test-build-id-6", |
| 136 | }, |
| 137 | }, |
| 138 | } { |
| 139 | t.Run(tc.desc, func(t *testing.T) { |
| 140 | info1 := pm.mapMapping(&tc.m1) |
| 141 | info2 := pm.mapMapping(&tc.m2) |
| 142 | gotM1, gotM2 := *info1.m, *info2.m |
| 143 | |
| 144 | wantM1 := tc.m1 |
| 145 | wantM1.ID = gotM1.ID |
| 146 | if gotM1 != wantM1 { |
| 147 | t.Errorf("first mapping got %v, want %v", gotM1, wantM1) |
| 148 | } |
| 149 | |
| 150 | if tc.wantMerged { |
| 151 | if gotM1 != gotM2 { |
| 152 | t.Errorf("first mapping got %v, second mapping got %v, want equal", gotM1, gotM2) |
| 153 | } |
| 154 | if info1.offset != 0 { |
| 155 | t.Errorf("first mapping info got offset %d, want 0", info1.offset) |
| 156 | } |
| 157 | if wantOffset := int64(tc.m1.Start) - int64(tc.m2.Start); wantOffset != info2.offset { |
| 158 | t.Errorf("second mapping info got offset %d, want %d", info2.offset, wantOffset) |
| 159 | } |
| 160 | } else { |
| 161 | if gotM1.ID == gotM2.ID { |
| 162 | t.Errorf("first mapping got %v, second mapping got %v, want different IDs", gotM1, gotM2) |
| 163 | } |
| 164 | wantM2 := tc.m2 |
| 165 | wantM2.ID = gotM2.ID |
| 166 | if gotM2 != wantM2 { |
| 167 | t.Errorf("second mapping got %v, want %v", gotM2, wantM2) |
| 168 | } |
| 169 | } |
| 170 | }) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestLocationIDMap(t *testing.T) { |
| 175 | ids := []uint64{1, 2, 5, 9, 10, 11, 100, 1000, 1000000} |
| 176 | missing := []uint64{3, 4, 200} |
| 177 | |
| 178 | // Populate the map,. |
| 179 | idmap := makeLocationIDMap(10) |
| 180 | for _, id := range ids { |
| 181 | loc := &Location{ID: id} |
| 182 | idmap.set(id, loc) |
| 183 | } |
| 184 | |
| 185 | // Check ids that should be present in the map. |
| 186 | for _, id := range ids { |
| 187 | loc := idmap.get(id) |
| 188 | if loc == nil { |
| 189 | t.Errorf("No location found for %d", id) |
| 190 | } else if loc.ID != id { |
| 191 | t.Errorf("Wrong location %d found for %d", loc.ID, id) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Check ids that should not be present in the map. |
| 196 | for _, id := range missing { |
| 197 | loc := idmap.get(id) |
| 198 | if loc != nil { |
| 199 | t.Errorf("Unexpected location %d found for %d", loc.ID, id) |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func BenchmarkMerge(b *testing.B) { |
| 205 | data := proftest.LargeProfile(b) |
| 206 | for n := 1; n <= 2; n++ { // Merge either 1 or 2 instances. |
| 207 | b.Run(fmt.Sprintf("%d", n), func(b *testing.B) { |
| 208 | list := make([]*Profile, n) |
| 209 | for i := 0; i < n; i++ { |
| 210 | prof, err := Parse(bytes.NewBuffer(data)) |
| 211 | if err != nil { |
| 212 | b.Fatal(err) |
| 213 | } |
| 214 | list[i] = prof |
| 215 | } |
| 216 | b.ResetTimer() |
| 217 | for i := 0; i < b.N; i++ { |
| 218 | _, err := Merge(list) |
| 219 | if err != nil { |
| 220 | b.Fatal(err) |
| 221 | } |
| 222 | } |
| 223 | }) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | func TestCompatibilizeSampleTypes(t *testing.T) { |
| 228 | for _, tc := range []struct { |
| 229 | desc string |
| 230 | ps []*Profile |
| 231 | want []*Profile |
| 232 | wantError bool |
| 233 | }{ |
| 234 | { |
| 235 | desc: "drop first sample types", |
| 236 | ps: []*Profile{ |
| 237 | { |
| 238 | DefaultSampleType: "delete1", |
| 239 | SampleType: []*ValueType{ |
| 240 | {Type: "delete1", Unit: "Unit1"}, |
| 241 | {Type: "delete2", Unit: "Unit2"}, |
| 242 | {Type: "keep1", Unit: "Unit3"}, |
| 243 | {Type: "keep2", Unit: "Unit4"}, |
| 244 | {Type: "keep3", Unit: "Unit5"}, |
| 245 | }, |
| 246 | Sample: []*Sample{ |
| 247 | {Value: []int64{1, 2, 3, 4, 5}}, |
| 248 | {Value: []int64{10, 20, 30, 40, 50}}, |
| 249 | }, |
| 250 | }, |
| 251 | { |
| 252 | DefaultSampleType: "keep1", |
| 253 | SampleType: []*ValueType{ |
| 254 | {Type: "keep1", Unit: "Unit3"}, |
| 255 | {Type: "keep2", Unit: "Unit4"}, |
| 256 | {Type: "keep3", Unit: "Unit5"}, |
| 257 | }, |
| 258 | Sample: []*Sample{ |
| 259 | {Value: []int64{1, 2, 3}}, |
| 260 | {Value: []int64{10, 20, 30}}, |
| 261 | }, |
| 262 | }, |
| 263 | }, |
| 264 | want: []*Profile{ |
| 265 | { |
| 266 | DefaultSampleType: "keep1", |
| 267 | SampleType: []*ValueType{ |
| 268 | {Type: "keep1", Unit: "Unit3"}, |
| 269 | {Type: "keep2", Unit: "Unit4"}, |
| 270 | {Type: "keep3", Unit: "Unit5"}, |
| 271 | }, |
| 272 | Sample: []*Sample{ |
| 273 | {Value: []int64{3, 4, 5}}, |
| 274 | {Value: []int64{30, 40, 50}}, |
| 275 | }, |
| 276 | }, |
| 277 | { |
| 278 | DefaultSampleType: "keep1", |
| 279 | SampleType: []*ValueType{ |
| 280 | {Type: "keep1", Unit: "Unit3"}, |
| 281 | {Type: "keep2", Unit: "Unit4"}, |
| 282 | {Type: "keep3", Unit: "Unit5"}, |
| 283 | }, |
| 284 | Sample: []*Sample{ |
| 285 | {Value: []int64{1, 2, 3}}, |
| 286 | {Value: []int64{10, 20, 30}}, |
| 287 | }, |
| 288 | }, |
| 289 | }, |
| 290 | }, |
| 291 | { |
| 292 | desc: "drop last sample types", |
| 293 | ps: []*Profile{ |
| 294 | { |
| 295 | DefaultSampleType: "delete2", |
| 296 | SampleType: []*ValueType{ |
| 297 | {Type: "keep1", Unit: "Unit3"}, |
| 298 | {Type: "keep2", Unit: "Unit4"}, |
| 299 | {Type: "keep3", Unit: "Unit5"}, |
| 300 | {Type: "delete1", Unit: "Unit1"}, |
| 301 | {Type: "delete2", Unit: "Unit2"}, |
| 302 | }, |
| 303 | Sample: []*Sample{ |
| 304 | {Value: []int64{1, 2, 3, 4, 5}}, |
| 305 | {Value: []int64{10, 20, 30, 40, 50}}, |
| 306 | }, |
| 307 | }, |
| 308 | { |
| 309 | DefaultSampleType: "keep2", |
| 310 | SampleType: []*ValueType{ |
| 311 | {Type: "keep1", Unit: "Unit3"}, |
| 312 | {Type: "keep2", Unit: "Unit4"}, |
| 313 | {Type: "keep3", Unit: "Unit5"}, |
| 314 | }, |
| 315 | Sample: []*Sample{ |
| 316 | {Value: []int64{1, 2, 3}}, |
| 317 | {Value: []int64{10, 20, 30}}, |
| 318 | }, |
| 319 | }, |
| 320 | }, |
| 321 | want: []*Profile{ |
| 322 | { |
| 323 | DefaultSampleType: "keep1", |
| 324 | SampleType: []*ValueType{ |
| 325 | {Type: "keep1", Unit: "Unit3"}, |
| 326 | {Type: "keep2", Unit: "Unit4"}, |
| 327 | {Type: "keep3", Unit: "Unit5"}, |
| 328 | }, |
| 329 | Sample: []*Sample{ |
| 330 | {Value: []int64{1, 2, 3}}, |
| 331 | {Value: []int64{10, 20, 30}}, |
| 332 | }, |
| 333 | }, |
| 334 | { |
| 335 | DefaultSampleType: "keep2", |
| 336 | SampleType: []*ValueType{ |
| 337 | {Type: "keep1", Unit: "Unit3"}, |
| 338 | {Type: "keep2", Unit: "Unit4"}, |
| 339 | {Type: "keep3", Unit: "Unit5"}, |
| 340 | }, |
| 341 | Sample: []*Sample{ |
| 342 | {Value: []int64{1, 2, 3}}, |
| 343 | {Value: []int64{10, 20, 30}}, |
| 344 | }, |
| 345 | }, |
| 346 | }, |
| 347 | }, |
| 348 | { |
| 349 | desc: "drop sample types and reorder", |
| 350 | ps: []*Profile{ |
| 351 | { |
| 352 | DefaultSampleType: "keep3", |
| 353 | SampleType: []*ValueType{ |
| 354 | {Type: "delete1", Unit: "Unit1"}, |
| 355 | {Type: "keep1", Unit: "Unit3"}, |
| 356 | {Type: "delete2", Unit: "Unit2"}, |
| 357 | {Type: "keep2", Unit: "Unit4"}, |
| 358 | {Type: "keep3", Unit: "Unit5"}, |
| 359 | }, |
| 360 | Sample: []*Sample{ |
| 361 | {Value: []int64{1, 2, 3, 4, 5}}, |
| 362 | {Value: []int64{10, 20, 30, 40, 50}}, |
| 363 | }, |
| 364 | }, |
| 365 | { |
| 366 | DefaultSampleType: "keep2", |
| 367 | SampleType: []*ValueType{ |
| 368 | {Type: "keep3", Unit: "Unit5"}, |
| 369 | {Type: "keep2", Unit: "Unit4"}, |
| 370 | {Type: "keep1", Unit: "Unit3"}, |
| 371 | }, |
| 372 | Sample: []*Sample{ |
| 373 | {Value: []int64{1, 2, 3}}, |
| 374 | {Value: []int64{10, 20, 30}}, |
| 375 | }, |
| 376 | }, |
| 377 | }, |
| 378 | want: []*Profile{ |
| 379 | { |
| 380 | DefaultSampleType: "keep3", |
| 381 | SampleType: []*ValueType{ |
| 382 | {Type: "keep1", Unit: "Unit3"}, |
| 383 | {Type: "keep2", Unit: "Unit4"}, |
| 384 | {Type: "keep3", Unit: "Unit5"}, |
| 385 | }, |
| 386 | Sample: []*Sample{ |
| 387 | {Value: []int64{2, 4, 5}}, |
| 388 | {Value: []int64{20, 40, 50}}, |
| 389 | }, |
| 390 | }, |
| 391 | { |
| 392 | DefaultSampleType: "keep2", |
| 393 | SampleType: []*ValueType{ |
| 394 | {Type: "keep1", Unit: "Unit3"}, |
| 395 | {Type: "keep2", Unit: "Unit4"}, |
| 396 | {Type: "keep3", Unit: "Unit5"}, |
| 397 | }, |
| 398 | Sample: []*Sample{ |
| 399 | {Value: []int64{3, 2, 1}}, |
| 400 | {Value: []int64{30, 20, 10}}, |
| 401 | }, |
| 402 | }, |
| 403 | }, |
| 404 | }, |
| 405 | { |
| 406 | desc: "empty common types", |
| 407 | ps: []*Profile{ |
| 408 | { |
| 409 | SampleType: []*ValueType{ |
| 410 | {Type: "keep1", Unit: "Unit1"}, |
| 411 | {Type: "keep2", Unit: "Unit2"}, |
| 412 | {Type: "keep3", Unit: "Unit3"}, |
| 413 | }, |
| 414 | }, |
| 415 | { |
| 416 | SampleType: []*ValueType{ |
| 417 | {Type: "keep4", Unit: "Unit4"}, |
| 418 | {Type: "keep5", Unit: "Unit5"}, |
| 419 | }, |
| 420 | }, |
| 421 | }, |
| 422 | wantError: true, |
| 423 | }, |
| 424 | } { |
| 425 | t.Run(tc.desc, func(t *testing.T) { |
| 426 | err := CompatibilizeSampleTypes(tc.ps) |
| 427 | if (err != nil) != tc.wantError { |
| 428 | t.Fatalf("CompatibilizeSampleTypes() returned error: %v, want any error=%t", err, tc.wantError) |
| 429 | } |
| 430 | if err != nil { |
| 431 | return |
| 432 | } |
| 433 | for i := 0; i < len(tc.want); i++ { |
| 434 | gotStr := tc.ps[i].String() |
| 435 | wantStr := tc.want[i].String() |
| 436 | if gotStr != wantStr { |
| 437 | d, err := proftest.Diff([]byte(wantStr), []byte(gotStr)) |
| 438 | if err != nil { |
| 439 | t.Fatalf("failed to get diff: %v", err) |
| 440 | } |
| 441 | t.Errorf("CompatibilizeSampleTypes(): profile[%d] got diff (-want +got)\n%s", i, string(d)) |
| 442 | } |
| 443 | } |
| 444 | }) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | func TestDocURLMerge(t *testing.T) { |
| 449 | const url1 = "http://example.com/url1" |
| 450 | const url2 = "http://example.com/url2" |
| 451 | type testCase struct { |
| 452 | name string |
| 453 | profiles []*Profile |
| 454 | want string |
| 455 | } |
| 456 | profile := func(url string) *Profile { |
| 457 | return &Profile{ |
| 458 | PeriodType: &ValueType{Type: "cpu", Unit: "seconds"}, |
| 459 | DocURL: url, |
| 460 | } |
| 461 | } |
| 462 | for _, test := range []testCase{ |
| 463 | { |
| 464 | name: "nolinks", |
| 465 | profiles: []*Profile{ |
| 466 | profile(""), |
| 467 | profile(""), |
| 468 | }, |
| 469 | want: "", |
| 470 | }, |
| 471 | { |
| 472 | name: "single", |
| 473 | profiles: []*Profile{ |
| 474 | profile(url1), |
| 475 | }, |
| 476 | want: url1, |
| 477 | }, |
| 478 | { |
| 479 | name: "mix", |
| 480 | profiles: []*Profile{ |
| 481 | profile(""), |
| 482 | profile(url1), |
| 483 | }, |
| 484 | want: url1, |
| 485 | }, |
| 486 | { |
| 487 | name: "different", |
| 488 | profiles: []*Profile{ |
| 489 | profile(url1), |
| 490 | profile(url2), |
| 491 | }, |
| 492 | want: url1, |
| 493 | }, |
| 494 | } { |
| 495 | t.Run(test.name, func(t *testing.T) { |
| 496 | merged, err := combineHeaders(test.profiles) |
| 497 | if err != nil { |
| 498 | t.Fatal(err) |
| 499 | } |
| 500 | got := merged.DocURL |
| 501 | if !reflect.DeepEqual(test.want, got) { |
| 502 | t.Errorf("unexpected links; want: %#v, got: %#v", test.want, got) |
| 503 | } |
| 504 | }) |
| 505 | } |
| 506 | } |