mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
feat: add TrackFuncTime in datetime package
This commit is contained in:
@@ -30,6 +30,7 @@ package datetime
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -382,11 +383,31 @@ func TimestampNano(timezone ...string) int64 {
|
|||||||
return t.UnixNano()
|
return t.UnixNano()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TraceFuncTime: trace the func costed time,just call it at top of the func like `defer TraceFuncTime()()`
|
// TrackFuncTime track the time of function execution.
|
||||||
func TraceFuncTime() func() {
|
// call it at top of the func like `defer TrackFuncTime(time.Now())()`
|
||||||
pre := time.Now()
|
// Play: todo
|
||||||
|
func TrackFuncTime(pre time.Time) func() {
|
||||||
|
callerName := getCallerName()
|
||||||
return func() {
|
return func() {
|
||||||
elapsed := time.Since(pre)
|
elapsed := time.Since(pre)
|
||||||
fmt.Println("Costs Time:\t", elapsed)
|
fmt.Printf("Function %s execution time:\t %v", callerName, elapsed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getCallerName() string {
|
||||||
|
pc, _, _, ok := runtime.Caller(2)
|
||||||
|
if !ok {
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
fn := runtime.FuncForPC(pc)
|
||||||
|
if fn == nil {
|
||||||
|
return "Unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
fullName := fn.Name()
|
||||||
|
if lastDot := strings.LastIndex(fullName, "."); lastDot != -1 {
|
||||||
|
return fullName[lastDot+1:]
|
||||||
|
}
|
||||||
|
|
||||||
|
return fullName
|
||||||
|
}
|
||||||
|
|||||||
@@ -408,3 +408,17 @@ func ExampleIsWeekend() {
|
|||||||
// true
|
// true
|
||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func ExampleTrackFuncTime() {
|
||||||
|
// defer TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
// var n int
|
||||||
|
// for i := 0; i < 5000000; i++ {
|
||||||
|
// n++
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fmt.Println(1)
|
||||||
|
|
||||||
|
// // Output:
|
||||||
|
// // 1
|
||||||
|
// }
|
||||||
|
|||||||
@@ -410,3 +410,12 @@ func TestTimestamp(t *testing.T) {
|
|||||||
ts4 := TimestampNano()
|
ts4 := TimestampNano()
|
||||||
t.Log(ts4)
|
t.Log(ts4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrackFuncTime(t *testing.T) {
|
||||||
|
defer TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
var n int
|
||||||
|
for i := 0; i < 5000000; i++ {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -64,6 +64,7 @@ import (
|
|||||||
- [TimestampMilli](#TimestampMilli)
|
- [TimestampMilli](#TimestampMilli)
|
||||||
- [TimestampMicro](#TimestampMicro)
|
- [TimestampMicro](#TimestampMicro)
|
||||||
- [TimestampNano](#TimestampNano)
|
- [TimestampNano](#TimestampNano)
|
||||||
|
- [TrackFuncTime](#TrackFuncTime)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1334,7 +1335,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss")
|
result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss")
|
||||||
|
|
||||||
result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")
|
result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST")
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
@@ -1464,4 +1465,36 @@ func main() {
|
|||||||
// Output:
|
// Output:
|
||||||
// 1690363051331788000
|
// 1690363051331788000
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="TrackFuncTime">TrackFuncTime</span>
|
||||||
|
|
||||||
|
<p>测试函数执行时间。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TrackFuncTime(pre time.Time) func()
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer datetime.TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
var n int
|
||||||
|
for i := 0; i < 5000000; i++ {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(1) // Function main execution time: 1.460287ms
|
||||||
|
}
|
||||||
```
|
```
|
||||||
@@ -65,6 +65,7 @@ import (
|
|||||||
- [TimestampMilli](#TimestampMilli)
|
- [TimestampMilli](#TimestampMilli)
|
||||||
- [TimestampMicro](#TimestampMicro)
|
- [TimestampMicro](#TimestampMicro)
|
||||||
- [TimestampNano](#TimestampNano)
|
- [TimestampNano](#TimestampNano)
|
||||||
|
- [TrackFuncTime](#TrackFuncTime)
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -1465,3 +1466,35 @@ func main() {
|
|||||||
// 1690363051331788000
|
// 1690363051331788000
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="TrackFuncTime">TrackFuncTime</span>
|
||||||
|
|
||||||
|
<p>Tracks function execution time.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func TrackFuncTime(pre time.Time) func()
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run]()</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/datetime"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
defer datetime.TrackFuncTime(time.Now())()
|
||||||
|
|
||||||
|
var n int
|
||||||
|
for i := 0; i < 5000000; i++ {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(1) // Function main execution time: 1.460287ms
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user