From 1489cfc7da74dafd23c36030afce2846e7fa2e60 Mon Sep 17 00:00:00 2001 From: Rahul Date: Tue, 26 Sep 2023 21:58:21 +0530 Subject: [PATCH] perf: Zapi allocs improvements (#2380) * perf: Zapi allocs improvements * perf: Zapi allocs improvements --- cmd/collectors/sensor_test.go | 2 +- pkg/api/ontapi/zapi/client.go | 22 +++++++++++----------- pkg/tree/tree.go | 12 ++++++++---- pkg/tree/xml/xml.go | 18 +++++++++++++----- 4 files changed, 33 insertions(+), 21 deletions(-) diff --git a/cmd/collectors/sensor_test.go b/cmd/collectors/sensor_test.go index 6dd70c09d..6424bcac1 100644 --- a/cmd/collectors/sensor_test.go +++ b/cmd/collectors/sensor_test.go @@ -27,7 +27,7 @@ func loadTestdata() { // setup matrix data var err error var fetch func(*matrix.Instance, *node.Node, []string) - dat, err := os.ReadFile(testxml) + dat, err := os.Open(testxml) if err != nil { abs, _ := filepath.Abs(testxml) fmt.Printf("failed to load %s\n", abs) diff --git a/pkg/api/ontapi/zapi/client.go b/pkg/api/ontapi/zapi/client.go index 5e6ab2166..3af08869a 100644 --- a/pkg/api/ontapi/zapi/client.go +++ b/pkg/api/ontapi/zapi/client.go @@ -16,6 +16,7 @@ import ( "github.com/netapp/harvest/v2/pkg/requests" "github.com/netapp/harvest/v2/pkg/tree" "github.com/netapp/harvest/v2/pkg/tree/node" + "github.com/netapp/harvest/v2/pkg/tree/xml" "io" "net/http" "strconv" @@ -496,23 +497,22 @@ func (c *Client) invoke(withTimers bool) (*node.Node, time.Duration, time.Durati return result, responseT, parseT, errs.New(errs.ErrAPIResponse, response.Status, errs.WithStatus(response.StatusCode)) } - // read response body - if body, err = io.ReadAll(response.Body); err != nil { - return result, responseT, parseT, err - } - defer c.printRequestAndResponse(zapiReq, body) - // parse xml if withTimers { start = time.Now() } - if root, err = tree.LoadXML(body); err != nil { + if root, body, err = xml.Load(response.Body, c.logZapi); err != nil { return result, responseT, parseT, err } if withTimers { parseT = time.Since(start) } + // read response body + if c.logZapi { + defer c.printRequestAndResponse(zapiReq, body) + } + // check if the request was successful if result = root.GetChildS("results"); result == nil { return result, responseT, parseT, errs.New(errs.ErrAPIResponse, "missing \"results\"") @@ -551,11 +551,11 @@ func (c *Client) TraceLogSet(collectorName string, config *node.Node) { } func (c *Client) printRequestAndResponse(req string, response []byte) { - res := "" - if response != nil { - res = string(response) - } if req != "" { + res := "" + if response != nil { + res = string(response) + } c.Logger.Info(). Str("Request", req). Str("Response", res). diff --git a/pkg/tree/tree.go b/pkg/tree/tree.go index ef6314852..692a2eb91 100644 --- a/pkg/tree/tree.go +++ b/pkg/tree/tree.go @@ -9,6 +9,7 @@ import ( "github.com/netapp/harvest/v2/pkg/tree/node" "github.com/netapp/harvest/v2/pkg/tree/xml" y3 "gopkg.in/yaml.v3" + "io" "os" ) @@ -67,8 +68,9 @@ func consume(r *node.Node, key string, y *y3.Node, makeNewChild bool) { } } -func LoadXML(data []byte) (*node.Node, error) { - return xml.Load(data) +func LoadXML(r io.Reader) (*node.Node, error) { + root, _, err := xml.Load(r, false) + return root, err } func DumpXML(n *node.Node) ([]byte, error) { @@ -76,9 +78,11 @@ func DumpXML(n *node.Node) ([]byte, error) { } func ImportXML(filepath string) (*node.Node, error) { - data, err := os.ReadFile(filepath) + file, err := os.Open(filepath) if err != nil { return nil, err } - return LoadXML(data) + //goland:noinspection GoUnhandledErrorResult + defer file.Close() + return LoadXML(file) } diff --git a/pkg/tree/xml/xml.go b/pkg/tree/xml/xml.go index a213cd758..0941bc67e 100644 --- a/pkg/tree/xml/xml.go +++ b/pkg/tree/xml/xml.go @@ -8,16 +8,24 @@ import ( "bytes" "encoding/xml" "github.com/netapp/harvest/v2/pkg/tree/node" + "io" ) -func Load(data []byte) (*node.Node, error) { +func Load(data io.Reader, log bool) (*node.Node, []byte, error) { + var buf bytes.Buffer + reader := data + + if log { + reader = io.TeeReader(data, &buf) + } + root := new(node.Node) - buf := bytes.NewBuffer(data) - dec := xml.NewDecoder(buf) + dec := xml.NewDecoder(reader) if err := dec.Decode(&root); err != nil { - return nil, err + return nil, nil, err } - return root, nil + + return root, buf.Bytes(), nil } func Dump(n *node.Node) ([]byte, error) {