Bläddra i källkod

Merge pull request #125 from hyangah/master

internal/report: change the format of tags command output
Raul Silvera 8 år sedan
förälder
incheckning
159df4f8ba

+ 9
- 0
internal/driver/driver_test.go Visa fil

@@ -22,6 +22,7 @@ import (
22 22
 	"crypto/tls"
23 23
 	"crypto/x509"
24 24
 	"encoding/pem"
25
+	"flag"
25 26
 	"fmt"
26 27
 	"io/ioutil"
27 28
 	"math/big"
@@ -41,6 +42,8 @@ import (
41 42
 	"github.com/google/pprof/profile"
42 43
 )
43 44
 
45
+var updateFlag = flag.Bool("update", false, "Update the golden files")
46
+
44 47
 func TestParse(t *testing.T) {
45 48
 	// Override weblist command to collect output in buffer
46 49
 	pprofCommands["weblist"].postProcess = nil
@@ -193,6 +196,12 @@ func TestParse(t *testing.T) {
193 196
 				t.Fatalf("diff %s %v", solution, err)
194 197
 			}
195 198
 			t.Errorf("%s\n%s\n", solution, d)
199
+			if *updateFlag {
200
+				err := ioutil.WriteFile(solution, b, 0644)
201
+				if err != nil {
202
+					t.Errorf("failed to update the solution file %q: %v", solution, err)
203
+				}
204
+			}
196 205
 		}
197 206
 	}
198 207
 }

+ 10
- 10
internal/driver/testdata/pprof.cpu.tags Visa fil

@@ -1,13 +1,13 @@
1
-key1: Total 1120
2
-      1000 (89.29%): tag1
3
-       100 ( 8.93%): tag2
4
-        10 ( 0.89%): tag3
5
-        10 ( 0.89%): tag4
1
+ key1: Total 1.1s
2
+          1.0s (89.29%): tag1
3
+       100.0ms ( 8.93%): tag2
4
+        10.0ms ( 0.89%): tag3
5
+        10.0ms ( 0.89%): tag4
6 6
 
7
-key2: Total 1020
8
-      1010 (99.02%): tag1
9
-        10 ( 0.98%): tag2
7
+ key2: Total 1.0s
8
+         1.0s (99.02%): tag1
9
+       10.0ms ( 0.98%): tag2
10 10
 
11
-key3: Total 100
12
-       100 (  100%): tag2
11
+ key3: Total 100.0ms
12
+       100.0ms (  100%): tag2
13 13
 

+ 4
- 4
internal/driver/testdata/pprof.cpu.tags.focus.ignore Visa fil

@@ -1,6 +1,6 @@
1
-key1: Total 100
2
-       100 (  100%): tag2
1
+ key1: Total 100.0ms
2
+       100.0ms (  100%): tag2
3 3
 
4
-key3: Total 100
5
-       100 (  100%): tag2
4
+ key3: Total 100.0ms
5
+       100.0ms (  100%): tag2
6 6
 

+ 5
- 5
internal/driver/testdata/pprof.heap.tags Visa fil

@@ -1,6 +1,6 @@
1
-bytes: Total 150
2
-        80 (53.33%): 400kB
3
-        40 (26.67%): 1.56MB
4
-        20 (13.33%): 200kB
5
-        10 ( 6.67%): 100kB
1
+ bytes: Total 98.6MB
2
+          62.5MB (63.37%): 1.56MB
3
+          31.2MB (31.68%): 400kB
4
+           3.9MB ( 3.96%): 200kB
5
+        1000.0kB ( 0.99%): 100kB
6 6
 

+ 5
- 5
internal/driver/testdata/pprof.heap.tags.unit Visa fil

@@ -1,6 +1,6 @@
1
-bytes: Total 150
2
-        80 (53.33%): 409600B
3
-        40 (26.67%): 1638400B
4
-        20 (13.33%): 204800B
5
-        10 ( 6.67%): 102400B
1
+ bytes: Total 103424000.0B
2
+        65536000.0B (63.37%): 1638400B
3
+        32768000.0B (31.68%): 409600B
4
+         4096000.0B ( 3.96%): 204800B
5
+         1024000.0B ( 0.99%): 102400B
6 6
 

