Guest User

Untitled

a guest
Jan 22nd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. // file: gotop.go
  2. package main
  3.  
  4. import (
  5. "log"
  6. "sort"
  7. // "time"
  8.  
  9. "github.com/pkg/profile"
  10. "github.com/shirou/gopsutil/process"
  11. )
  12.  
  13. type ProcInfo struct {
  14. Name string
  15. Usage float64
  16. }
  17.  
  18. type ByUsage []ProcInfo
  19.  
  20. func (a ByUsage) Len() int { return len(a) }
  21. func (a ByUsage) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  22. func (a ByUsage) Less(i, j int) bool {
  23. return a[i].Usage > a[j].Usage
  24. }
  25.  
  26. func main() {
  27.  
  28. defer profile.Start(profile.CPUProfile).Stop()
  29.  
  30. for i := 0; i < 2; i++ {
  31.  
  32. processes, _ := process.Processes()
  33.  
  34. var procinfos []ProcInfo
  35. for _, p := range processes {
  36. a, _ := p.CPUPercent()
  37. n, _ := p.Name()
  38. procinfos = append(procinfos, ProcInfo{n, a})
  39. }
  40. sort.Sort(ByUsage(procinfos))
  41.  
  42. for _, p := range procinfos[:5] {
  43. log.Printf(" %s -> %f", p.Name, p.Usage)
  44. }
  45. // time.Sleep(3 * time.Second)
  46. }
  47. }
Add Comment
Please, Sign In to add comment