You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
5.7 KiB

  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. "fmt"
  10. "math"
  11. "os"
  12. "regexp"
  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. m := lineRe.FindStringSubmatch(line)
  60. if m == nil {
  61. return nil, fmt.Errorf("line %q doesn't match expected format: %v", line, lineRe)
  62. }
  63. fn := m[1]
  64. p := files[fn]
  65. if p == nil {
  66. p = &Profile{
  67. FileName: fn,
  68. Mode: mode,
  69. }
  70. files[fn] = p
  71. }
  72. p.Blocks = append(p.Blocks, ProfileBlock{
  73. StartLine: toInt(m[2]),
  74. StartCol: toInt(m[3]),
  75. EndLine: toInt(m[4]),
  76. EndCol: toInt(m[5]),
  77. NumStmt: toInt(m[6]),
  78. Count: toInt(m[7]),
  79. })
  80. }
  81. if err := s.Err(); err != nil {
  82. return nil, err
  83. }
  84. for _, p := range files {
  85. sort.Sort(blocksByStart(p.Blocks))
  86. // Merge samples from the same location.
  87. j := 1
  88. for i := 1; i < len(p.Blocks); i++ {
  89. b := p.Blocks[i]
  90. last := p.Blocks[j-1]
  91. if b.StartLine == last.StartLine &&
  92. b.StartCol == last.StartCol &&
  93. b.EndLine == last.EndLine &&
  94. b.EndCol == last.EndCol {
  95. if b.NumStmt != last.NumStmt {
  96. return nil, fmt.Errorf("inconsistent NumStmt: changed from %d to %d", last.NumStmt, b.NumStmt)
  97. }
  98. if mode == "set" {
  99. p.Blocks[j-1].Count |= b.Count
  100. } else {
  101. p.Blocks[j-1].Count += b.Count
  102. }
  103. continue
  104. }
  105. p.Blocks[j] = b
  106. j++
  107. }
  108. p.Blocks = p.Blocks[:j]
  109. }
  110. // Generate a sorted slice.
  111. profiles := make([]*Profile, 0, len(files))
  112. for _, profile := range files {
  113. profiles = append(profiles, profile)
  114. }
  115. sort.Sort(byFileName(profiles))
  116. return profiles, nil
  117. }
  118. type blocksByStart []ProfileBlock
  119. func (b blocksByStart) Len() int { return len(b) }
  120. func (b blocksByStart) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  121. func (b blocksByStart) Less(i, j int) bool {
  122. bi, bj := b[i], b[j]
  123. return bi.StartLine < bj.StartLine || bi.StartLine == bj.StartLine && bi.StartCol < bj.StartCol
  124. }
  125. var lineRe = regexp.MustCompile(`^(.+):([0-9]+).([0-9]+),([0-9]+).([0-9]+) ([0-9]+) ([0-9]+)$`)
  126. func toInt(s string) int {
  127. i, err := strconv.Atoi(s)
  128. if err != nil {
  129. panic(err)
  130. }
  131. return i
  132. }
  133. // Boundary represents the position in a source file of the beginning or end of a
  134. // block as reported by the coverage profile. In HTML mode, it will correspond to
  135. // the opening or closing of a <span> tag and will be used to colorize the source
  136. type Boundary struct {
  137. Offset int // Location as a byte offset in the source file.
  138. Start bool // Is this the start of a block?
  139. Count int // Event count from the cover profile.
  140. Norm float64 // Count normalized to [0..1].
  141. }
  142. // Boundaries returns a Profile as a set of Boundary objects within the provided src.
  143. func (p *Profile) Boundaries(src []byte) (boundaries []Boundary) {
  144. // Find maximum count.
  145. max := 0
  146. for _, b := range p.Blocks {
  147. if b.Count > max {
  148. max = b.Count
  149. }
  150. }
  151. // Divisor for normalization.
  152. divisor := math.Log(float64(max))
  153. // boundary returns a Boundary, populating the Norm field with a normalized Count.
  154. boundary := func(offset int, start bool, count int) Boundary {
  155. b := Boundary{Offset: offset, Start: start, Count: count}
  156. if !start || count == 0 {
  157. return b
  158. }
  159. if max <= 1 {
  160. b.Norm = 0.8 // Profile is in"set" mode; we want a heat map. Use cov8 in the CSS.
  161. } else if count > 0 {
  162. b.Norm = math.Log(float64(count)) / divisor
  163. }
  164. return b
  165. }
  166. line, col := 1, 2 // TODO: Why is this 2?
  167. for si, bi := 0, 0; si < len(src) && bi < len(p.Blocks); {
  168. b := p.Blocks[bi]
  169. if b.StartLine == line && b.StartCol == col {
  170. boundaries = append(boundaries, boundary(si, true, b.Count))
  171. }
  172. if b.EndLine == line && b.EndCol == col || line > b.EndLine {
  173. boundaries = append(boundaries, boundary(si, false, 0))
  174. bi++
  175. continue // Don't advance through src; maybe the next block starts here.
  176. }
  177. if src[si] == '\n' {
  178. line++
  179. col = 0
  180. }
  181. col++
  182. si++
  183. }
  184. sort.Sort(boundariesByPos(boundaries))
  185. return
  186. }
  187. type boundariesByPos []Boundary
  188. func (b boundariesByPos) Len() int { return len(b) }
  189. func (b boundariesByPos) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  190. func (b boundariesByPos) Less(i, j int) bool {
  191. if b[i].Offset == b[j].Offset {
  192. return !b[i].Start && b[j].Start
  193. }
  194. return b[i].Offset < b[j].Offset
  195. }