Created
January 5, 2022 07:33
-
-
Save kougazhang/ab215d4dbad42ac5b676cbff0806ee4d to your computer and use it in GitHub Desktop.
#golang #geektime
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "time" | |
import "sync" | |
import "flag" | |
import "os" | |
import "log" | |
import "runtime" | |
import "runtime/pprof" | |
import "runtime/trace" | |
import "fmt" | |
import "testing" | |
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") | |
var memprofile = flag.String("memprofile", "", "write memory profile to `file`") | |
var mutexprofile = flag.String("mutexprofile", "", "write memory profile to `file`") | |
func TestMutex(t *testing.T) { | |
flag.Parse() | |
f, _ := os.Create("trace.out") | |
defer f.Close() | |
trace.Start(f) | |
defer trace.Stop() | |
runtime.SetMutexProfileFraction(1) | |
if *cpuprofile != "" { | |
f, err := os.Create(*cpuprofile) | |
if err != nil { | |
log.Fatal("could not create CPU profile: ", err) | |
} | |
defer f.Close() // error handling omitted for example | |
if err := pprof.StartCPUProfile(f); err != nil { | |
log.Fatal("could not start CPU profile: ", err) | |
} | |
defer pprof.StopCPUProfile() | |
} | |
// ... rest of the program ... | |
if *memprofile != "" { | |
f, err := os.Create(*memprofile) | |
if err != nil { | |
log.Fatal("could not create memory profile: ", err) | |
} | |
defer f.Close() // error handling omitted for example | |
runtime.GC() // get up-to-date statistics | |
if err := pprof.WriteHeapProfile(f); err != nil { | |
log.Fatal("could not write memory profile: ", err) | |
} | |
} | |
var acquired1 int | |
var acquired2 int | |
done := make(chan bool, 1) | |
var mu sync.Mutex | |
// goroutine1 | |
go func() { | |
for { | |
select { | |
case <-done: | |
return | |
default: | |
mu.Lock() | |
acquired1++ | |
time.Sleep(100 * time.Microsecond) | |
mu.Unlock() | |
} | |
} | |
}() | |
// goroutine2 | |
for i := 0; i < 1000; i++ { | |
time.Sleep(100 * time.Microsecond) | |
mu.Lock() | |
acquired2++ | |
mu.Unlock() | |
} | |
done <- true | |
fmt.Printf("mutex1 acquired times %d\n", acquired1) | |
fmt.Printf("mutex2 acquired times %d\n", acquired2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment