Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file: gotop.go
- package main
- import (
- "log"
- "sort"
- // "time"
- "github.com/pkg/profile"
- "github.com/shirou/gopsutil/process"
- )
- type ProcInfo struct {
- Name string
- Usage float64
- }
- type ByUsage []ProcInfo
- func (a ByUsage) Len() int { return len(a) }
- func (a ByUsage) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
- func (a ByUsage) Less(i, j int) bool {
- return a[i].Usage > a[j].Usage
- }
- func main() {
- defer profile.Start(profile.CPUProfile).Stop()
- for i := 0; i < 2; i++ {
- processes, _ := process.Processes()
- var procinfos []ProcInfo
- for _, p := range processes {
- a, _ := p.CPUPercent()
- n, _ := p.Name()
- procinfos = append(procinfos, ProcInfo{n, a})
- }
- sort.Sort(ByUsage(procinfos))
- for _, p := range procinfos[:5] {
- log.Printf(" %s -> %f", p.Name, p.Usage)
- }
- // time.Sleep(3 * time.Second)
- }
- }
Add Comment
Please, Sign In to add comment