1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| package main
import ( "encoding/json" "fmt" "log" "math/rand" "time"
"github.com/golang/glog" "github.com/sirupsen/logrus" "go.uber.org/zap" )
type dummy struct { Foo string `json:"foo"` Bar string `json:"bar"` }
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const ( letterIdxBits = 6 letterIdxMask = 1<<letterIdxBits - 1 letterIdxMax = 63 / letterIdxBits )
func RandString(n int) string { b := make([]byte, n) for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; { if remain == 0 { cache, remain = rand.Int63(), letterIdxMax } if idx := int(cache & letterIdxMask); idx < len(letterBytes) { b[i] = letterBytes[idx] i-- } cache >>= letterIdxBits remain-- } return string(b) }
func dummyData() interface{} { return dummy{ Foo: RandString(12), Bar: RandString(16), } }
func main() {
var x int64 = 0 t := time.Now() for i := 0; i < 10000; i++ { logrus.WithField("Dummy", dummyData()).Infoln("this is a dummy log") } x += time.Since(t).Nanoseconds()
zlogger, _ := zap.NewProduction() sugar := zlogger.Sugar() var y int64 = 0 t = time.Now() for i := 0; i < 10000; i++ { sugar.Infow("this is a dummy log", "Dummy", dummyData()) } y += time.Since(t).Nanoseconds()
var z int64 = 0 t = time.Now() for i := 0; i < 10000; i++ { dummyStr, _ := json.Marshal(dummyData()) log.Printf("this is a dummy log: %s\n", string(dummyStr)) } z += time.Since(t).Nanoseconds()
var w int64 = 0 t = time.Now() for i := 0; i < 10000; i++ { glog.Info("\nthis is a dummy log: ", dummyData()) } w += time.Since(t).Nanoseconds()
fmt.Println("=====================") fmt.Printf("Logrus: %5d ns per request \n", x/10000) fmt.Printf("Zap: %5d ns per request \n", y/10000) fmt.Printf("StdLog: %5d ns per request \n", z/10000) fmt.Printf("Glog: %5d ns per request \n", w/10000) }
|