+ 19
- 18
internal/report/report.go Visa fil

@@ -25,6 +25,7 @@ import (
25 25
 	"sort"
26 26
 	"strconv"
27 27
 	"strings"
28
+	"text/tabwriter"
28 29
 	"time"
29 30
 
30 31
 	"github.com/google/pprof/internal/graph"
@@ -620,25 +621,23 @@ func printTags(w io.Writer, rpt *Report) error {
620 621
 	for _, s := range p.Sample {
621 622
 		for key, vals := range s.Label {
622 623
 			for _, val := range vals {
623
-				if valueMap, ok := tagMap[key]; ok {
624
-					valueMap[val] = valueMap[val] + s.Value[0]
625
-					continue
624
+				valueMap, ok := tagMap[key]
625
+				if !ok {
626
+					valueMap = make(map[string]int64)
627
+					tagMap[key] = valueMap
626 628
 				}
627
-				valueMap := make(map[string]int64)
628
-				valueMap[val] = s.Value[0]
629
-				tagMap[key] = valueMap
629
+				valueMap[val] += o.SampleValue(s.Value)
630 630
 			}
631 631
 		}
632 632
 		for key, vals := range s.NumLabel {
633 633
 			for _, nval := range vals {
634 634
 				val := formatTag(nval, key)
635
-				if valueMap, ok := tagMap[key]; ok {
636
-					valueMap[val] = valueMap[val] + s.Value[0]
637
-					continue
635
+				valueMap, ok := tagMap[key]
636
+				if !ok {
637
+					valueMap = make(map[string]int64)
638
+					tagMap[key] = valueMap
638 639
 				}
639
-				valueMap := make(map[string]int64)
640
-				valueMap[val] = s.Value[0]
641
-				tagMap[key] = valueMap
640
+				valueMap[val] += o.SampleValue(s.Value)
642 641
 			}
643 642
 		}
644 643
 	}
@@ -647,6 +646,7 @@ func printTags(w io.Writer, rpt *Report) error {
647 646
 	for key := range tagMap {
648 647
 		tagKeys = append(tagKeys, &graph.Tag{Name: key})
649 648
 	}
649
+	tabw := tabwriter.NewWriter(w, 0, 0, 1, ' ', tabwriter.AlignRight)
650 650
 	for _, tagKey := range graph.SortTags(tagKeys, true) {
651 651
 		var total int64
652 652
 		key := tagKey.Name
@@ -656,18 +656,19 @@ func printTags(w io.Writer, rpt *Report) error {
656 656
 			tags = append(tags, &graph.Tag{Name: t, Flat: c})
657 657
 		}
658 658
 
659
-		fmt.Fprintf(w, "%s: Total %d\n", key, total)
659
+		f, u := measurement.Scale(total, o.SampleUnit, o.OutputUnit)
660
+		fmt.Fprintf(tabw, "%s:\t Total %.1f%s\n", key, f, u)
660 661
 		for _, t := range graph.SortTags(tags, true) {
662
+			f, u := measurement.Scale(t.FlatValue(), o.SampleUnit, o.OutputUnit)
661 663
 			if total > 0 {
662
-				fmt.Fprintf(w, "  %8d (%s): %s\n", t.FlatValue(),
663
-					percentage(t.FlatValue(), total), t.Name)
664
+				fmt.Fprintf(tabw, " \t%.1f%s (%s):\t %s\n", f, u, percentage(t.FlatValue(), total), t.Name)
664 665
 			} else {
665
-				fmt.Fprintf(w, "  %8d: %s\n", t.FlatValue(), t.Name)
666
+				fmt.Fprintf(tabw, " \t%.1f%s:\t %s\n", f, u, t.Name)
666 667
 			}
667 668
 		}
668
-		fmt.Fprintln(w)
669
+		fmt.Fprintln(tabw)
669 670
 	}
670
-	return nil
671
+	return tabw.Flush()
671 672
 }
672 673
 
673 674
 // printComments prints all freeform comments in the profile.