Merge lp:~freeman-wes/gocheck/gocheck into lp:gocheck

Proposed by Wes Freeman
Status: Needs review
Proposed branch: lp:~freeman-wes/gocheck/gocheck
Merge into: lp:gocheck
Diff against target: 200 lines (+57/-24)
3 files modified
benchmark.go (+28/-1)
gocheck.go (+22/-18)
run.go (+7/-5)
To merge this branch: bzr merge lp:~freeman-wes/gocheck/gocheck
Reviewer Review Type Date Requested Status
Gustavo Niemeyer Pending
Review via email: mp+199915@code.launchpad.net

Description of the change

Adding -benchmem functionality with -gocheck.bmem.

To post a comment you must log in.
lp:~freeman-wes/gocheck/gocheck updated
87. By Wes Freeman

adding another tab before memstats

Unmerged revisions

87. By Wes Freeman

adding another tab before memstats

86. By Wes Freeman

added benchmem functionality

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'benchmark.go'
2--- benchmark.go 2013-03-02 02:47:45 +0000
3+++ benchmark.go 2013-12-22 09:38:42 +0000
4@@ -6,9 +6,12 @@
5
6 import (
7 "fmt"
8+ "runtime"
9 "time"
10 )
11
12+var memStats runtime.MemStats
13+
14 // testingB is a type passed to Benchmark functions to manage benchmark
15 // timing and to specify the number of iterations to run.
16 type timer struct {
17@@ -18,6 +21,12 @@
18 bytes int64
19 timerOn bool
20 benchTime time.Duration
21+ // The initial states of memStats.Mallocs and memStats.TotalAlloc.
22+ startAllocs uint64
23+ startBytes uint64
24+ // The net total of this test after being run.
25+ netAllocs uint64
26+ netBytes uint64
27 }
28
29 // StartTimer starts timing a test. This function is called automatically
30@@ -27,6 +36,10 @@
31 if !c.timerOn {
32 c.start = time.Now()
33 c.timerOn = true
34+
35+ runtime.ReadMemStats(&memStats)
36+ c.startAllocs = memStats.Mallocs
37+ c.startBytes = memStats.TotalAlloc
38 }
39 }
40
41@@ -37,6 +50,9 @@
42 if c.timerOn {
43 c.duration += time.Now().Sub(c.start)
44 c.timerOn = false
45+ runtime.ReadMemStats(&memStats)
46+ c.netAllocs += memStats.Mallocs - c.startAllocs
47+ c.netBytes += memStats.TotalAlloc - c.startBytes
48 }
49 }
50
51@@ -45,8 +61,13 @@
52 func (c *C) ResetTimer() {
53 if c.timerOn {
54 c.start = time.Now()
55+ runtime.ReadMemStats(&memStats)
56+ c.startAllocs = memStats.Mallocs
57+ c.startBytes = memStats.TotalAlloc
58 }
59 c.duration = 0
60+ c.netAllocs = 0
61+ c.netBytes = 0
62 }
63
64 // SetBytes informs the number of bytes that the benchmark processes
65@@ -90,7 +111,13 @@
66 ns = fmt.Sprintf("%12.1f ns/op", float64(c.duration.Nanoseconds())/float64(c.N))
67 }
68 }
69- return fmt.Sprintf("%8d\t%s%s", c.N, ns, mb)
70+ memStats := ""
71+ if c.benchMem {
72+ allocedBytes := fmt.Sprintf("%8d B/op", int64(c.netBytes)/int64(c.N))
73+ allocs := fmt.Sprintf("%8d allocs/op", int64(c.netAllocs)/int64(c.N))
74+ memStats = fmt.Sprintf("\t%s\t%s", allocedBytes, allocs)
75+ }
76+ return fmt.Sprintf("%8d\t%s%s%s", c.N, ns, mb, memStats)
77 }
78
79 func min(x, y int) int {
80
81=== modified file 'gocheck.go'
82--- gocheck.go 2013-03-02 02:47:45 +0000
83+++ gocheck.go 2013-12-22 09:38:42 +0000
84@@ -62,7 +62,7 @@
85 }
86
87 func (method *methodType) String() string {
88- return method.suiteName()+"."+method.Info.Name
89+ return method.suiteName() + "." + method.Info.Name
90 }
91
92 func (method *methodType) matches(re *regexp.Regexp) bool {
93@@ -72,15 +72,16 @@
94 }
95
96 type C struct {
97- method *methodType
98- kind funcKind
99- status funcStatus
100- logb *logger
101- logw io.Writer
102- done chan *C
103- reason string
104- mustFail bool
105- tempDir *tempDir
106+ method *methodType
107+ kind funcKind
108+ status funcStatus
109+ logb *logger
110+ logw io.Writer
111+ done chan *C
112+ reason string
113+ mustFail bool
114+ tempDir *tempDir
115+ benchMem bool
116 timer
117 }
118
119@@ -495,6 +496,7 @@
120 output *outputWriter
121 reportedProblemLast bool
122 benchTime time.Duration
123+ benchMem bool
124 }
125
126 type RunConf struct {
127@@ -504,6 +506,7 @@
128 Filter string
129 Benchmark bool
130 BenchmarkTime time.Duration // Defaults to 1 second
131+ BenchmarkMem bool
132 }
133
134 // Create a new suiteRunner able to run all methods in the given suite.
135@@ -528,6 +531,7 @@
136 output: newOutputWriter(conf.Output, conf.Stream, conf.Verbose),
137 tracker: newResultTracker(),
138 benchTime: conf.BenchmarkTime,
139+ benchMem: conf.BenchmarkMem,
140 }
141 runner.tests = make([]*methodType, 0, suiteNumMethods)
142 runner.tempDir = new(tempDir)
143@@ -613,13 +617,14 @@
144 logb = new(logger)
145 }
146 c := &C{
147- method: method,
148- kind: kind,
149- logb: logb,
150- logw: logw,
151- tempDir: runner.tempDir,
152- done: make(chan *C, 1),
153- timer: timer{benchTime: runner.benchTime},
154+ method: method,
155+ kind: kind,
156+ logb: logb,
157+ logw: logw,
158+ tempDir: runner.tempDir,
159+ done: make(chan *C, 1),
160+ timer: timer{benchTime: runner.benchTime},
161+ benchMem: runner.benchMem,
162 }
163 runner.tracker.expectCall(c)
164 go (func() {
165@@ -913,4 +918,3 @@
166 return fmt.Sprintf("%s%s: %s: %s%s", prefix, label, niceFuncPath(pc),
167 niceFuncName(pc), suffix)
168 }
169-
170
171=== modified file 'run.go'
172--- run.go 2013-03-01 22:15:07 +0000
173+++ run.go 2013-12-22 09:38:42 +0000
174@@ -30,7 +30,8 @@
175 verboseFlag = flag.Bool("gocheck.v", false, "Verbose mode")
176 streamFlag = flag.Bool("gocheck.vv", false, "Super verbose mode (disables output caching)")
177 benchFlag = flag.Bool("gocheck.b", false, "Run benchmarks")
178- benchTime = flag.Duration("gocheck.btime", 1 * time.Second, "approximate run time for each benchmark")
179+ benchTime = flag.Duration("gocheck.btime", 1*time.Second, "approximate run time for each benchmark")
180+ benchMem = flag.Bool("gocheck.bmem", false, "Report memory benchmarks")
181 listFlag = flag.Bool("gocheck.list", false, "List the names of all tests that will be run")
182 )
183
184@@ -39,11 +40,12 @@
185 // module.
186 func TestingT(testingT *testing.T) {
187 conf := &RunConf{
188- Filter: *filterFlag,
189- Verbose: *verboseFlag,
190- Stream: *streamFlag,
191- Benchmark: *benchFlag,
192+ Filter: *filterFlag,
193+ Verbose: *verboseFlag,
194+ Stream: *streamFlag,
195+ Benchmark: *benchFlag,
196 BenchmarkTime: *benchTime,
197+ BenchmarkMem: *benchMem,
198 }
199 if *listFlag {
200 w := bufio.NewWriter(os.Stdout)

Subscribers

People subscribed via source and target branches