如果设置为丢弃,则Golang记录器的开销 [英] Overhead for Golang's logger if set to discard

查看:129
本文介绍了如果设置为丢弃,则Golang记录器的开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTTP处理程序,该处理程序有40个记录器,它们设置为 os.Stdout .

I have a HTTP handler that has 40 loggers that is set to os.Stdout.

它对我来说很好,因为我是目前唯一的一项测试. 但是,在生产过程中,恐怕会产生过多的开销.

It works just fine for me as I am the only one testing at the moment. But, when it goes to the production, I'm afraid it will have too much overhead.

当前记录器设置为 os.Stdout os.Stderr . 但是一旦进入生产阶段, os.Stdout 将被设置为 ioutil.discard .

Currently loggers are set to os.Stdout and os.Stderr. But once it gets to the production, os.Stdout will be set to ioutil.discard.

Q1.如果我仍然要保留记录器,对性能会有影响吗?

Q1. Will it impact the performance if I still have the logger in set to discard?

Q2.为了最佳实践,最好将记录程序从HTTP处理程序中完全删除吗?

Q2. For the best practice, is it better to remove loggers completely from the HTTP handler?

----更新

package main

import (
    "time"
    "fmt"
    "log"
    "io/ioutil"
    "io"
)

func testPrintf(w io.Writer, note string, cnt int) {
    l := log.New(w, "", 0)
    t1 := time.Now()
    for i:=0; i<cnt; i++ {
        l.Printf("%s - %d", "test", i)
    }
    t2 := time.Now()
    fmt.Printf("%-15s  %-15s, %v\n","Printf ", note, t2.Sub(t1))
}

func testPrintln(w io.Writer, note string, cnt int) {
    l := log.New(w, "", 0)
    t1 := time.Now()
    for i:=0; i<cnt; i++ {
        l.Println("test" + string(i))
    }
    t2 := time.Now()
    fmt.Printf("%-15s  %-15s, %v\n","Println", note, t2.Sub(t1))
}

func testDoNothing(w io.Writer, note string, cnt int) {
    //l := log.New(w, "", 0)
    t1 := time.Now()
    for i:=0; i<cnt; i++ {
        _ = "test" + string(i) // evaluated but didn't do any.
    }
    t2 := time.Now()
    fmt.Printf("%-15s  %-15s, %v\n", "DoNothing", note, t2.Sub(t1))
}

func main() {
    cnt := 10000000 // ten million
    testPrintf(ioutil.Discard, "discard.Attempt.1", cnt)
    testPrintln(ioutil.Discard, "discard.Attempt.1", cnt)
    testDoNothing(ioutil.Discard, "discard.Attempt.1", cnt)
    fmt.Println("\n")
    testPrintf(ioutil.Discard, "discard.Attempt.2", cnt)
    testPrintln(ioutil.Discard, "discard.Attempt.2", cnt)
    testDoNothing(ioutil.Discard, "discard.Attempt.2", cnt)
    fmt.Println("\n")
    testPrintf(ioutil.Discard, "discard.Attempt.3", cnt)
    testPrintln(ioutil.Discard, "discard.Attempt.3", cnt)
    testDoNothing(ioutil.Discard, "discard.Attempt.3", cnt)
}

---结果---

Printf           discard.Attempt.1, 2.663697209s
Println          discard.Attempt.1, 2.4289759s
DoNothing        discard.Attempt.1, 190.480694ms

Printf           discard.Attempt.2, 2.493506245s
Println          discard.Attempt.2, 2.426081786s
DoNothing        discard.Attempt.2, 182.899574ms

Printf           discard.Attempt.3, 2.480853275s
Println          discard.Attempt.3, 2.481552836s
DoNothing        discard.Attempt.3, 180.916608ms

  1. 我每次跑了1000万次.
  2. 2到3秒以10M记录到io.丢弃的速度比我想象的要快.我想我不必担心速度.
  3. os.Stdout我不是故意要使用的; (我最初关心的是使用ioutil.Discard与删除代码来保存代码),但是由于os.Stdout没有缓冲,所以速度很慢.
  4. 顺便说一句,printf()比我想象的要快得多.与println()几乎相同
  1. I ran for 10M times for each.
  2. 2~3 sec for 10M log to io.Discard is faster than I thought.. I guess I don't have to worry about the speed.
  3. os.Stdout which I didn't mean to use; (my original concern was keeping code with ioutil.Discard vs remove the code), but since os.Stdout was not buffered, it was slow.
  4. By the way, printf() was a lot faster than I thought. Almost same as println()

我不是每天都编写Go代码,因此此测试可能不准确.如果您在测试中看到误导性信息,请在此处留下评论,以使其他人知道.谢谢.

I don't write Go code daily basis, so this test may be inaccurate. If you see misleading info from the test, please leave a comment here for others to aware. Thank you.

推荐答案

基准

虽然我的直觉认为将日志放置到Discard对您的代码的影响很小,但我建议您建立一个基准测试测试记录器的性能影响.

Benchmarks

While my gut says that dropping the logs to Discard will have minimal impact to your code I would recommend setting up a benchmark to test the performance impact of your loggers.

幸运的是,Go通过在单元测试中编写func BenchmarkXxxxx(*testing.B)函数并运行go test -bench使得此操作非常容易.可以在文档中 中找到更多信息.对于您的基准测试,我建议编写两个测试,一个使用os.Stdout,另一个使用ioutil.Discard-确保两个测试的输入相同.

Fortunately Go makes this very easy by writing a func BenchmarkXxxxx(*testing.B) function in your unit tests and running go test -bench. More information can be found in the documentation. For your benchmark I recommend writing two tests, one with the os.Stdout and the other with ioutil.Discard - make sure the inputs to both tests are the same.

来自 ioutil 以显示将要发生的事情在幕后":

Relevant code from ioutil to show just what will happen "under the hood":

// ioutil.go
type devNull int

func (devNull) Write(p []byte) (int, error) {
    return len(p), nil
}

func (devNull) WriteString(s string) (int, error) {
    return len(s), nil
}

记录最佳做法

一种记录方法是常用的基于级别的记录",您可以在其中选择希望记录的消息的严重性(DEBUG/INFO/WARN/ERROR等),然后选择哪个级别在部署应用程序时显示(例如,开发中为DEBUG及更高版本,生产环境中为WARN及更高版本).

Logging Best Practices

One approach to logging is the common "Level Based Logging" where you pick the severity of the message wishing to be logged (DEBUG/INFO/WARN/ERROR etc.) then choose which levels to display when deploying your application (e.g. DEBUG and above in development but WARN and above in production).

您会发现Go在标准库中仅提供了PrintError级别的语句,因此您将需要/a>.

As you will have noticed Go only provides Print and Error level statements in the standard library so you will need an external package to benefit from something like this (these can also help structuring your logs in a more machine readable/search friendly JSON format too). Dave Cheney has justified the reasoning behind this decision in a blog post.

在您的情况下,40条日志语句听起来很像,但这可能取决于有多少简单的Print日志. Print日志记录作为开发中的临时措施可能非常有用,如果您没有将错误暴露给客户端,则Error可以帮助您在故障期间进行诊断.

In your case 40 log statements sounds like a lot but this may depend on how many are simple Print logs; size and complexity of the handler etc. Print logging can be exceptionally helpful as a temporary measure in development and Error to help diagnose during failures if you are not surfacing the error to the client.

这篇关于如果设置为丢弃,则Golang记录器的开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