Browse Source

Add method to calculate idle time

Oz Tiram 5 years ago
parent
commit
1ce156d71b
2 changed files with 15 additions and 0 deletions
  1. 4 0
      gupta.go
  2. 11 0
      gupta_test.go

+ 4 - 0
gupta.go

@@ -81,6 +81,10 @@ func (c *CPUStat) TotalTime() uint64 {
 	return c.user + c.nice + c.system + c.idle + c.iowait + c.irq + c.softirq + c.steal
 }
 
+func (c *CPUStat) IdleTime() uint64 {
+	return c.idle + c.iowait
+}
+
 func NewCPUStat(procstatline string) CPUStat {
 	cpustat := CPUStat{}
 	statline := strings.Fields(procstatline)

+ 11 - 0
gupta_test.go

@@ -89,3 +89,14 @@ func TestTotalCPUTime(t *testing.T) {
 		t.Errorf("got total time %v want %v", got, want)
 	}
 }
+
+func TestIdleTime(t *testing.T) {
+	procstatLine := "cpu 4705 356  584    3699   23    23     0       0     0    0"
+	cpustat := NewCPUStat(procstatLine)
+
+	got := cpustat.IdleTime()
+	want := uint64(3722)
+	if got != want {
+		t.Errorf("got idle time %v want %v", got, want)
+	}
+}