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.

formatter_bench_test.go 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. }