lock_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package lock_test
  2. import (
  3. "sync"
  4. "testing"
  5. )
  6. func BenchmarkSyncMutex(t *testing.B) {
  7. // 100000000 loop
  8. // goos: darwin
  9. // goarch: amd64
  10. // pkg: git.wecise.com/wecise/mring/z/lock
  11. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  12. // BenchmarkSyncMutex
  13. // BenchmarkSyncMutex-8 1 33905064348 ns/op 2407287568 B/op 100034140 allocs/op
  14. // PASS
  15. // ok git.wecise.com/wecise/mring/z/lock 34.056s
  16. //
  17. // 10000000 loop
  18. // goos: darwin
  19. // goarch: amd64
  20. // pkg: git.wecise.com/wecise/mring/z/lock
  21. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  22. // BenchmarkSyncMutex
  23. // BenchmarkSyncMutex-8 1 3215950432 ns/op 243519072 B/op 10016404 allocs/op
  24. // PASS
  25. // ok git.wecise.com/wecise/mring/z/lock 3.980s
  26. //
  27. // 1000000 loop
  28. // goos: darwin
  29. // goarch: amd64
  30. // pkg: git.wecise.com/wecise/mring/z/lock
  31. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  32. // BenchmarkSyncMutex
  33. // BenchmarkSyncMutex-8 1000000000 0.3031 ns/op 0 B/op 0 allocs/op
  34. // PASS
  35. // ok git.wecise.com/wecise/mring/z/lock 5.979s
  36. var mutex sync.Mutex
  37. t.StartTimer()
  38. for i := 0; i < 10000000; {
  39. go func() {
  40. mutex.Lock()
  41. i++
  42. mutex.Unlock()
  43. }()
  44. }
  45. t.StopTimer()
  46. }
  47. func BenchmarkChan(t *testing.B) {
  48. // 100000000 loop Crash
  49. //
  50. // 10000000 loop
  51. // goos: darwin
  52. // goarch: amd64
  53. // pkg: git.wecise.com/wecise/mring/z/lock
  54. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  55. // BenchmarkChan
  56. // BenchmarkChan-8 1 49887892128 ns/op 5246415680 B/op 35689879 allocs/op
  57. // PASS
  58. // ok git.wecise.com/wecise/mring/z/lock 52.270s
  59. //
  60. // 1000000 loop
  61. // goos: darwin
  62. // goarch: amd64
  63. // pkg: git.wecise.com/wecise/mring/z/lock
  64. // cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  65. // BenchmarkChan
  66. // BenchmarkChan-8 1 1749597455 ns/op 313448576 B/op 2504514 allocs/op
  67. // PASS
  68. // ok git.wecise.com/wecise/mring/z/lock 2.472s
  69. ch := make(chan struct{}, 1)
  70. t.StartTimer()
  71. for i := 0; i < 10000000; {
  72. go func() {
  73. ch <- struct{}{}
  74. i++
  75. <-ch
  76. }()
  77. }
  78. t.StopTimer()
  79. }