chore: optimize registry collector (#1683)

This commit is contained in:
Jan-Otto Kröpke 2024-10-11 00:18:36 +02:00 committed by GitHub
parent f46f9082f9
commit 22fdb33b4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 17 additions and 6 deletions

View File

@ -39,7 +39,7 @@ func (c *Collector) Describe() map[string]string {
}
func (c *Collector) Collect() (map[string]map[string]perftypes.CounterValues, error) {
perfObjects, err := QueryPerformanceData(c.query)
perfObjects, err := QueryPerformanceData(c.query, c.object)
if err != nil {
return nil, fmt.Errorf("QueryPerformanceData: %w", err)
}

View File

@ -272,7 +272,7 @@ The query can be any of the following:
Many objects have dependencies - if you query one of them, you often get back
more than you asked for.
*/
func QueryPerformanceData(query string) ([]*PerfObject, error) {
func QueryPerformanceData(query string, counterName string) ([]*PerfObject, error) {
buffer, err := queryRawData(query)
if err != nil {
return nil, err
@ -297,6 +297,8 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
// Parse the performance data
numObjects := int(header.NumObjectTypes)
numFilteredObjects := 0
objects := make([]*PerfObject, numObjects)
objOffset := int64(header.HeaderLength)
@ -314,6 +316,14 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
return nil, err
}
perfCounterName := obj.LookupName()
if counterName != "" && perfCounterName != counterName {
objOffset += int64(obj.TotalByteLength)
continue
}
numCounterDefs := int(obj.NumCounters)
numInstances := int(obj.NumInstances)
@ -328,7 +338,7 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
counterDefs := make([]*PerfCounterDef, numCounterDefs)
objects[i] = &PerfObject{
Name: obj.LookupName(),
Name: perfCounterName,
NameIndex: uint(obj.ObjectNameTitleIndex),
Instances: instances,
CounterDefs: counterDefs,
@ -410,9 +420,10 @@ func QueryPerformanceData(query string) ([]*PerfObject, error) {
// Next perfObjectType
objOffset += int64(obj.TotalByteLength)
numFilteredObjects++
}
return objects, nil
return objects[:numFilteredObjects], nil
}
func parseCounterBlock(b []byte, r io.ReadSeeker, pos int64, defs []*PerfCounterDef) (int64, []*PerfCounter, error) {

View File

@ -6,6 +6,6 @@ import (
func BenchmarkQueryPerformanceData(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = QueryPerformanceData("Global")
_, _ = QueryPerformanceData("Global", "")
}
}

View File

@ -9,7 +9,7 @@ func MapCounterToIndex(name string) string {
}
func GetPerflibSnapshot(objNames string) (map[string]*PerfObject, error) {
objects, err := QueryPerformanceData(objNames)
objects, err := QueryPerformanceData(objNames, "")
if err != nil {
return nil, err
}