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.

61 lines
1.3 KiB

  1. package logrus
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. // smallFields is a small size data set for benchmarking
  7. var loggerFields = Fields{
  8. "foo": "bar",
  9. "baz": "qux",
  10. "one": "two",
  11. "three": "four",
  12. }
  13. func BenchmarkDummyLogger(b *testing.B) {
  14. nullf, err := os.OpenFile("/dev/null", os.O_WRONLY, 0666)
  15. if err != nil {
  16. b.Fatalf("%v", err)
  17. }
  18. defer nullf.Close()
  19. doLoggerBenchmark(b, nullf, &TextFormatter{DisableColors: true}, smallFields)
  20. }
  21. func BenchmarkDummyLoggerNoLock(b *testing.B) {
  22. nullf, err := os.OpenFile("/dev/null", os.O_WRONLY|os.O_APPEND, 0666)
  23. if err != nil {
  24. b.Fatalf("%v", err)
  25. }
  26. defer nullf.Close()
  27. doLoggerBenchmarkNoLock(b, nullf, &TextFormatter{DisableColors: true}, smallFields)
  28. }
  29. func doLoggerBenchmark(b *testing.B, out *os.File, formatter Formatter, fields Fields) {
  30. logger := Logger{
  31. Out: out,
  32. Level: InfoLevel,
  33. Formatter: formatter,
  34. }
  35. entry := logger.WithFields(fields)
  36. b.RunParallel(func(pb *testing.PB) {
  37. for pb.Next() {
  38. entry.Info("aaa")
  39. }
  40. })
  41. }
  42. func doLoggerBenchmarkNoLock(b *testing.B, out *os.File, formatter Formatter, fields Fields) {
  43. logger := Logger{
  44. Out: out,
  45. Level: InfoLevel,
  46. Formatter: formatter,
  47. }
  48. logger.SetNoLock()
  49. entry := logger.WithFields(fields)
  50. b.RunParallel(func(pb *testing.PB) {
  51. for pb.Next() {
  52. entry.Info("aaa")
  53. }
  54. })
  55. }