Example #1
0
func TestLinuxCPU(t *testing.T) {
	setUp(t)
	defer tearDown(t)

	tests := []struct {
		stat string
		user uint64
	}{
		{"cpu 25 1 2 3 4 5 6 7", 25},
		// Ignore empty lines
		{"cpu ", 0},
	}

	statFile := procd + "/stat"
	for _, test := range tests {
		func() {
			statContents := []byte(test.stat)
			err := ioutil.WriteFile(statFile, statContents, 0644)
			if err != nil {
				t.Fatal(err)
			}
			defer os.RemoveAll(statFile)

			cpu := sigar.Cpu{}
			if assert.NoError(t, cpu.Get()) {
				assert.Equal(t, uint64(test.user), cpu.User, "cpu.User")
			}
		}()
	}
}
Example #2
0
func GetCpuTimes() (*CpuTimes, error) {

	cpu := sigar.Cpu{}
	err := cpu.Get()
	if err != nil {
		return nil, err
	}

	return &CpuTimes{Cpu: cpu}, nil
}
Example #3
0
func GetCpuTimes() (*CpuTimes, error) {

	cpu := sigar.Cpu{}
	err := cpu.Get()
	if err != nil {
		return nil, err
	}

	return &CpuTimes{
		User:    cpu.User,
		Nice:    cpu.Nice,
		System:  cpu.Sys,
		Idle:    cpu.Idle,
		IOWait:  cpu.Wait,
		Irq:     cpu.Irq,
		SoftIrq: cpu.SoftIrq,
		Steal:   cpu.Stolen,
	}, nil
}
Example #4
0
		It("Knows the process name", func() {
			writePidStats(pid, "cron", pidStatFile)
			state.Get(pid)
			Expect(state.Name).To(Equal("cron"))
		})

		It("Can handle spaces in the process name", func() {
			writePidStats(pid, "a very long process name", pidStatFile)
			state.Get(pid)
			Expect(state.Name).To(Equal("a very long process name"))
		})
	})

	Describe("CPU", func() {
		var (
			statFile string
			cpu      sigar.Cpu
		)

		BeforeEach(func() {
			statFile = procd + "/stat"
			cpu = sigar.Cpu{}
		})

		Describe("Get", func() {
			It("gets CPU usage", func() {
				statContents := []byte("cpu 25 1 2 3 4 5 6 7")
				err := ioutil.WriteFile(statFile, statContents, 0644)
				Expect(err).ToNot(HaveOccurred())

				err = cpu.Get()
				Expect(err).ToNot(HaveOccurred())