Нет описания

dotgraph_test.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. package graph
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io/ioutil"
  19. "reflect"
  20. "strconv"
  21. "strings"
  22. "testing"
  23. "github.com/google/pprof/internal/proftest"
  24. )
  25. const path = "testdata/"
  26. func TestComposeWithStandardGraph(t *testing.T) {
  27. g := baseGraph()
  28. a, c := baseAttrsAndConfig()
  29. var buf bytes.Buffer
  30. ComposeDot(&buf, g, a, c)
  31. want, err := ioutil.ReadFile(path + "compose1.dot")
  32. if err != nil {
  33. t.Fatalf("error reading test file: %v", err)
  34. }
  35. compareGraphs(t, buf.Bytes(), want)
  36. }
  37. func TestComposeWithNodeAttributesAndZeroFlat(t *testing.T) {
  38. g := baseGraph()
  39. a, c := baseAttrsAndConfig()
  40. // Set NodeAttributes for Node 1.
  41. a.Nodes[g.Nodes[0]] = &DotNodeAttributes{
  42. Shape: "folder",
  43. Bold: true,
  44. Peripheries: 2,
  45. URL: "www.google.com",
  46. Formatter: func(ni *NodeInfo) string {
  47. return strings.ToUpper(ni.Name)
  48. },
  49. }
  50. // Set Flat value to zero on Node 2.
  51. g.Nodes[1].Flat = 0
  52. var buf bytes.Buffer
  53. ComposeDot(&buf, g, a, c)
  54. want, err := ioutil.ReadFile(path + "compose2.dot")
  55. if err != nil {
  56. t.Fatalf("error reading test file: %v", err)
  57. }
  58. compareGraphs(t, buf.Bytes(), want)
  59. }
  60. func TestComposeWithTagsAndResidualEdge(t *testing.T) {
  61. g := baseGraph()
  62. a, c := baseAttrsAndConfig()
  63. // Add tags to Node 1.
  64. g.Nodes[0].LabelTags["a"] = &Tag{
  65. Name: "tag1",
  66. Cum: 10,
  67. Flat: 10,
  68. }
  69. g.Nodes[0].NumericTags[""] = TagMap{
  70. "b": &Tag{
  71. Name: "tag2",
  72. Cum: 20,
  73. Flat: 20,
  74. Unit: "ms",
  75. },
  76. }
  77. // Set edge to be Residual.
  78. g.Nodes[0].Out[g.Nodes[1]].Residual = true
  79. var buf bytes.Buffer
  80. ComposeDot(&buf, g, a, c)
  81. want, err := ioutil.ReadFile(path + "compose3.dot")
  82. if err != nil {
  83. t.Fatalf("error reading test file: %v", err)
  84. }
  85. compareGraphs(t, buf.Bytes(), want)
  86. }
  87. func TestComposeWithNestedTags(t *testing.T) {
  88. g := baseGraph()
  89. a, c := baseAttrsAndConfig()
  90. // Add tags to Node 1.
  91. g.Nodes[0].LabelTags["tag1"] = &Tag{
  92. Name: "tag1",
  93. Cum: 10,
  94. Flat: 10,
  95. }
  96. g.Nodes[0].NumericTags["tag1"] = TagMap{
  97. "tag2": &Tag{
  98. Name: "tag2",
  99. Cum: 20,
  100. Flat: 20,
  101. Unit: "ms",
  102. },
  103. }
  104. var buf bytes.Buffer
  105. ComposeDot(&buf, g, a, c)
  106. want, err := ioutil.ReadFile(path + "compose5.dot")
  107. if err != nil {
  108. t.Fatalf("error reading test file: %v", err)
  109. }
  110. compareGraphs(t, buf.Bytes(), want)
  111. }
  112. func TestComposeWithEmptyGraph(t *testing.T) {
  113. g := &Graph{}
  114. a, c := baseAttrsAndConfig()
  115. var buf bytes.Buffer
  116. ComposeDot(&buf, g, a, c)
  117. want, err := ioutil.ReadFile(path + "compose4.dot")
  118. if err != nil {
  119. t.Fatalf("error reading test file: %v", err)
  120. }
  121. compareGraphs(t, buf.Bytes(), want)
  122. }
  123. func baseGraph() *Graph {
  124. src := &Node{
  125. Info: NodeInfo{Name: "src"},
  126. Flat: 10,
  127. Cum: 25,
  128. In: make(EdgeMap),
  129. Out: make(EdgeMap),
  130. LabelTags: make(TagMap),
  131. NumericTags: make(map[string]TagMap),
  132. }
  133. dest := &Node{
  134. Info: NodeInfo{Name: "dest"},
  135. Flat: 15,
  136. Cum: 25,
  137. In: make(EdgeMap),
  138. Out: make(EdgeMap),
  139. LabelTags: make(TagMap),
  140. NumericTags: make(map[string]TagMap),
  141. }
  142. edge := &Edge{
  143. Src: src,
  144. Dest: dest,
  145. Weight: 10,
  146. }
  147. src.Out[dest] = edge
  148. src.In[src] = edge
  149. return &Graph{
  150. Nodes: Nodes{
  151. src,
  152. dest,
  153. },
  154. }
  155. }
  156. func baseAttrsAndConfig() (*DotAttributes, *DotConfig) {
  157. a := &DotAttributes{
  158. Nodes: make(map[*Node]*DotNodeAttributes),
  159. }
  160. c := &DotConfig{
  161. Title: "testtitle",
  162. Labels: []string{"label1", "label2"},
  163. Total: 100,
  164. FormatValue: func(v int64) string {
  165. return strconv.FormatInt(v, 10)
  166. },
  167. }
  168. return a, c
  169. }
  170. func compareGraphs(t *testing.T, got, want []byte) {
  171. if string(got) != string(want) {
  172. d, err := proftest.Diff(got, want)
  173. if err != nil {
  174. t.Fatalf("error finding diff: %v", err)
  175. }
  176. t.Errorf("Compose incorrectly wrote %s", string(d))
  177. }
  178. }
  179. func TestNodeletCountCapping(t *testing.T) {
  180. labelTags := make(TagMap)
  181. for i := 0; i < 10; i++ {
  182. name := fmt.Sprintf("tag-%d", i)
  183. labelTags[name] = &Tag{
  184. Name: name,
  185. Flat: 10,
  186. Cum: 10,
  187. }
  188. }
  189. numTags := make(TagMap)
  190. for i := 0; i < 10; i++ {
  191. name := fmt.Sprintf("num-tag-%d", i)
  192. numTags[name] = &Tag{
  193. Name: name,
  194. Unit: "mb",
  195. Value: 16,
  196. Flat: 10,
  197. Cum: 10,
  198. }
  199. }
  200. node1 := &Node{
  201. Info: NodeInfo{Name: "node1-with-tags"},
  202. Flat: 10,
  203. Cum: 10,
  204. NumericTags: map[string]TagMap{"": numTags},
  205. LabelTags: labelTags,
  206. }
  207. node2 := &Node{
  208. Info: NodeInfo{Name: "node2"},
  209. Flat: 15,
  210. Cum: 15,
  211. }
  212. node3 := &Node{
  213. Info: NodeInfo{Name: "node3"},
  214. Flat: 15,
  215. Cum: 15,
  216. }
  217. g := &Graph{
  218. Nodes: Nodes{
  219. node1,
  220. node2,
  221. node3,
  222. },
  223. }
  224. for n := 1; n <= 3; n++ {
  225. input := maxNodelets + n
  226. if got, want := len(g.SelectTopNodes(input, true)), n; got != want {
  227. t.Errorf("SelectTopNodes(%d): got %d nodes, want %d", input, got, want)
  228. }
  229. }
  230. }
  231. func TestMultilinePrintableName(t *testing.T) {
  232. ni := &NodeInfo{
  233. Name: "test1.test2::test3",
  234. File: "src/file.cc",
  235. Address: 123,
  236. Lineno: 999,
  237. }
  238. want := fmt.Sprintf(`%016x\ntest1\ntest2\ntest3\nfile.cc:999\n`, 123)
  239. if got := multilinePrintableName(ni); got != want {
  240. t.Errorf("multilinePrintableName(%#v) == %q, want %q", ni, got, want)
  241. }
  242. }
  243. func TestTagCollapse(t *testing.T) {
  244. makeTag := func(name, unit string, value, flat, cum int64) *Tag {
  245. return &Tag{name, unit, value, flat, 0, cum, 0}
  246. }
  247. tagSource := []*Tag{
  248. makeTag("12mb", "mb", 12, 100, 100),
  249. makeTag("1kb", "kb", 1, 1, 1),
  250. makeTag("1mb", "mb", 1, 1000, 1000),
  251. makeTag("2048mb", "mb", 2048, 1000, 1000),
  252. makeTag("1b", "b", 1, 100, 100),
  253. makeTag("2b", "b", 2, 100, 100),
  254. makeTag("7b", "b", 7, 100, 100),
  255. }
  256. tagWant := [][]*Tag{
  257. {
  258. makeTag("1B..2GB", "", 0, 2401, 2401),
  259. },
  260. {
  261. makeTag("2GB", "", 0, 1000, 1000),
  262. makeTag("1B..12MB", "", 0, 1401, 1401),
  263. },
  264. {
  265. makeTag("2GB", "", 0, 1000, 1000),
  266. makeTag("12MB", "", 0, 100, 100),
  267. makeTag("1B..1MB", "", 0, 1301, 1301),
  268. },
  269. {
  270. makeTag("2GB", "", 0, 1000, 1000),
  271. makeTag("1MB", "", 0, 1000, 1000),
  272. makeTag("2B..1kB", "", 0, 201, 201),
  273. makeTag("1B", "", 0, 100, 100),
  274. makeTag("12MB", "", 0, 100, 100),
  275. },
  276. }
  277. for _, tc := range tagWant {
  278. var got, want []*Tag
  279. b := builder{nil, &DotAttributes{}, &DotConfig{}}
  280. got = b.collapsedTags(tagSource, len(tc), true)
  281. want = SortTags(tc, true)
  282. if !reflect.DeepEqual(got, want) {
  283. t.Errorf("collapse to %d, got:\n%v\nwant:\n%v", len(tc), tagString(got), tagString(want))
  284. }
  285. }
  286. }
  287. func tagString(t []*Tag) string {
  288. var ret []string
  289. for _, s := range t {
  290. ret = append(ret, fmt.Sprintln(s))
  291. }
  292. return strings.Join(ret, ":")
  293. }