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.

101 lines
2.1 KiB

  1. package logrus
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. )
  7. // smallFields is a small size data set for benchmarking
  8. var smallFields = Fields{
  9. "foo": "bar",
  10. "baz": "qux",
  11. "one": "two",
  12. "three": "four",
  13. }
  14. // largeFields is a large size data set for benchmarking
  15. var largeFields = Fields{
  16. "foo": "bar",
  17. "baz": "qux",
  18. "one": "two",
  19. "three": "four",
  20. "five": "six",
  21. "seven": "eight",
  22. "nine": "ten",
  23. "eleven": "twelve",
  24. "thirteen": "fourteen",
  25. "fifteen": "sixteen",
  26. "seventeen": "eighteen",
  27. "nineteen": "twenty",
  28. "a": "b",
  29. "c": "d",
  30. "e": "f",
  31. "g": "h",
  32. "i": "j",
  33. "k": "l",
  34. "m": "n",
  35. "o": "p",
  36. "q": "r",
  37. "s": "t",
  38. "u": "v",
  39. "w": "x",
  40. "y": "z",
  41. "this": "will",
  42. "make": "thirty",
  43. "entries": "yeah",
  44. }
  45. var errorFields = Fields{
  46. "foo": fmt.Errorf("bar"),
  47. "baz": fmt.Errorf("qux"),
  48. }
  49. func BenchmarkErrorTextFormatter(b *testing.B) {
  50. doBenchmark(b, &TextFormatter{DisableColors: true}, errorFields)
  51. }
  52. func BenchmarkSmallTextFormatter(b *testing.B) {
  53. doBenchmark(b, &TextFormatter{DisableColors: true}, smallFields)
  54. }
  55. func BenchmarkLargeTextFormatter(b *testing.B) {
  56. doBenchmark(b, &TextFormatter{DisableColors: true}, largeFields)
  57. }
  58. func BenchmarkSmallColoredTextFormatter(b *testing.B) {
  59. doBenchmark(b, &TextFormatter{ForceColors: true}, smallFields)
  60. }
  61. func BenchmarkLargeColoredTextFormatter(b *testing.B) {
  62. doBenchmark(b, &TextFormatter{ForceColors: true}, largeFields)
  63. }
  64. func BenchmarkSmallJSONFormatter(b *testing.B) {
  65. doBenchmark(b, &JSONFormatter{}, smallFields)
  66. }
  67. func BenchmarkLargeJSONFormatter(b *testing.B) {
  68. doBenchmark(b, &JSONFormatter{}, largeFields)
  69. }
  70. func doBenchmark(b *testing.B, formatter Formatter, fields Fields) {
  71. logger := New()
  72. entry := &Entry{
  73. Time: time.Time{},
  74. Level: InfoLevel,
  75. Message: "message",
  76. Data: fields,
  77. Logger: logger,
  78. }
  79. var d []byte
  80. var err error
  81. for i := 0; i < b.N; i++ {
  82. d, err = formatter.Format(entry)
  83. if err != nil {
  84. b.Fatal(err)
  85. }
  86. b.SetBytes(int64(len(d)))
  87. }
  88. }