github.com/phsym/zeroslog@v0.1.1-0.20240224183259-0b7a5ea94339/internal_bench_test.go (about)

     1  //go:build ignorepc
     2  
     3  package zeroslog
     4  
     5  import (
     6  	"context"
     7  	"io"
     8  	"log/slog"
     9  	"runtime"
    10  	"testing"
    11  	"time"
    12  
    13  	_ "unsafe"
    14  )
    15  
    16  //go:linkname IgnorePC log/slog/internal.IgnorePC
    17  var IgnorePC bool
    18  
    19  func BenchmarkDummy(b *testing.B) {
    20  	ctx := context.Background()
    21  	l := slog.New(&DummyHandler{})
    22  	l = l.With("foo", "bar")
    23  	b.ResetTimer()
    24  	f := func(b *testing.B) {
    25  		for i := 0; i < b.N; i++ {
    26  			l.LogAttrs(ctx, slog.LevelInfo, "hello", slog.String("bar", "baz"))
    27  		}
    28  	}
    29  	b.Run("with-pc", f)
    30  	b.Run("no-pc", func(b *testing.B) {
    31  		IgnorePC = true
    32  		defer func() {
    33  			IgnorePC = false
    34  		}()
    35  		f(b)
    36  	})
    37  }
    38  
    39  func BenchmarkSlogZerolog(b *testing.B) {
    40  	ctx := context.Background()
    41  	l := slog.New(NewJsonHandler(io.Discard, &HandlerOptions{Level: slog.LevelDebug}))
    42  	l = l.With("foo", "bar")
    43  	b.ResetTimer()
    44  	f := func(b *testing.B) {
    45  		for i := 0; i < b.N; i++ {
    46  			l.LogAttrs(ctx, slog.LevelInfo, "hello", slog.String("bar", "baz"))
    47  		}
    48  	}
    49  
    50  	b.Run("with-pc", f)
    51  	b.Run("no-pc", func(b *testing.B) {
    52  		IgnorePC = true
    53  		defer func() {
    54  			IgnorePC = false
    55  		}()
    56  		f(b)
    57  	})
    58  }
    59  
    60  func BenchmarkSlogZerolog_HandlerWithRec(b *testing.B) {
    61  	ctx := context.Background()
    62  	h := slog.Handler(NewJsonHandler(io.Discard, &HandlerOptions{Level: slog.LevelDebug}))
    63  	h = h.WithAttrs([]slog.Attr{slog.String("foo", "bar")})
    64  	b.ResetTimer()
    65  	for i := 0; i < b.N; i++ {
    66  		rec := slog.NewRecord(time.Now(), slog.LevelInfo, "hello", 0)
    67  		rec.AddAttrs(slog.String("bar", "baz"))
    68  		h.Handle(ctx, rec)
    69  	}
    70  }
    71  
    72  func BenchmarkSlogZerolog_HandlerWithRecAndCaller(b *testing.B) {
    73  	ctx := context.Background()
    74  	h := slog.Handler(NewJsonHandler(io.Discard, &HandlerOptions{Level: slog.LevelDebug}))
    75  	h = h.WithAttrs([]slog.Attr{slog.String("foo", "bar")})
    76  	b.ResetTimer()
    77  	for i := 0; i < b.N; i++ {
    78  		pcx := [1]uintptr{0}
    79  		runtime.Callers(3, pcx[:])
    80  		rec := slog.NewRecord(time.Now(), slog.LevelInfo, "hello", pcx[0])
    81  		rec.AddAttrs(slog.String("bar", "baz"))
    82  		h.Handle(ctx, rec)
    83  	}
    84  }
    85  
    86  var (
    87  	rec  slog.Record
    88  	attr slog.Attr
    89  	t    time.Time
    90  )
    91  
    92  func BenchmarkSlog(b *testing.B) {
    93  	b.Run("new-rec", func(b *testing.B) {
    94  		now := time.Now()
    95  		b.ResetTimer()
    96  		for i := 0; i < b.N; i++ {
    97  			rec = slog.NewRecord(now, slog.LevelInfo, "yo", 0)
    98  		}
    99  	})
   100  	b.Run("new-rec-time", func(b *testing.B) {
   101  		for i := 0; i < b.N; i++ {
   102  			rec = slog.NewRecord(time.Now(), slog.LevelInfo, "yo", 0)
   103  		}
   104  	})
   105  	b.Run("time.now", func(b *testing.B) {
   106  		for i := 0; i < b.N; i++ {
   107  			now = time.Now()
   108  		}
   109  	})
   110  	b.Run("new-attr", func(b *testing.B) {
   111  		for i := 0; i < b.N; i++ {
   112  			attr = slog.String("foo", "bar")
   113  		}
   114  	})
   115  	b.Run("new-add-attr", func(b *testing.B) {
   116  		for i := 0; i < b.N; i++ {
   117  			rec := slog.NewRecord(time.Now(), slog.LevelInfo, "yo", 0)
   118  			attr := slog.String("foo", "bar")
   119  			rec.AddAttrs(attr)
   120  		}
   121  	})
   122  	b.Run("runtime-caller.1", func(b *testing.B) {
   123  		pc := [1]uintptr{}
   124  		for i := 0; i < b.N; i++ {
   125  			runtime.Callers(1, pc[:])
   126  		}
   127  	})
   128  	b.Run("runtime-caller.3", func(b *testing.B) {
   129  		pc := [1]uintptr{}
   130  		for i := 0; i < b.N; i++ {
   131  			runtime.Callers(3, pc[:])
   132  		}
   133  	})
   134  }