Avoid repeated regexp compilations

pull/1569/head
Ville Skyttä 1 year ago
parent 108235a098
commit 362fa4b9c4

@ -36,6 +36,8 @@ func Times(percpu bool) ([]TimesStat, error) {
return TimesWithContext(context.Background(), percpu) return TimesWithContext(context.Background(), percpu)
} }
var kstatSplit = regexp.MustCompile(`[:\s]+`)
func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) { func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/") kstatSysOut, err := invoke.CommandWithContext(ctx, "kstat", "-p", "cpu_stat:*:*:/^idle$|^user$|^kernel$|^iowait$|^swap$/")
if err != nil { if err != nil {
@ -47,9 +49,8 @@ func TimesWithContext(ctx context.Context, percpu bool) ([]TimesStat, error) {
kern := make(map[float64]float64) kern := make(map[float64]float64)
iowt := make(map[float64]float64) iowt := make(map[float64]float64)
// swap := make(map[float64]float64) // swap := make(map[float64]float64)
re := regexp.MustCompile(`[:\s]+`)
for _, line := range strings.Split(string(kstatSysOut), "\n") { for _, line := range strings.Split(string(kstatSysOut), "\n") {
fields := re.Split(line, -1) fields := kstatSplit.Split(line, -1)
if fields[0] != "cpu_stat" { if fields[0] != "cpu_stat" {
continue continue
} }

@ -83,6 +83,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
return ret, err return ret, err
} }
var kstatSplit = regexp.MustCompile(`[:\s]+`)
func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) {
var issolaris bool var issolaris bool
if runtime.GOOS == "illumos" { if runtime.GOOS == "illumos" {
@ -107,7 +109,6 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
writesarr := make(map[string]uint64) writesarr := make(map[string]uint64)
rtimearr := make(map[string]uint64) rtimearr := make(map[string]uint64)
wtimearr := make(map[string]uint64) wtimearr := make(map[string]uint64)
re := regexp.MustCompile(`[:\s]+`)
// in case the name is "/dev/sda1", then convert to "sda1" // in case the name is "/dev/sda1", then convert to "sda1"
for i, name := range names { for i, name := range names {
@ -115,7 +116,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
} }
for _, line := range lines { for _, line := range lines {
fields := re.Split(line, -1) fields := kstatSplit.Split(line, -1)
if len(fields) == 0 { if len(fields) == 0 {
continue continue
} }

@ -339,13 +339,15 @@ func getSlackwareVersion(contents []string) string {
return c return c
} }
var redhatishReleaseMatch = regexp.MustCompile(`release (\w[\d.]*)`)
func getRedhatishVersion(contents []string) string { func getRedhatishVersion(contents []string) string {
c := strings.ToLower(strings.Join(contents, "")) c := strings.ToLower(strings.Join(contents, ""))
if strings.Contains(c, "rawhide") { if strings.Contains(c, "rawhide") {
return "rawhide" return "rawhide"
} }
if matches := regexp.MustCompile(`release (\w[\d.]*)`).FindStringSubmatch(c); matches != nil { if matches := redhatishReleaseMatch.FindStringSubmatch(c); matches != nil {
return matches[1] return matches[1]
} }
return "" return ""
@ -362,12 +364,17 @@ func getRedhatishPlatform(contents []string) string {
return f[0] return f[0]
} }
var (
suseVersionMatch = regexp.MustCompile(`VERSION = ([\d.]+)`)
susePatchLevelMatch = regexp.MustCompile(`PATCHLEVEL = (\d+)`)
)
func getSuseVersion(contents []string) string { func getSuseVersion(contents []string) string {
version := "" version := ""
for _, line := range contents { for _, line := range contents {
if matches := regexp.MustCompile(`VERSION = ([\d.]+)`).FindStringSubmatch(line); matches != nil { if matches := suseVersionMatch.FindStringSubmatch(line); matches != nil {
version = matches[1] version = matches[1]
} else if matches := regexp.MustCompile(`PATCHLEVEL = ([\d]+)`).FindStringSubmatch(line); matches != nil { } else if matches = susePatchLevelMatch.FindStringSubmatch(line); matches != nil {
version = version + "." + matches[1] version = version + "." + matches[1]
} }
} }

@ -23,6 +23,8 @@ func IOCounters(pernic bool) ([]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), pernic) return IOCountersWithContext(context.Background(), pernic)
} }
var kstatSplit = regexp.MustCompile(`[:\s]+`)
func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) { func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
// collect all the net class's links with below statistics // collect all the net class's links with below statistics
filterstr := "/^(?!vnic)/::phys:/^rbytes64$|^ipackets64$|^idrops64$|^ierrors$|^obytes64$|^opackets64$|^odrops64$|^oerrors$/" filterstr := "/^(?!vnic)/::phys:/^rbytes64$|^ipackets64$|^idrops64$|^ierrors$|^obytes64$|^opackets64$|^odrops64$|^oerrors$/"
@ -47,9 +49,8 @@ func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat,
odrops64arr := make(map[string]uint64) odrops64arr := make(map[string]uint64)
oerrorsarr := make(map[string]uint64) oerrorsarr := make(map[string]uint64)
re := regexp.MustCompile(`[:\s]+`)
for _, line := range lines { for _, line := range lines {
fields := re.Split(line, -1) fields := kstatSplit.Split(line, -1)
interfaceName := fields[0] interfaceName := fields[0]
instance := fields[1] instance := fields[1]
switch fields[3] { switch fields[3] {

Loading…
Cancel
Save