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.
 
 
 

162 lines
3.2 KiB

  1. // Copyright 2012 The Gorilla Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import (
  6. "net/http"
  7. "testing"
  8. )
  9. type keyType int
  10. const (
  11. key1 keyType = iota
  12. key2
  13. )
  14. func TestContext(t *testing.T) {
  15. assertEqual := func(val interface{}, exp interface{}) {
  16. if val != exp {
  17. t.Errorf("Expected %v, got %v.", exp, val)
  18. }
  19. }
  20. r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
  21. emptyR, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
  22. // Get()
  23. assertEqual(Get(r, key1), nil)
  24. // Set()
  25. Set(r, key1, "1")
  26. assertEqual(Get(r, key1), "1")
  27. assertEqual(len(data[r]), 1)
  28. Set(r, key2, "2")
  29. assertEqual(Get(r, key2), "2")
  30. assertEqual(len(data[r]), 2)
  31. //GetOk
  32. value, ok := GetOk(r, key1)
  33. assertEqual(value, "1")
  34. assertEqual(ok, true)
  35. value, ok = GetOk(r, "not exists")
  36. assertEqual(value, nil)
  37. assertEqual(ok, false)
  38. Set(r, "nil value", nil)
  39. value, ok = GetOk(r, "nil value")
  40. assertEqual(value, nil)
  41. assertEqual(ok, true)
  42. // GetAll()
  43. values := GetAll(r)
  44. assertEqual(len(values), 3)
  45. // GetAll() for empty request
  46. values = GetAll(emptyR)
  47. if values != nil {
  48. t.Error("GetAll didn't return nil value for invalid request")
  49. }
  50. // GetAllOk()
  51. values, ok = GetAllOk(r)
  52. assertEqual(len(values), 3)
  53. assertEqual(ok, true)
  54. // GetAllOk() for empty request
  55. values, ok = GetAllOk(emptyR)
  56. assertEqual(value, nil)
  57. assertEqual(ok, false)
  58. // Delete()
  59. Delete(r, key1)
  60. assertEqual(Get(r, key1), nil)
  61. assertEqual(len(data[r]), 2)
  62. Delete(r, key2)
  63. assertEqual(Get(r, key2), nil)
  64. assertEqual(len(data[r]), 1)
  65. // Clear()
  66. Clear(r)
  67. assertEqual(len(data), 0)
  68. }
  69. func parallelReader(r *http.Request, key string, iterations int, wait, done chan struct{}) {
  70. <-wait
  71. for i := 0; i < iterations; i++ {
  72. Get(r, key)
  73. }
  74. done <- struct{}{}
  75. }
  76. func parallelWriter(r *http.Request, key, value string, iterations int, wait, done chan struct{}) {
  77. <-wait
  78. for i := 0; i < iterations; i++ {
  79. Set(r, key, value)
  80. }
  81. done <- struct{}{}
  82. }
  83. func benchmarkMutex(b *testing.B, numReaders, numWriters, iterations int) {
  84. b.StopTimer()
  85. r, _ := http.NewRequest("GET", "http://localhost:8080/", nil)
  86. done := make(chan struct{})
  87. b.StartTimer()
  88. for i := 0; i < b.N; i++ {
  89. wait := make(chan struct{})
  90. for i := 0; i < numReaders; i++ {
  91. go parallelReader(r, "test", iterations, wait, done)
  92. }
  93. for i := 0; i < numWriters; i++ {
  94. go parallelWriter(r, "test", "123", iterations, wait, done)
  95. }
  96. close(wait)
  97. for i := 0; i < numReaders+numWriters; i++ {
  98. <-done
  99. }
  100. }
  101. }
  102. func BenchmarkMutexSameReadWrite1(b *testing.B) {
  103. benchmarkMutex(b, 1, 1, 32)
  104. }
  105. func BenchmarkMutexSameReadWrite2(b *testing.B) {
  106. benchmarkMutex(b, 2, 2, 32)
  107. }
  108. func BenchmarkMutexSameReadWrite4(b *testing.B) {
  109. benchmarkMutex(b, 4, 4, 32)
  110. }
  111. func BenchmarkMutex1(b *testing.B) {
  112. benchmarkMutex(b, 2, 8, 32)
  113. }
  114. func BenchmarkMutex2(b *testing.B) {
  115. benchmarkMutex(b, 16, 4, 64)
  116. }
  117. func BenchmarkMutex3(b *testing.B) {
  118. benchmarkMutex(b, 1, 2, 128)
  119. }
  120. func BenchmarkMutex4(b *testing.B) {
  121. benchmarkMutex(b, 128, 32, 256)
  122. }
  123. func BenchmarkMutex5(b *testing.B) {
  124. benchmarkMutex(b, 1024, 2048, 64)
  125. }
  126. func BenchmarkMutex6(b *testing.B) {
  127. benchmarkMutex(b, 2048, 1024, 512)
  128. }