Browse Source

Clean, Sweep refactor ...

Because we don't need so many methods just to calculate CPU Usage ...
Oz Tiram 5 years ago
parent
commit
ec56a2268e
2 changed files with 23 additions and 120 deletions
  1. 23 74
      gupta.go
  2. 0 46
      gupta_test.go

+ 23 - 74
gupta.go

@@ -31,96 +31,45 @@ func (g *GuptaReport) GetCPULoad() string {
 
 // collect all CPU stat from /proc/cpustat
 type CPUStat struct {
-	user         uint64
-	nice         uint64
-	system       uint64
-	idle         uint64
-	iowait       uint64
-	idleNew      uint64
-	iowaitNew    uint64
-	irq          uint64
-	softirq      uint64
-	steal        uint64
-	guest        uint64
-	guest_nice   uint64
-	totalTimeNew uint64
+	totalTime    float64
+	idleTime     float64
+	totalTimeNew float64
+	idleTimeNew  float64
 }
 
-// parse the first line of /proc/cpustat without the word cpu
-func (c *CPUStat) ReadInfo(rawInfo []string) {
-	if s, err := strconv.ParseUint(rawInfo[0], 10, 64); err == nil {
-		c.user = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[1], 10, 64); err == nil {
-		c.nice = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[2], 10, 64); err == nil {
-		c.system = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[3], 10, 64); err == nil {
-		c.idle = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[4], 10, 64); err == nil {
-		c.iowait = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[5], 10, 64); err == nil {
-		c.irq = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[6], 10, 64); err == nil {
-		c.softirq = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[7], 10, 64); err == nil {
-		c.steal = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[8], 10, 64); err == nil {
-		c.guest = s
-	}
-	if s, err := strconv.ParseUint(rawInfo[9], 10, 64); err == nil {
-		c.guest_nice = s
-	}
-}
-
-func (c *CPUStat) ReadInfoNew(rawInfo []string) {
-	idleNew, _ := strconv.ParseUint(rawInfo[3], 10, 64)
-	iowaitNew, _ := strconv.ParseUint(rawInfo[4], 10, 64)
-	c.idleNew = idleNew
-	c.iowaitNew = iowaitNew
-
+func (c *CPUStat) ReadInfoNew(statline string) {
+	rawInfo := strings.Fields(statline)
+	idleNew, _ := strconv.ParseFloat(rawInfo[4], 10)
+	iowaitNew, _ := strconv.ParseFloat(rawInfo[5], 10)
+	c.idleTimeNew = idleNew + iowaitNew
 	for _, s := range rawInfo {
-		u, _ := strconv.ParseUint(s, 10, 64)
+		u, _ := strconv.ParseFloat(s, 10)
 		c.totalTimeNew += u
 	}
 }
 
-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) TotalTimeNew() uint64 {
-	return c.totalTimeNew
-}
-
-func (c *CPUStat) IdleTime() uint64 {
-	return c.idle + c.iowait
-}
-
-func (c *CPUStat) IdleTimeNew() uint64 {
-	return c.idleNew + c.iowaitNew
+func (c *CPUStat) ReadInfo(statline string) {
+	rawInfo := strings.Fields(statline)
+	for _, s := range rawInfo[1:] {
+		u, _ := strconv.ParseFloat(s, 10)
+		c.totalTime += u
+	}
+	idle, _ := strconv.ParseFloat(rawInfo[4], 10)
+	iowait, _ := strconv.ParseFloat(rawInfo[5], 10)
+	c.idleTime = idle + iowait
 }
 
 func (c *CPUStat) CPUUsage() float64 {
-	deltaIdleTime := c.IdleTimeNew() - c.IdleTime()
-	deltaTotalTime := c.TotalTimeNew() - c.TotalTime()
+	deltaIdleTime := c.idleTimeNew - c.idleTime
+	deltaTotalTime := c.totalTimeNew - c.totalTime
 	cpuUsage := (1.0 - float64(deltaIdleTime)/float64(deltaTotalTime)) * 100
 	return cpuUsage
 }
 
 func NewCPUStat(procstatline, procstatlineNew string) CPUStat {
 	cpustat := CPUStat{}
-	statline := strings.Fields(procstatline)
-	statlineNew := strings.Fields(procstatlineNew)
 
-	cpustat.ReadInfo(statline[1:])
-	cpustat.ReadInfoNew(statlineNew[1:])
+	cpustat.ReadInfo(procstatline)
+	cpustat.ReadInfoNew(procstatlineNew)
 	return cpustat
 }

+ 0 - 46
gupta_test.go

@@ -59,52 +59,6 @@ func TestCPUStat(t *testing.T) {
 
 	})
 
-	t.Run("Test the CPUStat can read info", func(t *testing.T) {
-
-		cpustat := CPUStat{}
-		cpustat.ReadInfo([]string{
-			"4705",
-			"356",
-			"584",
-			"3699",
-			"23",
-			"23",
-			"0",
-			"0",
-			"0",
-			"0"})
-		got := cpustat.user
-		want := uint64(4705)
-		name := "cpustat.user"
-		assert(t, name, got, want)
-	})
-
-	t.Run("Test the NewCPUStat can create an instance", func(t *testing.T) {
-
-		got := cpustat.user
-		want := uint64(4705)
-		name := "cpustat.user"
-		assert(t, name, got, want)
-	})
-
-	t.Run("Test that Total CPUTime calculates properly", func(t *testing.T) {
-
-		got := cpustat.TotalTime()
-		want := cpustat.user + cpustat.nice + cpustat.system + cpustat.idle + cpustat.iowait +
-			cpustat.irq + cpustat.softirq + cpustat.steal
-		name := "total time"
-		assert(t, name, got, want)
-	})
-
-	t.Run("Test that IdleTime calculates properly", func(t *testing.T) {
-
-		got := cpustat.IdleTime()
-		want := uint64(3722)
-		name := "idle time"
-		assert(t, name, got, want)
-
-	})
-
 	t.Run("Test that CPUUsage calculates properly", func(t *testing.T) {
 		cpustat = NewCPUStat("cpu 657046 84 232824 4025519 2874 0 40555 0 0 0",
 			"cpu 657136 84 232952 4025695 2874 0 40555 0 0 0")