Browse Source

Dedupe profile comments when merging profiles. (#303)

Alexey Alexandrov 7 years ago
parent
commit
65d07f9e45
2 changed files with 11 additions and 1 deletions
  1. 7
    1
      profile/merge.go
  2. 4
    0
      profile/profile_test.go

+ 7
- 1
profile/merge.go View File

425
 
425
 
426
 	var timeNanos, durationNanos, period int64
426
 	var timeNanos, durationNanos, period int64
427
 	var comments []string
427
 	var comments []string
428
+	seenComments := map[string]bool{}
428
 	var defaultSampleType string
429
 	var defaultSampleType string
429
 	for _, s := range srcs {
430
 	for _, s := range srcs {
430
 		if timeNanos == 0 || s.TimeNanos < timeNanos {
431
 		if timeNanos == 0 || s.TimeNanos < timeNanos {
434
 		if period == 0 || period < s.Period {
435
 		if period == 0 || period < s.Period {
435
 			period = s.Period
436
 			period = s.Period
436
 		}
437
 		}
437
-		comments = append(comments, s.Comments...)
438
+		for _, c := range s.Comments {
439
+			if seen := seenComments[c]; !seen {
440
+				comments = append(comments, c)
441
+				seenComments[c] = true
442
+			}
443
+		}
438
 		if defaultSampleType == "" {
444
 		if defaultSampleType == "" {
439
 			defaultSampleType = s.DefaultSampleType
445
 			defaultSampleType = s.DefaultSampleType
440
 		}
446
 		}

+ 4
- 0
profile/profile_test.go View File

631
 	// location should add up to 0).
631
 	// location should add up to 0).
632
 
632
 
633
 	prof := testProfile1.Copy()
633
 	prof := testProfile1.Copy()
634
+	prof.Comments = []string{"comment1"}
634
 	p1, err := Merge([]*Profile{prof, prof})
635
 	p1, err := Merge([]*Profile{prof, prof})
635
 	if err != nil {
636
 	if err != nil {
636
 		t.Errorf("merge error: %v", err)
637
 		t.Errorf("merge error: %v", err)
640
 	if err != nil {
641
 	if err != nil {
641
 		t.Errorf("merge error: %v", err)
642
 		t.Errorf("merge error: %v", err)
642
 	}
643
 	}
644
+	if got, want := len(prof.Comments), 1; got != want {
645
+		t.Errorf("len(prof.Comments) = %d, want %d", got, want)
646
+	}
643
 
647
 
644
 	// Use aggregation to merge locations at function granularity.
648
 	// Use aggregation to merge locations at function granularity.
645
 	if err := prof.Aggregate(false, true, false, false, false); err != nil {
649
 	if err := prof.Aggregate(false, true, false, false, false); err != nil {