Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(604)

Delta Between Two Patch Sets: src/pkg/testing/testing.go

Issue 6443115: code review 6443115: pprof: add contention profiling (Closed)
Left Patch Set: diff -r 248e11862ed5 https://go.googlecode.com/hg/ Created 11 years, 6 months ago
Right Patch Set: diff -r 2aef5548a9cf https://go.googlecode.com/hg/ Created 11 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/runtime/signal_linux_arm.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 // Copyright 2009 The Go Authors. All rights reserved. 1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // Package testing provides support for automated testing of Go packages. 5 // Package testing provides support for automated testing of Go packages.
6 // It is intended to be used in concert with the ``go test'' command, which auto mates 6 // It is intended to be used in concert with the ``go test'' command, which auto mates
7 // execution of any function of the form 7 // execution of any function of the form
8 // func TestXxx(*testing.T) 8 // func TestXxx(*testing.T)
9 // where Xxx can be any alphanumeric string (but the first letter must not be in 9 // where Xxx can be any alphanumeric string (but the first letter must not be in
10 // [a-z]) and serves to identify the test routine. 10 // [a-z]) and serves to identify the test routine.
11 // These TestXxx routines should be declared within the package they are testing . 11 // These TestXxx routines should be declared within the package they are testing .
12 // 12 //
13 // Functions of the form 13 // Functions of the form
14 // func BenchmarkXxx(*testing.B) 14 // func BenchmarkXxx(*testing.B)
15 // are considered benchmarks, and are executed by the "go test" command when 15 // are considered benchmarks, and are executed by the "go test" command when
16 // the -test.bench flag is provided. 16 // the -test.bench flag is provided. Benchmarks are run sequentially.
17 //·
18 // For a description of the testing flags, see
19 // http://golang.org/cmd/go/#Description_of_testing_flags.
17 // 20 //
18 // A sample benchmark function looks like this: 21 // A sample benchmark function looks like this:
19 // func BenchmarkHello(b *testing.B) { 22 // func BenchmarkHello(b *testing.B) {
20 // for i := 0; i < b.N; i++ { 23 // for i := 0; i < b.N; i++ {
21 // fmt.Sprintf("hello") 24 // fmt.Sprintf("hello")
22 // } 25 // }
23 // } 26 // }
24 // 27 //
25 // The benchmark package will vary b.N until the benchmark function lasts 28 // The benchmark package will vary b.N until the benchmark function lasts
26 // long enough to be timed reliably. The output 29 // long enough to be timed reliably. The output
27 // testing.BenchmarkHello 10000000 282 ns/op 30 // BenchmarkHello 10000000 282 ns/op
28 // means that the loop ran 10000000 times at a speed of 282 ns per loop. 31 // means that the loop ran 10000000 times at a speed of 282 ns per loop.
29 // 32 //
30 // If a benchmark needs some expensive setup before running, the timer 33 // If a benchmark needs some expensive setup before running, the timer
31 // may be stopped: 34 // may be reset:
32 // func BenchmarkBigLen(b *testing.B) { 35 // func BenchmarkBigLen(b *testing.B) {
33 // b.StopTimer()
34 // big := NewBig() 36 // big := NewBig()
35 // b.StartTimer() 37 // b.ResetTimer()
36 // for i := 0; i < b.N; i++ { 38 // for i := 0; i < b.N; i++ {
37 // big.Len() 39 // big.Len()
38 // } 40 // }
39 // } 41 // }
40 // 42 //
41 // The package also runs and verifies example code. Example functions may 43 // The package also runs and verifies example code. Example functions may
42 // include a concluding comment that begins with "Output:" and is compared with 44 // include a concluding comment that begins with "Output:" and is compared with
43 // the standard output of the function when the tests are run, as in these 45 // the standard output of the function when the tests are run, as in these
44 // examples of an example: 46 // examples of an example:
45 // 47 //
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 95
94 var ( 96 var (
95 // The short flag requests that tests run more quickly, but its function ality 97 // The short flag requests that tests run more quickly, but its function ality
96 // is provided by test writers themselves. The testing package is just its 98 // is provided by test writers themselves. The testing package is just its
97 // home. The all.bash installation script sets it to make installation more 99 // home. The all.bash installation script sets it to make installation more
98 // efficient, but by default the flag is off so a plain "go test" will d o a 100 // efficient, but by default the flag is off so a plain "go test" will d o a
99 // full test of the package. 101 // full test of the package.
100 short = flag.Bool("test.short", false, "run smaller test suite to save t ime") 102 short = flag.Bool("test.short", false, "run smaller test suite to save t ime")
101 103
102 // Report as tests are run; default is silent for success. 104 // Report as tests are run; default is silent for success.
103 » chatty = flag.Bool("test.v", false, "verbose: print addit ional output") 105 » chatty = flag.Bool("test.v", false, "verbose: print additional output")
104 » match = flag.String("test.run", "", "regular expression to select tests and examples to run") 106 » match = flag.String("test.run", "", "regular expression to se lect tests and examples to run")
105 » memProfile = flag.String("test.memprofile", "", "write a memo ry profile to the named file after execution") 107 » memProfile = flag.String("test.memprofile", "", "write a memory pr ofile to the named file after execution")
106 » memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runtime.MemProfileRate") 108 » memProfileRate = flag.Int("test.memprofilerate", 0, "if >=0, sets runt ime.MemProfileRate")
107 » cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to the named file during execution") 109 » cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profi le to the named file during execution")
108 » contentionProfile = flag.String("test.contentionprofile", "", "write a contention profile to the named file after execution") 110 » blockProfile = flag.String("test.blockprofile", "", "write a gorouti ne blocking profile to the named file after execution")
109 » contentionProfileRate = flag.Int("test.contentionprofilerate", 1, "if >= 0, sets runtime.ContentionProfileRate") 111 » blockProfileRate = flag.Int("test.blockprofilerate", 1, "if >= 0, calls runtime.SetBlockProfileRate()")
110 » timeout = flag.Duration("test.timeout", 0, "if positive, s ets an aggregate time limit for all tests") 112 » timeout = flag.Duration("test.timeout", 0, "if positive, sets a n aggregate time limit for all tests")
111 » cpuListStr = flag.String("test.cpu", "", "comma-separated lis t of number of CPUs to use for each test") 113 » cpuListStr = flag.String("test.cpu", "", "comma-separated list of number of CPUs to use for each test")
112 » parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "maximum test parallelism") 114 » parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "max imum test parallelism")
113 115
114 haveExamples bool // are there examples? 116 haveExamples bool // are there examples?
115 117
116 cpuList []int 118 cpuList []int
117 ) 119 )
118 120
119 // common holds the elements common between T and B and 121 // common holds the elements common between T and B and
120 // captures common methods such as Errorf. 122 // captures common methods such as Errorf.
121 type common struct { 123 type common struct {
122 mu sync.RWMutex // guards output and failed 124 mu sync.RWMutex // guards output and failed
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 fmt.Fprintf(os.Stderr, "testing: %s", err) 415 fmt.Fprintf(os.Stderr, "testing: %s", err)
414 return 416 return
415 } 417 }
416 if err := pprof.StartCPUProfile(f); err != nil { 418 if err := pprof.StartCPUProfile(f); err != nil {
417 fmt.Fprintf(os.Stderr, "testing: can't start cpu profile : %s", err) 419 fmt.Fprintf(os.Stderr, "testing: can't start cpu profile : %s", err)
418 f.Close() 420 f.Close()
419 return 421 return
420 } 422 }
421 // Could save f so after can call f.Close; not worth the effort. 423 // Could save f so after can call f.Close; not worth the effort.
422 } 424 }
423 » if *contentionProfile != "" && *contentionProfileRate >= 0 { 425 » if *blockProfile != "" && *blockProfileRate >= 0 {
424 » » runtime.ContentionProfileRate = *contentionProfileRate 426 » » runtime.SetBlockProfileRate(*blockProfileRate)
425 } 427 }
426 } 428 }
427 429
428 // after runs after all testing. 430 // after runs after all testing.
429 func after() { 431 func after() {
430 if *cpuProfile != "" { 432 if *cpuProfile != "" {
431 pprof.StopCPUProfile() // flushes profile to disk 433 pprof.StopCPUProfile() // flushes profile to disk
432 } 434 }
433 if *memProfile != "" { 435 if *memProfile != "" {
434 f, err := os.Create(*memProfile) 436 f, err := os.Create(*memProfile)
435 if err != nil { 437 if err != nil {
436 fmt.Fprintf(os.Stderr, "testing: %s", err) 438 fmt.Fprintf(os.Stderr, "testing: %s", err)
437 return 439 return
438 } 440 }
439 if err = pprof.WriteHeapProfile(f); err != nil { 441 if err = pprof.WriteHeapProfile(f); err != nil {
440 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *m emProfile, err) 442 fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *m emProfile, err)
441 } 443 }
442 f.Close() 444 f.Close()
443 } 445 }
444 » if *contentionProfile != "" { 446 » if *blockProfile != "" && *blockProfileRate >= 0 {
445 » » f, err := os.Create(*contentionProfile) 447 » » f, err := os.Create(*blockProfile)
446 if err != nil { 448 if err != nil {
447 fmt.Fprintf(os.Stderr, "testing: %s", err) 449 fmt.Fprintf(os.Stderr, "testing: %s", err)
448 return 450 return
449 } 451 }
450 » » if err = pprof.Lookup("contention").WriteTo(f, 0); err != nil { 452 » » if err = pprof.Lookup("block").WriteTo(f, 0); err != nil {
451 » » » fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *c ontentionProfile, err) 453 » » » fmt.Fprintf(os.Stderr, "testing: can't write %s: %s", *b lockProfile, err)
452 } 454 }
453 f.Close() 455 f.Close()
454 } 456 }
455 } 457 }
456 458
457 var timer *time.Timer 459 var timer *time.Timer
458 460
459 // startAlarm starts an alarm if requested. 461 // startAlarm starts an alarm if requested.
460 func startAlarm() { 462 func startAlarm() {
461 if *timeout > 0 { 463 if *timeout > 0 {
(...skipping 20 matching lines...) Expand all
482 for _, val := range strings.Split(*cpuListStr, ",") { 484 for _, val := range strings.Split(*cpuListStr, ",") {
483 cpu, err := strconv.Atoi(val) 485 cpu, err := strconv.Atoi(val)
484 if err != nil || cpu <= 0 { 486 if err != nil || cpu <= 0 {
485 fmt.Fprintf(os.Stderr, "testing: invalid value % q for -test.cpu", val) 487 fmt.Fprintf(os.Stderr, "testing: invalid value % q for -test.cpu", val)
486 os.Exit(1) 488 os.Exit(1)
487 } 489 }
488 cpuList = append(cpuList, cpu) 490 cpuList = append(cpuList, cpu)
489 } 491 }
490 } 492 }
491 } 493 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b