profile.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package cover provides support for parsing coverage profiles
  5. // generated by "go test -coverprofile=cover.out".
  6. package cover // import "golang.org/x/tools/cover"
  7. import (
  8. "bufio"
  9. "errors"
  10. "fmt"
  11. "math"
  12. "os"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. )
  17. // Profile represents the profiling data for a specific file.
  18. type Profile struct {
  19. FileName string
  20. Mode string
  21. Blocks []ProfileBlock
  22. }
  23. // ProfileBlock represents a single block of profiling data.
  24. type ProfileBlock struct {
  25. StartLine, StartCol int
  26. EndLine, EndCol int
  27. NumStmt, Count int
  28. }
  29. type byFileName []*Profile
  30. func (p byFileName) Len() int { return len(p) }
  31. func (p byFileName) Less(i, j int) bool { return p[i].FileName < p[j].FileName }
  32. func (p byFileName) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  33. // ParseProfiles parses profile data in the specified file and returns a
  34. // Profile for each source file described therein.
  35. func ParseProfiles(fileName string) ([]*Profile, error) {
  36. pf, err := os.Open(fileName)
  37. if err != nil {
  38. return nil, err
  39. }
  40. defer pf.Close()
  41. files := make(map[string]*Profile)
  42. buf := bufio.NewReader(pf)
  43. // First line is "mode: foo", where foo is "set", "count", or "atomic".
  44. // Rest of file is in the format
  45. // encoding/base64/base64.go:34.44,37.40 3 1
  46. // where the fields are: name.go:line.column,line.column numberOfStatements count
  47. s := bufio.NewScanner(buf)
  48. mode := ""
  49. for s.Scan() {
  50. line := s.Text()
  51. if mode == "" {
  52. const p = "mode: "
  53. if !strings.HasPrefix(line, p) || line == p {
  54. return nil, fmt.Errorf("bad mode line: %v", line)
  55. }
  56. mode = line[len(p):]
  57. continue
  58. }
  59. fn, b, err := parseLine(line)
  60. if err != nil {
  61. return nil, fmt.Errorf("line %q doesn't match expected format: %v", line, err)
  62. }
  63. p := files[fn]
  64. if p == nil {
  65. p = &Profile{
  66. FileName: fn,
  67. Mode: mode,
  68. }
  69. files[fn] = p
  70. }
  71. p.Blocks = append(p.Blocks, b)
  72. }
  73. if err := s.Err(); err != nil {
  74. return nil, err
  75. }
  76. for _, p := range files {
  77. sort.Sort(blocksByStart(p.Blocks))
  78. // Merge samples from the same location.
  79. j := 1
  80. for i := 1; i < len(p.Blocks); i++ {
  81. b := p.Blocks[i]
  82. last := p.Blocks[j-1]
  83. if b.StartLine == last.StartLine &&
  84. b.StartCol == last.StartCol &&
  85. b.EndLine == last.EndLine &&
  86. b.EndCol == last.EndCol {
  87. if b.NumStmt != last.NumStmt {
  88. return nil, fmt.Errorf("inconsistent NumStmt: changed from %d to %d", last.NumStmt, b.NumStmt)
  89. }
  90. if mode == "set" {
  91. p.Blocks[j-1].Count |= b.Count
  92. } else {
  93. p.Blocks[j-1].Count += b.Count
  94. }
  95. continue
  96. }
  97. p.Blocks[j] = b
  98. j++
  99. }
  100. p.Blocks = p.Blocks[:j]
  101. }
  102. // Generate a sorted slice.
  103. profiles := make([]*Profile, 0, len(files))
  104. for _, profile := range files {
  105. profiles = append(profiles, profile)
  106. }
  107. sort.Sort(byFileName(profiles))
  108. return profiles, nil
  109. }
  110. // parseLine parses a line from a coverage file.
  111. // It is equivalent to the regex
  112. // ^(.+):([0-9]+)\.([0-9]+),([0-9]+)\.([0-9]+) ([0-9]+) ([0-9]+)$
  113. //
  114. // However, it is much faster: https://golang.org/cl/179377
  115. func parseLine(l string) (fileName string, block ProfileBlock, err error) {
  116. end := len(l)
  117. b := ProfileBlock{}
  118. b.Count, end, err = seekBack(l, ' ', end, "Count")
  119. if err != nil {
  120. return "", b, err
  121. }
  122. b.NumStmt, end, err = seekBack(l, ' ', end, "NumStmt")
  123. if err != nil {
  124. return "", b, err
  125. }
  126. b.EndCol, end, err = seekBack(l, '.', end, "EndCol")
  127. if err != nil {
  128. return "", b, err
  129. }
  130. b.EndLine, end, err = seekBack(l, ',', end, "EndLine")
  131. if err != nil {
  132. return "", b, err
  133. }
  134. b.StartCol, end, err = seekBack(l, '.', end, "StartCol")
  135. if err != nil {
  136. return "", b, err
  137. }
  138. b.StartLine, end, err = seekBack(l, ':', end, "StartLine")
  139. if err != nil {
  140. return "", b, err
  141. }
  142. fn := l[0:end]
  143. if fn == "" {
  144. return "", b, errors.New("a FileName cannot be blank")
  145. }
  146. return fn, b, nil
  147. }
  148. // seekBack searches backwards from end to find sep in l, then returns the
  149. // value between sep and end as an integer.
  150. // If seekBack fails, the returned error will reference what.
  151. func seekBack(l string, sep byte, end int, what string) (value int, nextSep int, err error) {
  152. // Since we're seeking backwards and we know only ASCII is legal for these values,
  153. // we can ignore the possibility of non-ASCII characters.
  154. for start := end - 1; start >= 0; start-- {
  155. if l[start] == sep {
  156. i, err := strconv.Atoi(l[start+1 : end])
  157. if err != nil {
  158. return 0, 0, fmt.Errorf("couldn't parse %q: %v", what, err)
  159. }
  160. if i < 0 {
  161. return 0, 0, fmt.Errorf("negative values are not allowed for %s, found %d", what, i)
  162. }
  163. return i, start, nil
  164. }
  165. }
  166. return 0, 0, fmt.Errorf("couldn't find a %s before %s", string(sep), what)
  167. }
  168. type blocksByStart []ProfileBlock
  169. func (b blocksByStart) Len() int { return len(b) }
  170. func (b blocksByStart) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  171. func (b blocksByStart) Less(i, j int) bool {
  172. bi, bj := b[i], b[j]
  173. return bi.StartLine < bj.StartLine || bi.StartLine == bj.StartLine && bi.StartCol < bj.StartCol
  174. }
  175. // Boundary represents the position in a source file of the beginning or end of a
  176. // block as reported by the coverage profile. In HTML mode, it will correspond to
  177. // the opening or closing of a <span> tag and will be used to colorize the source
  178. type Boundary struct {
  179. Offset int // Location as a byte offset in the source file.
  180. Start bool // Is this the start of a block?
  181. Count int // Event count from the cover profile.
  182. Norm float64 // Count normalized to [0..1].
  183. }
  184. // Boundaries returns a Profile as a set of Boundary objects within the provided src.
  185. func (p *Profile) Boundaries(src []byte) (boundaries []Boundary) {
  186. // Find maximum count.
  187. max := 0
  188. for _, b := range p.Blocks {
  189. if b.Count > max {
  190. max = b.Count
  191. }
  192. }
  193. // Divisor for normalization.
  194. divisor := math.Log(float64(max))
  195. // boundary returns a Boundary, populating the Norm field with a normalized Count.
  196. boundary := func(offset int, start bool, count int) Boundary {
  197. b := Boundary{Offset: offset, Start: start, Count: count}
  198. if !start || count == 0 {
  199. return b
  200. }
  201. if max <= 1 {
  202. b.Norm = 0.8 // Profile is in"set" mode; we want a heat map. Use cov8 in the CSS.
  203. } else if count > 0 {
  204. b.Norm = math.Log(float64(count)) / divisor
  205. }
  206. return b
  207. }
  208. line, col := 1, 2 // TODO: Why is this 2?
  209. for si, bi := 0, 0; si < len(src) && bi < len(p.Blocks); {
  210. b := p.Blocks[bi]
  211. if b.StartLine == line && b.StartCol == col {
  212. boundaries = append(boundaries, boundary(si, true, b.Count))
  213. }
  214. if b.EndLine == line && b.EndCol == col || line > b.EndLine {
  215. boundaries = append(boundaries, boundary(si, false, 0))
  216. bi++
  217. continue // Don't advance through src; maybe the next block starts here.
  218. }
  219. if src[si] == '\n' {
  220. line++
  221. col = 0
  222. }
  223. col++
  224. si++
  225. }
  226. sort.Sort(boundariesByPos(boundaries))
  227. return
  228. }
  229. type boundariesByPos []Boundary
  230. func (b boundariesByPos) Len() int { return len(b) }
  231. func (b boundariesByPos) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  232. func (b boundariesByPos) Less(i, j int) bool {
  233. if b[i].Offset == b[j].Offset {
  234. return !b[i].Start && b[j].Start
  235. }
  236. return b[i].Offset < b[j].Offset
  237. }