forked from new-world-tools/new-world-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
177 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package hash | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
type Hash struct { | ||
FileName string | ||
Hash []byte | ||
} | ||
|
||
type Registry struct { | ||
hashes []*Hash | ||
mu sync.Mutex | ||
} | ||
|
||
func NewRegistry() *Registry { | ||
return &Registry{ | ||
hashes: []*Hash{}, | ||
} | ||
} | ||
|
||
func (registry *Registry) Add(fileName string, hash []byte) { | ||
registry.mu.Lock() | ||
defer registry.mu.Unlock() | ||
|
||
registry.hashes = append(registry.hashes, &Hash{ | ||
FileName: fileName, | ||
Hash: hash, | ||
}) | ||
} | ||
|
||
func (registry *Registry) Hashes() []*Hash { | ||
registry.mu.Lock() | ||
defer registry.mu.Unlock() | ||
|
||
return registry.hashes | ||
} |
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,67 @@ | ||
package profiler | ||
|
||
import ( | ||
"runtime" | ||
"sync" | ||
"time" | ||
) | ||
|
||
func New() *Profiler { | ||
profiler := &Profiler{ | ||
start: time.Now(), | ||
} | ||
|
||
profiler.loadCurrentMemory() | ||
|
||
go profiler.poller() | ||
|
||
return profiler | ||
} | ||
|
||
type Profiler struct { | ||
peakMemory uint64 | ||
currentMemory uint64 | ||
start time.Time | ||
mu sync.Mutex | ||
} | ||
|
||
func (profiler *Profiler) GetCurrentMemory() uint64 { | ||
profiler.mu.Lock() | ||
defer profiler.mu.Unlock() | ||
|
||
return profiler.currentMemory | ||
} | ||
|
||
func (profiler *Profiler) GetPeakMemory() uint64 { | ||
profiler.mu.Lock() | ||
defer profiler.mu.Unlock() | ||
|
||
return profiler.peakMemory | ||
} | ||
|
||
func (profiler *Profiler) GetDuration() time.Duration { | ||
profiler.mu.Lock() | ||
defer profiler.mu.Unlock() | ||
|
||
return time.Now().Sub(profiler.start) | ||
} | ||
|
||
func (profiler *Profiler) loadCurrentMemory() { | ||
profiler.mu.Lock() | ||
defer profiler.mu.Unlock() | ||
|
||
var memory runtime.MemStats | ||
runtime.ReadMemStats(&memory) | ||
|
||
profiler.currentMemory = memory.Sys | ||
if profiler.currentMemory > profiler.peakMemory { | ||
profiler.peakMemory = profiler.currentMemory | ||
} | ||
} | ||
|
||
func (profiler *Profiler) poller() { | ||
ticker := time.NewTicker(1 * time.Second) | ||
for range ticker.C { | ||
profiler.loadCurrentMemory() | ||
} | ||
} |