暂无描述

webui_test.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2017 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 driver
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "net/http"
  19. "net/http/httptest"
  20. "net/url"
  21. "os/exec"
  22. "regexp"
  23. "strings"
  24. "testing"
  25. "github.com/google/pprof/internal/plugin"
  26. "github.com/google/pprof/profile"
  27. )
  28. func TestWebInterface(t *testing.T) {
  29. prof := makeFakeProfile()
  30. ui := &webInterface{prof, &plugin.Options{Obj: fakeObjTool{}}}
  31. // Start test server.
  32. server := httptest.NewServer(http.HandlerFunc(
  33. func(w http.ResponseWriter, r *http.Request) {
  34. switch r.URL.Path {
  35. case "/":
  36. ui.dot(w, r)
  37. case "/disasm":
  38. ui.disasm(w, r)
  39. case "/weblist":
  40. ui.weblist(w, r)
  41. }
  42. }))
  43. defer server.Close()
  44. haveDot := false
  45. if _, err := exec.LookPath("dot"); err == nil {
  46. haveDot = true
  47. }
  48. type testCase struct {
  49. path string
  50. want []string
  51. needDot bool
  52. }
  53. testcases := []testCase{
  54. {"/", []string{"F1", "F2", "F3", "testbin", "cpu"}, true},
  55. {"/weblist?f=" + url.QueryEscape("F[12]"),
  56. []string{"F1", "F2", "300ms line1"}, false},
  57. {"/disasm?f=" + url.QueryEscape("F[12]"),
  58. []string{"f1:asm", "f2:asm"}, false},
  59. }
  60. for _, c := range testcases {
  61. if c.needDot && !haveDot {
  62. t.Log("skpping", c.path, "since dot (graphviz) does not seem to be installed")
  63. continue
  64. }
  65. res, err := http.Get(server.URL + c.path)
  66. if err != nil {
  67. t.Error("could not fetch", c.path, err)
  68. continue
  69. }
  70. data, err := ioutil.ReadAll(res.Body)
  71. if err != nil {
  72. t.Error("could not read response", c.path, err)
  73. continue
  74. }
  75. result := string(data)
  76. for _, w := range c.want {
  77. if !strings.Contains(result, w) {
  78. t.Errorf("response for %s does not contain "+
  79. "expected string '%s'; "+
  80. "actual result:\n%s", c.path, w, result)
  81. }
  82. }
  83. }
  84. }
  85. // Implement fake object file support.
  86. const addrBase = 0x1000
  87. const fakeSource = "testdata/file1000.src"
  88. type fakeObj struct{}
  89. func (f fakeObj) Close() error { return nil }
  90. func (f fakeObj) Name() string { return "testbin" }
  91. func (f fakeObj) Base() uint64 { return 0 }
  92. func (f fakeObj) BuildID() string { return "" }
  93. func (f fakeObj) SourceLine(addr uint64) ([]plugin.Frame, error) {
  94. return nil, fmt.Errorf("SourceLine unimplemented")
  95. }
  96. func (f fakeObj) Symbols(r *regexp.Regexp, addr uint64) ([]*plugin.Sym, error) {
  97. return []*plugin.Sym{
  98. {[]string{"F1"}, fakeSource, addrBase, addrBase + 10},
  99. {[]string{"F2"}, fakeSource, addrBase + 10, addrBase + 20},
  100. {[]string{"F3"}, fakeSource, addrBase + 20, addrBase + 30},
  101. }, nil
  102. }
  103. type fakeObjTool struct{}
  104. func (obj fakeObjTool) Open(file string, start, limit, offset uint64) (plugin.ObjFile, error) {
  105. return fakeObj{}, nil
  106. }
  107. func (obj fakeObjTool) Disasm(file string, start, end uint64) ([]plugin.Inst, error) {
  108. return []plugin.Inst{
  109. {Addr: addrBase + 0, Text: "f1:asm", Function: "F1"},
  110. {Addr: addrBase + 10, Text: "f2:asm", Function: "F2"},
  111. {Addr: addrBase + 20, Text: "d3:asm", Function: "F3"},
  112. }, nil
  113. }
  114. func makeFakeProfile() *profile.Profile {
  115. // Three functions: F1, F2, F3 with three lines, 11, 22, 33.
  116. funcs := []*profile.Function{
  117. {ID: 1, Name: "F1", Filename: fakeSource, StartLine: 3},
  118. {ID: 2, Name: "F2", Filename: fakeSource, StartLine: 5},
  119. {ID: 3, Name: "F3", Filename: fakeSource, StartLine: 7},
  120. }
  121. lines := []profile.Line{
  122. {Function: funcs[0], Line: 11},
  123. {Function: funcs[1], Line: 22},
  124. {Function: funcs[2], Line: 33},
  125. }
  126. mapping := []*profile.Mapping{
  127. {
  128. ID: 1,
  129. Start: addrBase,
  130. Limit: addrBase + 10,
  131. Offset: 0,
  132. File: "testbin",
  133. HasFunctions: true,
  134. HasFilenames: true,
  135. HasLineNumbers: true,
  136. },
  137. }
  138. // Three interesting addresses: base+{10,20,30}
  139. locs := []*profile.Location{
  140. {ID: 1, Address: addrBase + 10, Line: lines[0:1], Mapping: mapping[0]},
  141. {ID: 2, Address: addrBase + 20, Line: lines[1:2], Mapping: mapping[0]},
  142. {ID: 3, Address: addrBase + 30, Line: lines[2:3], Mapping: mapping[0]},
  143. }
  144. // Two stack traces.
  145. return &profile.Profile{
  146. PeriodType: &profile.ValueType{Type: "cpu", Unit: "milliseconds"},
  147. Period: 1,
  148. DurationNanos: 10e9,
  149. SampleType: []*profile.ValueType{
  150. {Type: "cpu", Unit: "milliseconds"},
  151. },
  152. Sample: []*profile.Sample{
  153. {
  154. Location: []*profile.Location{locs[2], locs[1], locs[0]},
  155. Value: []int64{100},
  156. },
  157. {
  158. Location: []*profile.Location{locs[1], locs[0]},
  159. Value: []int64{200},
  160. },
  161. },
  162. Location: locs,
  163. Function: funcs,
  164. Mapping: mapping,
  165. }
  166. }