diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-12-13 19:16:27 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2011-12-13 19:16:27 +0000 |
commit | 7b1c3dd9e670da2041ff1af415999310f88888ad (patch) | |
tree | c5132538d5da85ed816c7e1f9d93c4a503b838ab /libgo/go/testing | |
parent | 36cfbee133027429a681ce585643d38228ab1213 (diff) | |
download | gcc-7b1c3dd9e670da2041ff1af415999310f88888ad.tar.gz |
libgo: Update to weekly.2011-12-02.
From-SVN: r182295
Diffstat (limited to 'libgo/go/testing')
-rw-r--r-- | libgo/go/testing/benchmark.go | 44 | ||||
-rw-r--r-- | libgo/go/testing/example.go | 6 | ||||
-rw-r--r-- | libgo/go/testing/testing.go | 21 |
3 files changed, 37 insertions, 34 deletions
diff --git a/libgo/go/testing/benchmark.go b/libgo/go/testing/benchmark.go index 4f049a31f75..e81e5c5845c 100644 --- a/libgo/go/testing/benchmark.go +++ b/libgo/go/testing/benchmark.go @@ -27,17 +27,19 @@ type InternalBenchmark struct { type B struct { N int benchmark InternalBenchmark - ns int64 + ns time.Duration bytes int64 - start int64 + start time.Time + timerOn bool } // StartTimer starts timing a test. This function is called automatically // before a benchmark starts, but it can also used to resume timing after // a call to StopTimer. func (b *B) StartTimer() { - if b.start == 0 { - b.start = time.Nanoseconds() + if !b.timerOn { + b.start = time.Now() + b.timerOn = true } } @@ -45,17 +47,17 @@ func (b *B) StartTimer() { // while performing complex initialization that you don't // want to measure. func (b *B) StopTimer() { - if b.start > 0 { - b.ns += time.Nanoseconds() - b.start + if b.timerOn { + b.ns += time.Now().Sub(b.start) + b.timerOn = false } - b.start = 0 } // ResetTimer sets the elapsed benchmark time to zero. // It does not affect whether the timer is running. func (b *B) ResetTimer() { - if b.start > 0 { - b.start = time.Nanoseconds() + if b.timerOn { + b.start = time.Now() } b.ns = 0 } @@ -68,7 +70,7 @@ func (b *B) nsPerOp() int64 { if b.N <= 0 { return 0 } - return b.ns / int64(b.N) + return b.ns.Nanoseconds() / int64(b.N) } // runN runs a single benchmark for the specified number of iterations. @@ -134,14 +136,14 @@ func (b *B) run() BenchmarkResult { n := 1 b.runN(n) // Run the benchmark for at least the specified amount of time. - time := int64(*benchTime * 1e9) - for b.ns < time && n < 1e9 { + d := time.Duration(*benchTime * float64(time.Second)) + for b.ns < d && n < 1e9 { last := n // Predict iterations/sec. if b.nsPerOp() == 0 { n = 1e9 } else { - n = int(time / b.nsPerOp()) + n = int(d.Nanoseconds() / b.nsPerOp()) } // Run more iterations than we think we'll need for a second (1.5x). // Don't grow too fast in case we had timing errors previously. @@ -156,23 +158,23 @@ func (b *B) run() BenchmarkResult { // The results of a benchmark run. type BenchmarkResult struct { - N int // The number of iterations. - Ns int64 // The total time taken. - Bytes int64 // Bytes processed in one iteration. + N int // The number of iterations. + T time.Duration // The total time taken. + Bytes int64 // Bytes processed in one iteration. } func (r BenchmarkResult) NsPerOp() int64 { if r.N <= 0 { return 0 } - return r.Ns / int64(r.N) + return r.T.Nanoseconds() / int64(r.N) } func (r BenchmarkResult) mbPerSec() float64 { - if r.Bytes <= 0 || r.Ns <= 0 || r.N <= 0 { + if r.Bytes <= 0 || r.T <= 0 || r.N <= 0 { return 0 } - return float64(r.Bytes) * float64(r.N) / float64(r.Ns) * 1e3 + return (float64(r.Bytes) * float64(r.N) / 1e6) / r.T.Seconds() } func (r BenchmarkResult) String() string { @@ -187,9 +189,9 @@ func (r BenchmarkResult) String() string { // The format specifiers here make sure that // the ones digits line up for all three possible formats. if nsop < 10 { - ns = fmt.Sprintf("%13.2f ns/op", float64(r.Ns)/float64(r.N)) + ns = fmt.Sprintf("%13.2f ns/op", float64(r.T.Nanoseconds())/float64(r.N)) } else { - ns = fmt.Sprintf("%12.1f ns/op", float64(r.Ns)/float64(r.N)) + ns = fmt.Sprintf("%12.1f ns/op", float64(r.T.Nanoseconds())/float64(r.N)) } } return fmt.Sprintf("%8d\t%s%s", r.N, ns, mb) diff --git a/libgo/go/testing/example.go b/libgo/go/testing/example.go index 3b026ee66e0..e23f13b6f16 100644 --- a/libgo/go/testing/example.go +++ b/libgo/go/testing/example.go @@ -56,9 +56,9 @@ func RunExamples(examples []InternalExample) (ok bool) { }() // run example - ns := -time.Nanoseconds() + t0 := time.Now() eg.F() - ns += time.Nanoseconds() + dt := time.Now().Sub(t0) // close pipe, restore stdout/stderr, get output w.Close() @@ -66,7 +66,7 @@ func RunExamples(examples []InternalExample) (ok bool) { out := <-outC // report any errors - tstr := fmt.Sprintf("(%.2f seconds)", float64(ns)/1e9) + tstr := fmt.Sprintf("(%.2f seconds)", dt.Seconds()) if out != eg.Output { fmt.Printf( "--- FAIL: %s %s\ngot:\n%s\nwant:\n%s\n", diff --git a/libgo/go/testing/testing.go b/libgo/go/testing/testing.go index 08443a31259..0b3a07108cc 100644 --- a/libgo/go/testing/testing.go +++ b/libgo/go/testing/testing.go @@ -111,12 +111,13 @@ func decorate(s string, addFileLine bool) string { // T is a type passed to Test functions to manage test state and support formatted test logs. // Logs are accumulated during execution and dumped to standard error when done. type T struct { - name string // Name of test. - errors string // Error string from test. - failed bool // Test has failed. - ch chan *T // Output for serial tests. - startParallel chan bool // Parallel tests will wait on this. - ns int64 // Duration of test in nanoseconds. + name string // Name of test. + errors string // Error string from test. + failed bool // Test has failed. + ch chan *T // Output for serial tests. + startParallel chan bool // Parallel tests will wait on this. + start time.Time // Time test started + dt time.Duration // Length of test } // Fail marks the Test function as having failed but continues execution. @@ -128,7 +129,7 @@ func (t *T) Failed() bool { return t.failed } // FailNow marks the Test function as having failed and stops its execution. // Execution will continue at the next Test. func (t *T) FailNow() { - t.ns = time.Nanoseconds() - t.ns + t.dt = time.Now().Sub(t.start) t.Fail() t.ch <- t runtime.Goexit() @@ -184,9 +185,9 @@ type InternalTest struct { } func tRunner(t *T, test *InternalTest) { - t.ns = time.Nanoseconds() + t.start = time.Now() test.F(t) - t.ns = time.Nanoseconds() - t.ns + t.dt = time.Now().Sub(t.start) t.ch <- t } @@ -211,7 +212,7 @@ func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, } func report(t *T) { - tstr := fmt.Sprintf("(%.2f seconds)", float64(t.ns)/1e9) + tstr := fmt.Sprintf("(%.2f seconds)", t.dt.Seconds()) format := "--- %s: %s %s\n%s" if t.failed { fmt.Printf(format, "FAIL", t.name, tstr, t.errors) |