Skip to content

Commit

Permalink
Merge pull request #33 from Clivern/feature/latency
Browse files Browse the repository at this point in the history
Feature/latency
  • Loading branch information
Clivern authored May 26, 2019
2 parents 980956f + c8612b2 commit 7ad5f5d
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<p align="center">
<a href="https://godoc.org/github.com/clivern/hippo"><img src="https://godoc.org/github.com/clivern/hippo?status.svg"></a>
<a href="https://travis-ci.org/Clivern/Hippo"><img src="https://travis-ci.org/Clivern/Hippo.svg?branch=master"></a>
<a href="https://github.com/Clivern/Hippo/releases"><img src="https://img.shields.io/badge/Version-1.4.0-red.svg"></a>
<a href="https://github.com/Clivern/Hippo/releases"><img src="https://img.shields.io/badge/Version-1.5.0-red.svg"></a>
<a href="https://goreportcard.com/report/github.com/Clivern/Hippo"><img src="https://goreportcard.com/badge/github.com/Clivern/Hippo?v=1.0.0"></a>
<a href="https://github.com/Clivern/Hippo/blob/master/LICENSE"><img src="https://img.shields.io/badge/LICENSE-MIT-orange.svg"></a>
</p>
Expand Down Expand Up @@ -281,6 +281,38 @@ exists := hippo.FileExists("/var/log/error.log")
exists := hippo.DirExists("/var/log")
```

**Latency Tracker Component**

```golang
httpClient := hippo.NewHTTPClient()

latency := NewLatencyTracker()
latency.NewAction("api.call")

// First HTTP Call
start := time.Now()
httpClient.Get(
"https://httpbin.org/get",
map[string]string{},
map[string]string{},
)
latency.SetPoint("api.call", start, time.Now())

// Another HTTP Call
latency.SetStart("api.call", time.Now())
httpClient.Get(
"https://httpbin.org/get",
map[string]string{},
map[string]string{},
)
latency.SetEnd("api.call", time.Now())

// Now it will calculate the average
fmt.Println(latency.GetLatency("api.call"))
// Output 486.217112ms <nil>
```


## Versioning

For transparency into our release cycle and in striving to maintain backward compatibility, Hippo is maintained under the [Semantic Versioning guidelines](https://semver.org/) and release process is predictable and business-friendly.
Expand Down
94 changes: 94 additions & 0 deletions latency.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2019 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.

package hippo

import (
"fmt"
"time"
)

// Point struct
type Point struct {
Start time.Time
End time.Time
}

// Latency struct
type Latency struct {
Actions map[string][]Point
}

// NewLatencyTracker creates a new latency instance
func NewLatencyTracker() *Latency {
return &Latency{}
}

// NewAction creates a new action tracking bucket
func (l *Latency) NewAction(name string) {
l.Actions = make(map[string][]Point)
l.Actions[name] = []Point{}
}

// SetPoint adds a new point
func (l *Latency) SetPoint(name string, start, end time.Time) {
if _, ok := l.Actions[name]; !ok {
l.NewAction(name)
}
l.Actions[name] = append(l.Actions[name], Point{Start: start, End: end})
}

// SetStart adds point start time
func (l *Latency) SetStart(name string, start time.Time) bool {
if _, ok := l.Actions[name]; !ok {
l.NewAction(name)
}
l.Actions[name] = append(l.Actions[name], Point{Start: start})

return true
}

// SetEnd adds point end time
func (l *Latency) SetEnd(name string, end time.Time) bool {
if _, ok := l.Actions[name]; !ok {
l.NewAction(name)
}

length := len(l.Actions[name])

if length <= 0 {
return false
}

if l.Actions[name][length-1].End.String() == "" {
return false
}

l.Actions[name][length-1].End = end

return true
}

// GetLatency returns average latency in nanoseconds for specific action
func (l *Latency) GetLatency(name string) (time.Duration, error) {
var total time.Duration

for _, v := range l.Actions[name] {
total += v.GetLatency()
}

result := total.Nanoseconds() / int64(len(l.Actions[name]))
timeDuration, err := time.ParseDuration(fmt.Sprintf("%dns", result))

if err != nil {
return time.Duration(0), err
}

return timeDuration, nil
}

// GetLatency returns latency in nanoseconds
func (p *Point) GetLatency() time.Duration {
return p.End.Sub(p.Start)
}

0 comments on commit 7ad5f5d

Please sign in to comment.