Skip to content

Commit

Permalink
Fixed influxdata#4499 - replaced regexp with simple index searching, …
Browse files Browse the repository at this point in the history
…as it proved to be 40x faster
  • Loading branch information
vlastahajek committed Aug 20, 2018
1 parent d95824a commit e1a3053
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions plugins/inputs/win_perf_counters/win_perf_counters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"log"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -119,28 +118,46 @@ type instanceGrouping struct {
var sanitizedChars = strings.NewReplacer("/sec", "_persec", "/Sec", "_persec",
" ", "_", "%", "Percent", `\`, "")

//General Counter path pattern is: \\computer\object(parent/instance#index)\counter
//parent/instance#index part is skipped in single instance objects (e.g. Memory): \\computer\object\counter

var counterPathRE = regexp.MustCompile(`.*\\(.*)\\(.*)`)
var objectInstanceRE = regexp.MustCompile(`(.*)\((.*)\)`)

//extractObjectInstanceCounterFromQuery gets object name, instance name (if available) and counter name from counter path
func extractObjectInstanceCounterFromQuery(query string) (object string, instance string, counter string, err error) {
pathParts := counterPathRE.FindAllStringSubmatch(query, -1)
if pathParts == nil || len(pathParts[0]) != 3 {
err = errors.New("Could not extract counter info from: " + query)
// extractCounterInfoFromCounterPath gets object name, instance name (if available) and counter name from counter path
// General Counter path pattern is: \\computer\object(parent/instance#index)\counter
// parent/instance#index part is skipped in single instance objects (e.g. Memory): \\computer\object\counter
func extractCounterInfoFromCounterPath(counterPath string) (object string, instance string, counter string, err error) {
//find instance
var leftParenthesisIndex, rightParenthesisIndex, rightObjectBorderIndex int

leftParenthesisIndex = strings.Index(counterPath, "(")
if leftParenthesisIndex != -1 {
rightParenthesisIndex = strings.LastIndex(counterPath, ")")
if rightParenthesisIndex == -1 {
err = errors.New("cannot parse instance from: " + counterPath)
return
} else {
instance = counterPath[leftParenthesisIndex+1 : rightParenthesisIndex]
}
} else {
rightParenthesisIndex = -1
}
//find object
rightObjectBorderIndex = leftParenthesisIndex
if rightObjectBorderIndex == -1 {
rightObjectBorderIndex = strings.LastIndex(counterPath, "\\")
if rightObjectBorderIndex == -1 {
err = errors.New("cannot parse object from: " + counterPath)
return
}
}
leftobjectBorderIndex := strings.LastIndex(counterPath[:rightObjectBorderIndex], "\\")
if leftobjectBorderIndex == -1 {
err = errors.New("cannot parse object from: " + counterPath)
return
}
counter = pathParts[0][2]
//try to get instance name
objectInstanceParts := objectInstanceRE.FindAllStringSubmatch(pathParts[0][1], -1)
if objectInstanceParts == nil || len(objectInstanceParts[0]) != 3 {
object = pathParts[0][1]
} else {
object = objectInstanceParts[0][1]
instance = objectInstanceParts[0][2]
object = counterPath[leftobjectBorderIndex+1 : rightObjectBorderIndex]
//get countername
leftCounterBorderIndex := rightParenthesisIndex + 1
if leftCounterBorderIndex == 0 {
leftCounterBorderIndex = rightObjectBorderIndex
}
counter = counterPath[leftCounterBorderIndex+1:]
return
}

Expand Down Expand Up @@ -184,7 +201,7 @@ func (m *Win_PerfCounters) AddItem(counterPath string, objectName string, instan
var err error
counterHandle, err := m.query.AddCounterToQuery(counterPath)

objectName, instance, counterName, err = extractObjectInstanceCounterFromQuery(counterPath)
objectName, instance, counterName, err = extractCounterInfoFromCounterPath(counterPath)
if err != nil {
return err
}
Expand Down

0 comments on commit e1a3053

Please sign in to comment.