github.com/bibaroc/wingman@v0.0.2-0.20200911182922-33c2085136b1/pkg/logger/logger_test.go (about)

     1  package logger_test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"testing"
     8  
     9  	"github.com/bibaroc/wingman/pkg/logger"
    10  	pglog "github.com/go-playground/log/v7"
    11  	"github.com/go-playground/log/v7/handlers/console"
    12  )
    13  
    14  const (
    15  	short = "this will be fast"
    16  )
    17  
    18  func BenchmarkLoggerCustom(b *testing.B) {
    19  	log := logger.NewLogger(logger.ERROR, ioutil.Discard, 0)
    20  	b.SetBytes(int64(len(short)))
    21  
    22  	b.ResetTimer()
    23  	for i := 0; i < b.N; i++ {
    24  		log.Errorln(short)
    25  	}
    26  }
    27  func BenchmarkLoggerStd(b *testing.B) {
    28  	log := log.New(ioutil.Discard, "std", 0)
    29  	b.SetBytes(int64(len(short)))
    30  
    31  	b.ResetTimer()
    32  	for i := 0; i < b.N; i++ {
    33  		log.Println(short)
    34  	}
    35  }
    36  
    37  func BenchmarkLoggerPlayground(b *testing.B) {
    38  	csole := console.New(false)
    39  	csole.SetDisplayColor(false)
    40  	csole.SetWriter(ioutil.Discard)
    41  	pglog.AddHandler(csole, pglog.AllLevels...)
    42  	b.SetBytes(int64(len(short)))
    43  
    44  	b.ResetTimer()
    45  	for i := 0; i < b.N; i++ {
    46  		pglog.Debug(short)
    47  	}
    48  }
    49  func BenchmarkLoggerCustomWCaller(b *testing.B) {
    50  	log := logger.NewLogger(logger.ERROR, ioutil.Discard, logger.WithCallerInfo)
    51  	b.ResetTimer()
    52  	b.SetBytes(int64(len(short)))
    53  
    54  	for i := 0; i < b.N; i++ {
    55  		log.Errorln(short)
    56  	}
    57  }
    58  func BenchmarkLoggerStdWCaller(b *testing.B) {
    59  	log := log.New(ioutil.Discard, "std", log.Lshortfile)
    60  	b.SetBytes(int64(len(short)))
    61  
    62  	b.ResetTimer()
    63  	for i := 0; i < b.N; i++ {
    64  		log.Println(short)
    65  	}
    66  }
    67  func BenchmarkLoggerFmt(b *testing.B) {
    68  	b.ResetTimer()
    69  	b.SetBytes(int64(len(short)))
    70  
    71  	for i := 0; i < b.N; i++ {
    72  		fmt.Fprintln(ioutil.Discard, short)
    73  	}
    74  }