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.

logger_bench_test.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package logrus
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. )
  7. // smallFields is a small size data set for benchmarking
  8. var loggerFields = Fields{
  9. "foo": "bar",
  10. "baz": "qux",
  11. "one": "two",
  12. "three": "four",
  13. }
  14. func BenchmarkDummyLogger(b *testing.B) {
  15. nullf, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666)
  16. if err != nil {
  17. b.Fatalf("%v", err)
  18. }
  19. defer nullf.Close()
  20. doLoggerBenchmark(b, nullf, &TextFormatter{DisableColors: true}, smallFields)
  21. }
  22. func BenchmarkDummyLoggerNoLock(b *testing.B) {
  23. nullf, err := os.OpenFile("/dev/null", os.O_WRONLY|os.O_APPEND, 0666)
  24. if err != nil {
  25. b.Fatalf("%v", err)
  26. }
  27. defer nullf.Close()
  28. doLoggerBenchmarkNoLock(b, nullf, &TextFormatter{DisableColors: true}, smallFields)
  29. }
  30. func doLoggerBenchmark(b *testing.B, out *os.File, formatter Formatter, fields Fields) {
  31. logger := Logger{
  32. Out: out,
  33. Level: InfoLevel,
  34. Formatter: formatter,
  35. }
  36. entry := logger.WithFields(fields)
  37. b.RunParallel(func(pb *testing.PB) {
  38. for pb.Next() {
  39. entry.Info("aaa")
  40. }
  41. })
  42. }
  43. func doLoggerBenchmarkNoLock(b *testing.B, out *os.File, formatter Formatter, fields Fields) {
  44. logger := Logger{
  45. Out: out,
  46. Level: InfoLevel,
  47. Formatter: formatter,
  48. }
  49. logger.SetNoLock()
  50. entry := logger.WithFields(fields)
  51. b.RunParallel(func(pb *testing.PB) {
  52. for pb.Next() {
  53. entry.Info("aaa")
  54. }
  55. })
  56. }
  57. func BenchmarkLoggerJSONFormatter(b *testing.B) {
  58. doLoggerBenchmarkWithFormatter(b, &JSONFormatter{})
  59. }
  60. func BenchmarkLoggerTextFormatter(b *testing.B) {
  61. doLoggerBenchmarkWithFormatter(b, &TextFormatter{})
  62. }
  63. func doLoggerBenchmarkWithFormatter(b *testing.B, f Formatter) {
  64. b.SetParallelism(100)
  65. log := New()
  66. log.Formatter = f
  67. log.Out = ioutil.Discard
  68. b.RunParallel(func(pb *testing.PB) {
  69. for pb.Next() {
  70. log.
  71. WithField("foo1", "bar1").
  72. WithField("foo2", "bar2").
  73. Info("this is a dummy log")
  74. }
  75. })
  76. }