2016-01-22 22:02:21 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package win_perf_counters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-05-25 01:25:06 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
2016-01-22 22:02:21 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2018-05-25 01:25:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-01-22 22:02:21 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-05-25 01:25:06 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
2016-01-22 22:02:21 +00:00
|
|
|
)
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
type testCounter struct {
|
|
|
|
handle PDH_HCOUNTER
|
|
|
|
path string
|
|
|
|
value float64
|
|
|
|
}
|
|
|
|
type FakePerformanceQuery struct {
|
|
|
|
counters map[string]testCounter
|
|
|
|
addEnglishSupported bool
|
|
|
|
expandPaths map[string][]string
|
|
|
|
openCalled bool
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) Open() error {
|
|
|
|
if m.openCalled {
|
|
|
|
err := m.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
m.openCalled = true
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) Close() error {
|
|
|
|
if !m.openCalled {
|
|
|
|
return errors.New("CloSe: uninitialised query")
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
m.openCalled = false
|
|
|
|
return nil
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) AddCounterToQuery(counterPath string) (PDH_HCOUNTER, error) {
|
|
|
|
if !m.openCalled {
|
|
|
|
return 0, errors.New("AddCounterToQuery: uninitialised query")
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
if c, ok := m.counters[counterPath]; ok {
|
|
|
|
return c.handle, nil
|
|
|
|
} else {
|
|
|
|
return 0, errors.New(fmt.Sprintf("AddCounterToQuery: invalid counter path: %s", counterPath))
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) AddEnglishCounterToQuery(counterPath string) (PDH_HCOUNTER, error) {
|
|
|
|
if !m.openCalled {
|
|
|
|
return 0, errors.New("AddEnglishCounterToQuery: uninitialised query")
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
if c, ok := m.counters[counterPath]; ok {
|
|
|
|
return c.handle, nil
|
|
|
|
} else {
|
|
|
|
return 0, fmt.Errorf("AddEnglishCounterToQuery: invalid counter path: %s", counterPath)
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) GetCounterPath(counterHandle PDH_HCOUNTER) (string, error) {
|
|
|
|
for _, counter := range m.counters {
|
|
|
|
if counter.handle == counterHandle {
|
|
|
|
return counter.path, nil
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
return "", fmt.Errorf("GetCounterPath: invalid handle: %d", counterHandle)
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) ExpandWildCardPath(counterPath string) ([]string, error) {
|
|
|
|
if e, ok := m.expandPaths[counterPath]; ok {
|
|
|
|
return e, nil
|
|
|
|
} else {
|
|
|
|
return []string{}, fmt.Errorf("ExpandWildCardPath: invalid counter path: %s", counterPath)
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) GetFormattedCounterValueDouble(counterHandle PDH_HCOUNTER) (float64, error) {
|
|
|
|
if !m.openCalled {
|
|
|
|
return 0, errors.New("GetFormattedCounterValueDouble: uninitialised query")
|
2016-01-31 13:21:24 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
for _, counter := range m.counters {
|
|
|
|
if counter.handle == counterHandle {
|
|
|
|
if counter.value > 0 {
|
|
|
|
return counter.value, nil
|
|
|
|
} else {
|
|
|
|
if counter.value == 0 {
|
|
|
|
return 0, NewPdhError(PDH_INVALID_DATA)
|
|
|
|
} else {
|
|
|
|
return 0, NewPdhError(PDH_CALC_NEGATIVE_VALUE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-31 13:21:24 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
return 0, fmt.Errorf("GetFormattedCounterValueDouble: invalid handle: %d", counterHandle)
|
2016-01-31 13:21:24 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) CollectData() error {
|
|
|
|
if !m.openCalled {
|
|
|
|
return errors.New("CollectData: uninitialised query")
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
return nil
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func (m *FakePerformanceQuery) AddEnglishCounterSupported() bool {
|
|
|
|
return m.addEnglishSupported
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func createPerfObject(measurement string, object string, instances []string, counters []string, failOnMissing bool, includeTotal bool) []perfobject {
|
2016-01-22 22:02:21 +00:00
|
|
|
PerfObject := perfobject{
|
2018-05-25 01:25:06 +00:00
|
|
|
ObjectName: object,
|
2016-01-22 22:02:21 +00:00
|
|
|
Instances: instances,
|
|
|
|
Counters: counters,
|
|
|
|
Measurement: measurement,
|
2018-05-25 01:25:06 +00:00
|
|
|
WarnOnMissing: false,
|
|
|
|
FailOnMissing: failOnMissing,
|
|
|
|
IncludeTotal: includeTotal,
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
perfobjects := []perfobject{PerfObject}
|
|
|
|
return perfobjects
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func createCounterMap(counterPaths []string, values []float64) map[string]testCounter {
|
|
|
|
counters := make(map[string]testCounter)
|
|
|
|
for i, cp := range counterPaths {
|
|
|
|
counters[cp] = testCounter{
|
|
|
|
PDH_HCOUNTER(i),
|
|
|
|
cp,
|
|
|
|
values[i],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return counters
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestAddItemSimple(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
cps1 := []string{"\\O(I)\\C"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: nil, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(cps1, []float64{1.1}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cps1[0]: cps1,
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.AddItem(cps1[0], "I", "test", false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestAddItemInvalidCountPath(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
cps1 := []string{"\\O\\C"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: nil, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(cps1, []float64{1.1}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cps1[0]: {"\\O/C"},
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.AddItem("\\O\\C", "*", "test", false)
|
2016-01-22 22:02:21 +00:00
|
|
|
require.Error(t, err)
|
2018-05-25 01:25:06 +00:00
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestParseConfigBasic(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
perfObjects := createPerfObject("m", "O", []string{"I1", "I2"}, []string{"C1", "C2"}, false, false)
|
|
|
|
cps1 := []string{"\\O(I1)\\C1", "\\O(I1)\\C2", "\\O(I2)\\C1", "\\O(I2)\\C2"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(cps1, []float64{1.1, 1.2, 1.3, 1.4}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cps1[0]: {cps1[0]},
|
|
|
|
cps1[1]: {cps1[1]},
|
|
|
|
cps1[2]: {cps1[2]},
|
|
|
|
cps1[3]: {cps1[3]},
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.ParseConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, m.counters, 4)
|
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestParseConfigNoInstance(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
perfObjects := createPerfObject("m", "O", []string{"------"}, []string{"C1", "C2"}, false, false)
|
|
|
|
cps1 := []string{"\\O\\C1", "\\O\\C2"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(cps1, []float64{1.1, 1.2}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cps1[0]: {cps1[0]},
|
|
|
|
cps1[1]: {cps1[1]},
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.ParseConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, m.counters, 2)
|
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestParseConfigInvalidCounterError(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
perfObjects := createPerfObject("m", "O", []string{"I1", "I2"}, []string{"C1", "C2"}, true, false)
|
|
|
|
cps1 := []string{"\\O(I1)\\C2", "\\O(I2)\\C1", "\\O(I2)\\C2"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(cps1, []float64{1.1, 1.2, 1.3}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cps1[0]: {cps1[0]},
|
|
|
|
cps1[1]: {cps1[1]},
|
|
|
|
cps1[2]: {cps1[2]},
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.ParseConfig()
|
2016-01-22 22:02:21 +00:00
|
|
|
require.Error(t, err)
|
2018-05-25 01:25:06 +00:00
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestParseConfigInvalidCounterNoError(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
perfObjects := createPerfObject("m", "O", []string{"I1", "I2"}, []string{"C1", "C2"}, false, false)
|
|
|
|
cps1 := []string{"\\O(I1)\\C2", "\\O(I2)\\C1", "\\O(I2)\\C2"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(cps1, []float64{1.1, 1.2, 1.3}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cps1[0]: {cps1[0]},
|
|
|
|
cps1[1]: {cps1[1]},
|
|
|
|
cps1[2]: {cps1[2]},
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.ParseConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestParseConfigTotal(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
perfObjects := createPerfObject("m", "O", []string{"*"}, []string{"*"}, true, true)
|
|
|
|
cps1 := []string{"\\O(I1)\\C1", "\\O(I1)\\C2", "\\O(_Total)\\C1", "\\O(_Total)\\C2"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(append(cps1, "\\O(*)\\*"), []float64{1.1, 1.2, 1.3, 1.4, 0}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
"\\O(*)\\*": cps1,
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.ParseConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, m.counters, 4)
|
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
perfObjects[0].IncludeTotal = false
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
m = Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(append(cps1, "\\O(*)\\*"), []float64{1.1, 1.2, 1.3, 1.4, 0}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
"\\O(*)\\*": cps1,
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.ParseConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, m.counters, 2)
|
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2017-07-31 18:03:26 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestParseConfigExpand(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
perfObjects := createPerfObject("m", "O", []string{"*"}, []string{"*"}, false, false)
|
|
|
|
cps1 := []string{"\\O(I1)\\C1", "\\O(I1)\\C2", "\\O(I2)\\C1", "\\O(I2)\\C2"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(append(cps1, "\\O(*)\\*"), []float64{1.1, 1.2, 1.3, 1.4, 0}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
"\\O(*)\\*": cps1,
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}}
|
|
|
|
err = m.query.Open()
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = m.ParseConfig()
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, m.counters, 4)
|
|
|
|
err = m.query.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestSimpleGather(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping long taking test in short mode")
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
measurement := "test"
|
|
|
|
perfObjects := createPerfObject(measurement, "O", []string{"I"}, []string{"C"}, false, false)
|
|
|
|
cp1 := "\\O(I)\\C"
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap([]string{cp1}, []float64{1.2}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cp1: {cp1},
|
|
|
|
},
|
|
|
|
addEnglishSupported: false,
|
|
|
|
}}
|
|
|
|
var acc1 testutil.Accumulator
|
|
|
|
err = m.Gather(&acc1)
|
2016-01-22 22:02:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
fields1 := map[string]interface{}{
|
|
|
|
"C": float32(1.2),
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
tags1 := map[string]string{
|
|
|
|
"instance": "I",
|
|
|
|
"objectname": "O",
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
acc1.AssertContainsTaggedFields(t, measurement, fields1, tags1)
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestGatherInvalidDataIgnore(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping long taking test in short mode")
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
measurement := "test"
|
|
|
|
perfObjects := createPerfObject(measurement, "O", []string{"I"}, []string{"C1", "C2", "C3"}, false, false)
|
|
|
|
cps1 := []string{"\\O(I)\\C1", "\\O(I)\\C2", "\\O(I)\\C3"}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(cps1, []float64{1.2, -1, 0}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
cps1[0]: {cps1[0]},
|
|
|
|
cps1[1]: {cps1[1]},
|
|
|
|
cps1[2]: {cps1[2]},
|
|
|
|
},
|
|
|
|
addEnglishSupported: false,
|
|
|
|
}}
|
|
|
|
var acc1 testutil.Accumulator
|
|
|
|
err = m.Gather(&acc1)
|
|
|
|
require.NoError(t, err)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
fields1 := map[string]interface{}{
|
|
|
|
"C1": float32(1.2),
|
|
|
|
}
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"instance": "I",
|
|
|
|
"objectname": "O",
|
|
|
|
}
|
|
|
|
acc1.AssertContainsTaggedFields(t, measurement, fields1, tags1)
|
|
|
|
}
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
func TestGatherRefreshing(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping long taking test in short mode")
|
|
|
|
}
|
|
|
|
measurement := "test"
|
|
|
|
perfObjects := createPerfObject(measurement, "O", []string{"*"}, []string{"*"}, false, false)
|
|
|
|
cps1 := []string{"\\O(I1)\\C1", "\\O(I1)\\C2", "\\O(I2)\\C1", "\\O(I2)\\C2"}
|
|
|
|
fpm := &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(append(cps1, "\\O(*)\\*"), []float64{1.1, 1.2, 1.3, 1.4, 0}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
"\\O(*)\\*": cps1,
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
|
|
|
}
|
|
|
|
m := Win_PerfCounters{PrintValid: false, Object: perfObjects, query: fpm, CountersRefreshInterval: internal.Duration{Duration: time.Second * 10}}
|
|
|
|
var acc1 testutil.Accumulator
|
|
|
|
err = m.Gather(&acc1)
|
|
|
|
assert.Len(t, m.counters, 4)
|
2016-01-22 22:02:21 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-25 01:25:06 +00:00
|
|
|
assert.Len(t, acc1.Metrics, 2)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
fields1 := map[string]interface{}{
|
|
|
|
"C1": float32(1.1),
|
|
|
|
"C2": float32(1.2),
|
|
|
|
}
|
|
|
|
tags1 := map[string]string{
|
|
|
|
"instance": "I1",
|
|
|
|
"objectname": "O",
|
|
|
|
}
|
|
|
|
acc1.AssertContainsTaggedFields(t, measurement, fields1, tags1)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
fields2 := map[string]interface{}{
|
|
|
|
"C1": float32(1.3),
|
|
|
|
"C2": float32(1.4),
|
|
|
|
}
|
|
|
|
tags2 := map[string]string{
|
|
|
|
"instance": "I2",
|
|
|
|
"objectname": "O",
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
acc1.AssertContainsTaggedFields(t, measurement, fields2, tags2)
|
|
|
|
cps2 := []string{"\\O(I1)\\C1", "\\O(I1)\\C2", "\\O(I2)\\C1", "\\O(I2)\\C2", "\\O(I3)\\C1", "\\O(I3)\\C2"}
|
|
|
|
fpm = &FakePerformanceQuery{
|
|
|
|
counters: createCounterMap(append(cps2, "\\O(*)\\*"), []float64{1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 0}),
|
|
|
|
expandPaths: map[string][]string{
|
|
|
|
"\\O(*)\\*": cps2,
|
|
|
|
},
|
|
|
|
addEnglishSupported: true,
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
m.query = fpm
|
|
|
|
fpm.Open()
|
|
|
|
var acc2 testutil.Accumulator
|
2016-01-22 22:02:21 +00:00
|
|
|
|
2018-05-25 01:25:06 +00:00
|
|
|
fields3 := map[string]interface{}{
|
|
|
|
"C1": float32(1.5),
|
|
|
|
"C2": float32(1.6),
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
tags3 := map[string]string{
|
|
|
|
"instance": "I3",
|
|
|
|
"objectname": "O",
|
2016-01-22 22:02:21 +00:00
|
|
|
}
|
2018-05-25 01:25:06 +00:00
|
|
|
|
|
|
|
//test before elapsing CounterRefreshRate counters are not refreshed
|
|
|
|
err = m.Gather(&acc2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, m.counters, 4)
|
|
|
|
assert.Len(t, acc2.Metrics, 2)
|
|
|
|
|
|
|
|
acc2.AssertContainsTaggedFields(t, measurement, fields1, tags1)
|
|
|
|
acc2.AssertContainsTaggedFields(t, measurement, fields2, tags2)
|
|
|
|
acc2.AssertDoesNotContainsTaggedFields(t, measurement, fields3, tags3)
|
|
|
|
time.Sleep(m.CountersRefreshInterval.Duration)
|
|
|
|
|
|
|
|
var acc3 testutil.Accumulator
|
|
|
|
err = m.Gather(&acc3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, acc3.Metrics, 3)
|
|
|
|
|
|
|
|
acc3.AssertContainsTaggedFields(t, measurement, fields1, tags1)
|
|
|
|
acc3.AssertContainsTaggedFields(t, measurement, fields2, tags2)
|
|
|
|
|
|
|
|
acc3.AssertContainsTaggedFields(t, measurement, fields3, tags3)
|
2016-01-22 22:02:21 +00:00
|
|
|
|
|
|
|
}
|
2018-06-08 19:41:21 +00:00
|
|
|
|
|
|
|
// list of nul terminated strings from WinAPI
|
|
|
|
var unicodeStringListWithEnglishChars = []uint16{0x5c, 0x5c, 0x54, 0x34, 0x38, 0x30, 0x5c, 0x50, 0x68, 0x79, 0x73, 0x69, 0x63, 0x61, 0x6c, 0x44, 0x69, 0x73, 0x6b, 0x28, 0x30, 0x20, 0x43, 0x3a, 0x29, 0x5c, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x20, 0x44, 0x69, 0x73, 0x6b, 0x20, 0x51, 0x75, 0x65, 0x75, 0x65, 0x20, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x0, 0x5c, 0x5c, 0x54, 0x34, 0x38, 0x30, 0x5c, 0x50, 0x68, 0x79, 0x73, 0x69, 0x63, 0x61, 0x6c, 0x44, 0x69, 0x73, 0x6b, 0x28, 0x5f, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x29, 0x5c, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x20, 0x44, 0x69, 0x73, 0x6b, 0x20, 0x51, 0x75, 0x65, 0x75, 0x65, 0x20, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x0, 0x0}
|
|
|
|
var unicodeStringListWithCzechChars = []uint16{0x5c, 0x5c, 0x54, 0x34, 0x38, 0x30, 0x5c, 0x46, 0x79, 0x7a, 0x69, 0x63, 0x6b, 0xfd, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x28, 0x30, 0x20, 0x43, 0x3a, 0x29, 0x5c, 0x41, 0x6b, 0x74, 0x75, 0xe1, 0x6c, 0x6e, 0xed, 0x20, 0x64, 0xe9, 0x6c, 0x6b, 0x61, 0x20, 0x66, 0x72, 0x6f, 0x6e, 0x74, 0x79, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x75, 0x0, 0x5c, 0x5c, 0x54, 0x34, 0x38, 0x30, 0x5c, 0x46, 0x79, 0x7a, 0x69, 0x63, 0x6b, 0xfd, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x28, 0x5f, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x29, 0x5c, 0x41, 0x6b, 0x74, 0x75, 0xe1, 0x6c, 0x6e, 0xed, 0x20, 0x64, 0xe9, 0x6c, 0x6b, 0x61, 0x20, 0x66, 0x72, 0x6f, 0x6e, 0x74, 0x79, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x75, 0x0, 0x0}
|
|
|
|
var unicodeStringListSingleItem = []uint16{0x5c, 0x5c, 0x54, 0x34, 0x38, 0x30, 0x5c, 0x50, 0x68, 0x79, 0x73, 0x69, 0x63, 0x61, 0x6c, 0x44, 0x69, 0x73, 0x6b, 0x28, 0x30, 0x20, 0x43, 0x3a, 0x29, 0x5c, 0x43, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x20, 0x44, 0x69, 0x73, 0x6b, 0x20, 0x51, 0x75, 0x65, 0x75, 0x65, 0x20, 0x4c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x0, 0x0}
|
|
|
|
var unicodeStringListNoItem = []uint16{0x0}
|
|
|
|
|
|
|
|
var stringArrayWithEnglishChars = []string{
|
|
|
|
"\\\\T480\\PhysicalDisk(0 C:)\\Current Disk Queue Length",
|
|
|
|
"\\\\T480\\PhysicalDisk(_Total)\\Current Disk Queue Length",
|
|
|
|
}
|
|
|
|
var stringArrayWithCzechChars = []string{
|
|
|
|
"\\\\T480\\Fyzick\u00fd disk(0 C:)\\Aktu\u00e1ln\u00ed d\u00e9lka fronty disku",
|
|
|
|
"\\\\T480\\Fyzick\u00fd disk(_Total)\\Aktu\u00e1ln\u00ed d\u00e9lka fronty disku",
|
|
|
|
}
|
|
|
|
|
|
|
|
var stringArraySingleItem = []string{
|
|
|
|
"\\\\T480\\PhysicalDisk(0 C:)\\Current Disk Queue Length",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUTF16ToStringArray(t *testing.T) {
|
|
|
|
singleItem := UTF16ToStringArray(unicodeStringListSingleItem)
|
|
|
|
assert.True(t, assert.ObjectsAreEqual(singleItem, stringArraySingleItem), "Not equal single arrays")
|
|
|
|
|
|
|
|
noItem := UTF16ToStringArray(unicodeStringListNoItem)
|
|
|
|
assert.Nil(t, noItem)
|
|
|
|
|
|
|
|
engStrings := UTF16ToStringArray(unicodeStringListWithEnglishChars)
|
|
|
|
assert.True(t, assert.ObjectsAreEqual(engStrings, stringArrayWithEnglishChars), "Not equal eng arrays")
|
|
|
|
|
|
|
|
czechStrings := UTF16ToStringArray(unicodeStringListWithCzechChars)
|
|
|
|
assert.True(t, assert.ObjectsAreEqual(czechStrings, stringArrayWithCzechChars), "Not equal czech arrays")
|
|
|
|
|
|
|
|
}
|