diff options
Diffstat (limited to 'libgo/go/testing/testing.go')
-rw-r--r-- | libgo/go/testing/testing.go | 58 |
1 files changed, 40 insertions, 18 deletions
diff --git a/libgo/go/testing/testing.go b/libgo/go/testing/testing.go index e56b77c9c77..60edbd55ebd 100644 --- a/libgo/go/testing/testing.go +++ b/libgo/go/testing/testing.go @@ -13,7 +13,10 @@ // Functions of the form // func BenchmarkXxx(*testing.B) // are considered benchmarks, and are executed by the "go test" command when -// the -test.bench flag is provided. +// the -test.bench flag is provided. Benchmarks are run sequentially. +// +// For a description of the testing flags, see +// http://golang.org/cmd/go/#Description_of_testing_flags. // // A sample benchmark function looks like this: // func BenchmarkHello(b *testing.B) { @@ -24,15 +27,14 @@ // // The benchmark package will vary b.N until the benchmark function lasts // long enough to be timed reliably. The output -// testing.BenchmarkHello 10000000 282 ns/op +// BenchmarkHello 10000000 282 ns/op // means that the loop ran 10000000 times at a speed of 282 ns per loop. // // If a benchmark needs some expensive setup before running, the timer -// may be stopped: +// may be reset: // func BenchmarkBigLen(b *testing.B) { -// b.StopTimer() // big := NewBig() -// b.StartTimer() +// b.ResetTimer() // for i := 0; i < b.N; i++ { // big.Len() // } @@ -100,14 +102,16 @@ var ( short = flag.Bool("test.short", false, "run smaller test suite to save time") // Report as tests are run; default is silent for success. - chatty = flag.Bool("test.v", false, "verbose: print additional output") - match = flag.String("test.run", "", "regular expression to select tests and examples to run") - memProfile = flag.String("test.memprofile", "", "write a memory profile to the named file after execution") - memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate") - cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution") - timeout = flag.Duration("test.timeout", 0, "if positive, sets an aggregate time limit for all tests") - cpuListStr = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test") - parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism") + chatty = flag.Bool("test.v", false, "verbose: print additional output") + match = flag.String("test.run", "", "regular expression to select tests and examples to run") + memProfile = flag.String("test.memprofile", "", "write a memory profile to the named file after execution") + memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate") + cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution") + blockProfile = flag.String("test.blockprofile", "", "write a goroutine blocking profile to the named file after execution") + blockProfileRate = flag.Int("test.blockprofilerate", 1, "if >= 0, calls runtime.SetBlockProfileRate()") + timeout = flag.Duration("test.timeout", 0, "if positive, sets an aggregate time limit for all tests") + cpuListStr = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test") + parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism") haveExamples bool // are there examples? @@ -132,6 +136,11 @@ func Short() bool { return *short } +// Verbose reports whether the -test.v flag is set. +func Verbose() bool { + return *chatty +} + // decorate prefixes the string with the file and line of the call site // and inserts the final newline if needed and indentation tabs for formatting. func decorate(s string) string { @@ -151,6 +160,9 @@ func decorate(s string) string { fmt.Fprintf(buf, "%s:%d: ", file, line) lines := strings.Split(s, "\n") + if l := len(lines); l > 1 && lines[l-1] == "" { + lines = lines[:l-1] + } for i, line := range lines { if i > 0 { buf.WriteByte('\n') @@ -163,10 +175,7 @@ func decorate(s string) string { } buf.WriteString(line) } - if l := len(s); l > 0 && s[len(s)-1] != '\n' { - // Add final new line if needed. - buf.WriteByte('\n') - } + buf.WriteByte('\n') return buf.String() } @@ -413,7 +422,9 @@ func before() { } // Could save f so after can call f.Close; not worth the effort. } - + if *blockProfile != "" && *blockProfileRate >= 0 { + runtime.SetBlockProfileRate(*blockProfileRate) + } } // after runs after all testing. @@ -432,6 +443,17 @@ func after() { } f.Close() } + if *blockProfile != "" && *blockProfileRate >= 0 { + f, err := os.Create(*blockProfile) + if err != nil { + fmt.Fprintf(os.Stderr, "testing: %s", err) + return + } + if err = pprof.Lookup("block").WriteTo(f, 0); err != nil { + fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *blockProfile, err) + } + f.Close() + } } var timer *time.Timer |