...

Source file src/github.com/rs/zerolog/diode/diode_test.go

Documentation: github.com/rs/zerolog/diode

     1  package diode_test
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/rs/zerolog"
    13  	"github.com/rs/zerolog/diode"
    14  	"github.com/rs/zerolog/internal/cbor"
    15  )
    16  
    17  func TestNewWriter(t *testing.T) {
    18  	buf := bytes.Buffer{}
    19  	w := diode.NewWriter(&buf, 1000, 0, func(missed int) {
    20  		fmt.Printf("Dropped %d messages\n", missed)
    21  	})
    22  	log := zerolog.New(w)
    23  	log.Print("test")
    24  
    25  	w.Close()
    26  	want := "{\"level\":\"debug\",\"message\":\"test\"}\n"
    27  	got := cbor.DecodeIfBinaryToString(buf.Bytes())
    28  	if got != want {
    29  		t.Errorf("Diode New Writer Test failed. got:%s, want:%s!", got, want)
    30  	}
    31  }
    32  
    33  func Benchmark(b *testing.B) {
    34  	log.SetOutput(ioutil.Discard)
    35  	defer log.SetOutput(os.Stderr)
    36  	benchs := map[string]time.Duration{
    37  		"Waiter": 0,
    38  		"Pooler": 10 * time.Millisecond,
    39  	}
    40  	for name, interval := range benchs {
    41  		b.Run(name, func(b *testing.B) {
    42  			w := diode.NewWriter(ioutil.Discard, 100000, interval, nil)
    43  			log := zerolog.New(w)
    44  			defer w.Close()
    45  
    46  			b.SetParallelism(1000)
    47  			b.RunParallel(func(pb *testing.PB) {
    48  				for pb.Next() {
    49  					log.Print("test")
    50  				}
    51  			})
    52  		})
    53  	}
    54  }
    55  

View as plain text