-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ff4d51
commit 77b1bad
Showing
22 changed files
with
600 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package metrics | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type SimpleMetrics struct { | ||
duration int64 // 总耗时 | ||
count int64 | ||
mu sync.Mutex | ||
} | ||
|
||
func (s *SimpleMetrics) Add(duration time.Duration) { | ||
|
||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
s.count++ | ||
s.duration += int64(duration) | ||
|
||
} | ||
func (s *SimpleMetrics) Duration() (duration time.Duration) { | ||
|
||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if s.count == 0 { | ||
return 0 | ||
} | ||
|
||
duration = time.Duration(s.duration / s.count) | ||
|
||
return duration | ||
} | ||
|
||
func (s *SimpleMetrics) Clear() { | ||
|
||
s.mu.Lock() | ||
defer s.mu.Unlock() | ||
|
||
if s.count == 0 { | ||
return | ||
} | ||
|
||
s.count = 0 | ||
s.duration = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.