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.

178 lines
3.9 KiB

  1. // Copyright 2015 The Go 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 trace
  5. import (
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. type s struct{}
  11. func (s) String() string { return "lazy string" }
  12. // TestReset checks whether all the fields are zeroed after reset.
  13. func TestReset(t *testing.T) {
  14. tr := New("foo", "bar")
  15. tr.LazyLog(s{}, false)
  16. tr.LazyPrintf("%d", 1)
  17. tr.SetRecycler(func(_ interface{}) {})
  18. tr.SetTraceInfo(3, 4)
  19. tr.SetMaxEvents(100)
  20. tr.SetError()
  21. tr.Finish()
  22. tr.(*trace).reset()
  23. if !reflect.DeepEqual(tr, new(trace)) {
  24. t.Errorf("reset didn't clear all fields: %+v", tr)
  25. }
  26. }
  27. // TestResetLog checks whether all the fields are zeroed after reset.
  28. func TestResetLog(t *testing.T) {
  29. el := NewEventLog("foo", "bar")
  30. el.Printf("message")
  31. el.Errorf("error")
  32. el.Finish()
  33. el.(*eventLog).reset()
  34. if !reflect.DeepEqual(el, new(eventLog)) {
  35. t.Errorf("reset didn't clear all fields: %+v", el)
  36. }
  37. }
  38. func TestAuthRequest(t *testing.T) {
  39. testCases := []struct {
  40. host string
  41. want bool
  42. }{
  43. {host: "192.168.23.1", want: false},
  44. {host: "192.168.23.1:8080", want: false},
  45. {host: "malformed remote addr", want: false},
  46. {host: "localhost", want: true},
  47. {host: "localhost:8080", want: true},
  48. {host: "127.0.0.1", want: true},
  49. {host: "127.0.0.1:8080", want: true},
  50. {host: "::1", want: true},
  51. {host: "[::1]:8080", want: true},
  52. }
  53. for _, tt := range testCases {
  54. req := &http.Request{RemoteAddr: tt.host}
  55. any, sensitive := AuthRequest(req)
  56. if any != tt.want || sensitive != tt.want {
  57. t.Errorf("AuthRequest(%q) = %t, %t; want %t, %t", tt.host, any, sensitive, tt.want, tt.want)
  58. }
  59. }
  60. }
  61. // TestParseTemplate checks that all templates used by this package are valid
  62. // as they are parsed on first usage
  63. func TestParseTemplate(t *testing.T) {
  64. if tmpl := distTmpl(); tmpl == nil {
  65. t.Error("invalid template returned from distTmpl()")
  66. }
  67. if tmpl := pageTmpl(); tmpl == nil {
  68. t.Error("invalid template returned from pageTmpl()")
  69. }
  70. if tmpl := eventsTmpl(); tmpl == nil {
  71. t.Error("invalid template returned from eventsTmpl()")
  72. }
  73. }
  74. func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
  75. numSpans := (b.N + numEvents + 1) / numEvents
  76. for i := 0; i < numSpans; i++ {
  77. tr := New("test", "test")
  78. tr.SetMaxEvents(maxEvents)
  79. for j := 0; j < numEvents; j++ {
  80. tr.LazyPrintf("%d", j)
  81. }
  82. tr.Finish()
  83. }
  84. }
  85. func BenchmarkTrace_Default_2(b *testing.B) {
  86. benchmarkTrace(b, 0, 2)
  87. }
  88. func BenchmarkTrace_Default_10(b *testing.B) {
  89. benchmarkTrace(b, 0, 10)
  90. }
  91. func BenchmarkTrace_Default_100(b *testing.B) {
  92. benchmarkTrace(b, 0, 100)
  93. }
  94. func BenchmarkTrace_Default_1000(b *testing.B) {
  95. benchmarkTrace(b, 0, 1000)
  96. }
  97. func BenchmarkTrace_Default_10000(b *testing.B) {
  98. benchmarkTrace(b, 0, 10000)
  99. }
  100. func BenchmarkTrace_10_2(b *testing.B) {
  101. benchmarkTrace(b, 10, 2)
  102. }
  103. func BenchmarkTrace_10_10(b *testing.B) {
  104. benchmarkTrace(b, 10, 10)
  105. }
  106. func BenchmarkTrace_10_100(b *testing.B) {
  107. benchmarkTrace(b, 10, 100)
  108. }
  109. func BenchmarkTrace_10_1000(b *testing.B) {
  110. benchmarkTrace(b, 10, 1000)
  111. }
  112. func BenchmarkTrace_10_10000(b *testing.B) {
  113. benchmarkTrace(b, 10, 10000)
  114. }
  115. func BenchmarkTrace_100_2(b *testing.B) {
  116. benchmarkTrace(b, 100, 2)
  117. }
  118. func BenchmarkTrace_100_10(b *testing.B) {
  119. benchmarkTrace(b, 100, 10)
  120. }
  121. func BenchmarkTrace_100_100(b *testing.B) {
  122. benchmarkTrace(b, 100, 100)
  123. }
  124. func BenchmarkTrace_100_1000(b *testing.B) {
  125. benchmarkTrace(b, 100, 1000)
  126. }
  127. func BenchmarkTrace_100_10000(b *testing.B) {
  128. benchmarkTrace(b, 100, 10000)
  129. }
  130. func BenchmarkTrace_1000_2(b *testing.B) {
  131. benchmarkTrace(b, 1000, 2)
  132. }
  133. func BenchmarkTrace_1000_10(b *testing.B) {
  134. benchmarkTrace(b, 1000, 10)
  135. }
  136. func BenchmarkTrace_1000_100(b *testing.B) {
  137. benchmarkTrace(b, 1000, 100)
  138. }
  139. func BenchmarkTrace_1000_1000(b *testing.B) {
  140. benchmarkTrace(b, 1000, 1000)
  141. }
  142. func BenchmarkTrace_1000_10000(b *testing.B) {
  143. benchmarkTrace(b, 1000, 10000)
  144. }