github.com/phuslu/log@v1.0.100/async_test.go (about)

     1  package log
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  )
     8  
     9  func TestAsyncWriterZero(t *testing.T) {
    10  	w := &AsyncWriter{
    11  		ChannelSize: 0,
    12  		Writer:      IOWriter{os.Stderr},
    13  	}
    14  	for i := 0; i < 10; i++ {
    15  		_, _ = wlprintf(w, InfoLevel, "%s, %d during async writer 1k buff size\n", timeNow(), i)
    16  	}
    17  	if err := w.Close(); err != nil {
    18  		t.Errorf("async close error: %+v", err)
    19  	}
    20  }
    21  
    22  func TestAsyncWriterSmall(t *testing.T) {
    23  	w := &AsyncWriter{
    24  		ChannelSize: 5,
    25  		Writer:      IOWriter{os.Stderr},
    26  	}
    27  	for i := 0; i < 10; i++ {
    28  		_, _ = wlprintf(w, InfoLevel, "%s, %d during async writer 1k buff size\n", timeNow(), i)
    29  	}
    30  	if err := w.Close(); err != nil {
    31  		t.Errorf("async close error: %+v", err)
    32  	}
    33  }
    34  
    35  func BenchmarkAsyncWriter(b *testing.B) {
    36  	logger := Logger{
    37  		Writer: &AsyncWriter{
    38  			ChannelSize: 100,
    39  			Writer:      IOWriter{io.Discard},
    40  		},
    41  	}
    42  	b.SetParallelism(1000)
    43  	b.ReportAllocs()
    44  	b.ResetTimer()
    45  	b.RunParallel(func(b *testing.PB) {
    46  		for b.Next() {
    47  			logger.Info().Msg("hello async writer")
    48  		}
    49  	})
    50  }