...

Source file src/github.com/sirupsen/logrus/formatter_bench_test.go

Documentation: github.com/sirupsen/logrus

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

View as plain text