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.

156 lines
5.2 KiB

  1. // Copyright 2014 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 main
  5. import (
  6. "fmt"
  7. "math"
  8. "golang.org/x/tools/benchmark/parse"
  9. )
  10. // BenchCmp is a pair of benchmarks.
  11. type BenchCmp struct {
  12. Before *parse.Benchmark
  13. After *parse.Benchmark
  14. }
  15. // Correlate correlates benchmarks from two BenchSets.
  16. func Correlate(before, after parse.Set) (cmps []BenchCmp, warnings []string) {
  17. cmps = make([]BenchCmp, 0, len(after))
  18. for name, beforebb := range before {
  19. afterbb := after[name]
  20. if len(beforebb) != len(afterbb) {
  21. warnings = append(warnings, fmt.Sprintf("ignoring %s: before has %d instances, after has %d", name, len(beforebb), len(afterbb)))
  22. continue
  23. }
  24. for i, beforeb := range beforebb {
  25. afterb := afterbb[i]
  26. cmps = append(cmps, BenchCmp{beforeb, afterb})
  27. }
  28. }
  29. return
  30. }
  31. func (c BenchCmp) Name() string { return c.Before.Name }
  32. func (c BenchCmp) String() string { return fmt.Sprintf("<%s, %s>", c.Before, c.After) }
  33. func (c BenchCmp) Measured(flag int) bool { return (c.Before.Measured & c.After.Measured & flag) != 0 }
  34. func (c BenchCmp) DeltaNsPerOp() Delta { return Delta{c.Before.NsPerOp, c.After.NsPerOp} }
  35. func (c BenchCmp) DeltaMBPerS() Delta { return Delta{c.Before.MBPerS, c.After.MBPerS} }
  36. func (c BenchCmp) DeltaAllocedBytesPerOp() Delta {
  37. return Delta{float64(c.Before.AllocedBytesPerOp), float64(c.After.AllocedBytesPerOp)}
  38. }
  39. func (c BenchCmp) DeltaAllocsPerOp() Delta {
  40. return Delta{float64(c.Before.AllocsPerOp), float64(c.After.AllocsPerOp)}
  41. }
  42. // Delta is the before and after value for a benchmark measurement.
  43. // Both must be non-negative.
  44. type Delta struct {
  45. Before float64
  46. After float64
  47. }
  48. // mag calculates the magnitude of a change, regardless of the direction of
  49. // the change. mag is intended for sorting and has no independent meaning.
  50. func (d Delta) mag() float64 {
  51. switch {
  52. case d.Before != 0 && d.After != 0 && d.Before >= d.After:
  53. return d.After / d.Before
  54. case d.Before != 0 && d.After != 0 && d.Before < d.After:
  55. return d.Before / d.After
  56. case d.Before == 0 && d.After == 0:
  57. return 1
  58. default:
  59. // 0 -> 1 or 1 -> 0
  60. // These are significant changes and worth surfacing.
  61. return math.Inf(1)
  62. }
  63. }
  64. // Changed reports whether the benchmark quantities are different.
  65. func (d Delta) Changed() bool { return d.Before != d.After }
  66. // Float64 returns After / Before. If Before is 0, Float64 returns
  67. // 1 if After is also 0, and +Inf otherwise.
  68. func (d Delta) Float64() float64 {
  69. switch {
  70. case d.Before != 0:
  71. return d.After / d.Before
  72. case d.After == 0:
  73. return 1
  74. default:
  75. return math.Inf(1)
  76. }
  77. }
  78. // Percent formats a Delta as a percent change, ranging from -100% up.
  79. func (d Delta) Percent() string {
  80. return fmt.Sprintf("%+.2f%%", 100*d.Float64()-100)
  81. }
  82. // Multiple formats a Delta as a multiplier, ranging from 0.00x up.
  83. func (d Delta) Multiple() string {
  84. return fmt.Sprintf("%.2fx", d.Float64())
  85. }
  86. func (d Delta) String() string {
  87. return fmt.Sprintf("Δ(%f, %f)", d.Before, d.After)
  88. }
  89. // ByParseOrder sorts BenchCmps to match the order in
  90. // which the Before benchmarks were presented to Parse.
  91. type ByParseOrder []BenchCmp
  92. func (x ByParseOrder) Len() int { return len(x) }
  93. func (x ByParseOrder) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  94. func (x ByParseOrder) Less(i, j int) bool { return x[i].Before.Ord < x[j].Before.Ord }
  95. // lessByDelta provides lexicographic ordering:
  96. // * largest delta by magnitude
  97. // * alphabetic by name
  98. func lessByDelta(i, j BenchCmp, calcDelta func(BenchCmp) Delta) bool {
  99. iDelta, jDelta := calcDelta(i).mag(), calcDelta(j).mag()
  100. if iDelta != jDelta {
  101. return iDelta < jDelta
  102. }
  103. return i.Name() < j.Name()
  104. }
  105. // ByDeltaNsPerOp sorts BenchCmps lexicographically by change
  106. // in ns/op, descending, then by benchmark name.
  107. type ByDeltaNsPerOp []BenchCmp
  108. func (x ByDeltaNsPerOp) Len() int { return len(x) }
  109. func (x ByDeltaNsPerOp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  110. func (x ByDeltaNsPerOp) Less(i, j int) bool { return lessByDelta(x[i], x[j], BenchCmp.DeltaNsPerOp) }
  111. // ByDeltaMBPerS sorts BenchCmps lexicographically by change
  112. // in MB/s, descending, then by benchmark name.
  113. type ByDeltaMBPerS []BenchCmp
  114. func (x ByDeltaMBPerS) Len() int { return len(x) }
  115. func (x ByDeltaMBPerS) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  116. func (x ByDeltaMBPerS) Less(i, j int) bool { return lessByDelta(x[i], x[j], BenchCmp.DeltaMBPerS) }
  117. // ByDeltaAllocedBytesPerOp sorts BenchCmps lexicographically by change
  118. // in B/op, descending, then by benchmark name.
  119. type ByDeltaAllocedBytesPerOp []BenchCmp
  120. func (x ByDeltaAllocedBytesPerOp) Len() int { return len(x) }
  121. func (x ByDeltaAllocedBytesPerOp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  122. func (x ByDeltaAllocedBytesPerOp) Less(i, j int) bool {
  123. return lessByDelta(x[i], x[j], BenchCmp.DeltaAllocedBytesPerOp)
  124. }
  125. // ByDeltaAllocsPerOp sorts BenchCmps lexicographically by change
  126. // in allocs/op, descending, then by benchmark name.
  127. type ByDeltaAllocsPerOp []BenchCmp
  128. func (x ByDeltaAllocsPerOp) Len() int { return len(x) }
  129. func (x ByDeltaAllocsPerOp) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
  130. func (x ByDeltaAllocsPerOp) Less(i, j int) bool {
  131. return lessByDelta(x[i], x[j], BenchCmp.DeltaAllocsPerOp)
  132. }