Skip to content

Commit

Permalink
perf: Zapi allocs improvements (#2380)
Browse files Browse the repository at this point in the history
* perf: Zapi allocs improvements

* perf: Zapi allocs improvements
  • Loading branch information
rahulguptajss authored Sep 26, 2023
1 parent 51ed990 commit 1489cfc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion cmd/collectors/sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions pkg/api/ontapi/zapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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\"")
Expand Down Expand Up @@ -551,11 +551,11 @@ func (c *Client) TraceLogSet(collectorName string, config *node.Node) {
}

func (c *Client) printRequestAndResponse(req string, response []byte) {
res := "<nil>"
if response != nil {
res = string(response)
}
if req != "" {
res := "<nil>"
if response != nil {
res = string(response)
}
c.Logger.Info().
Str("Request", req).
Str("Response", res).
Expand Down
12 changes: 8 additions & 4 deletions pkg/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -67,18 +68,21 @@ 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) {
return xml.Dump(n)
}

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)
}
18 changes: 13 additions & 5 deletions pkg/tree/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1489cfc

Please sign in to comment.