Golang a simple way to record, calculate, and run time of a function _golang

Source: Internet
Author: User

First write a common function, such as under the common package there is a way:

Write Timeout warning Log general method

func timeoutwarning (tag, detailed string, start time.) Time, TimeLimit float64) {
  dis: = time. Now (). Sub (Start). Seconds ()
  if dis > timelimit {
    log. Warning (log. Center_common_warning, Tag, "Detailed:", detailed, "timeoutwarning using", Dis, "s")
    //pubstr: = FMT. Sprintf ("%s count%v, using%f seconds", tag, count, dis)
    //stats. Publish (tag, pubstr)
  }
}


Several parameters of this function are described as follows:
Tag, detailed two string parameters that indicate where the timeout occurred.
Time the Start program started executing
The TimeLimit function executes the timeout threshold in seconds.
When you use it, you have the following code in the first line of each function:



func Save function name (...) (...) {
  //If this method performs a timeout of 3 seconds, log
  defer common is logged. Timeoutwarning ("Saveapplogmain", "Total"), time. Now (), float64 (3))
  //... The logic of the function itself.
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.