You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 lines
2.6 KiB

  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package primitives_test
  19. import (
  20. "context"
  21. "testing"
  22. "time"
  23. )
  24. func BenchmarkCancelContextErrNoErr(b *testing.B) {
  25. ctx, cancel := context.WithCancel(context.Background())
  26. for i := 0; i < b.N; i++ {
  27. if err := ctx.Err(); err != nil {
  28. b.Fatal("error")
  29. }
  30. }
  31. cancel()
  32. }
  33. func BenchmarkCancelContextErrGotErr(b *testing.B) {
  34. ctx, cancel := context.WithCancel(context.Background())
  35. cancel()
  36. for i := 0; i < b.N; i++ {
  37. if err := ctx.Err(); err == nil {
  38. b.Fatal("error")
  39. }
  40. }
  41. }
  42. func BenchmarkCancelContextChannelNoErr(b *testing.B) {
  43. ctx, cancel := context.WithCancel(context.Background())
  44. for i := 0; i < b.N; i++ {
  45. select {
  46. case <-ctx.Done():
  47. b.Fatal("error: ctx.Done():", ctx.Err())
  48. default:
  49. }
  50. }
  51. cancel()
  52. }
  53. func BenchmarkCancelContextChannelGotErr(b *testing.B) {
  54. ctx, cancel := context.WithCancel(context.Background())
  55. cancel()
  56. for i := 0; i < b.N; i++ {
  57. select {
  58. case <-ctx.Done():
  59. if err := ctx.Err(); err == nil {
  60. b.Fatal("error")
  61. }
  62. default:
  63. b.Fatal("error: !ctx.Done()")
  64. }
  65. }
  66. }
  67. func BenchmarkTimerContextErrNoErr(b *testing.B) {
  68. ctx, cancel := context.WithTimeout(context.Background(), 24*time.Hour)
  69. for i := 0; i < b.N; i++ {
  70. if err := ctx.Err(); err != nil {
  71. b.Fatal("error")
  72. }
  73. }
  74. cancel()
  75. }
  76. func BenchmarkTimerContextErrGotErr(b *testing.B) {
  77. ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
  78. cancel()
  79. for i := 0; i < b.N; i++ {
  80. if err := ctx.Err(); err == nil {
  81. b.Fatal("error")
  82. }
  83. }
  84. }
  85. func BenchmarkTimerContextChannelNoErr(b *testing.B) {
  86. ctx, cancel := context.WithTimeout(context.Background(), 24*time.Hour)
  87. for i := 0; i < b.N; i++ {
  88. select {
  89. case <-ctx.Done():
  90. b.Fatal("error: ctx.Done():", ctx.Err())
  91. default:
  92. }
  93. }
  94. cancel()
  95. }
  96. func BenchmarkTimerContextChannelGotErr(b *testing.B) {
  97. ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond)
  98. cancel()
  99. for i := 0; i < b.N; i++ {
  100. select {
  101. case <-ctx.Done():
  102. if err := ctx.Err(); err == nil {
  103. b.Fatal("error")
  104. }
  105. default:
  106. b.Fatal("error: !ctx.Done()")
  107. }
  108. }
  109. }