Skip to content

Commit

Permalink
revert: "perf: Zapi allocs improvements (#2380)"
Browse files Browse the repository at this point in the history
This reverts commit 1489cfc.
  • Loading branch information
rahulguptajss committed Sep 28, 2023
1 parent 5830b93 commit bcf7426
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 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.Open(testxml)
dat, err := os.ReadFile(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,7 +16,6 @@ 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 @@ -497,22 +496,23 @@ 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, body, err = xml.Load(response.Body, c.logZapi); err != nil {
if root, err = tree.LoadXML(body); 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: 4 additions & 8 deletions pkg/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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 @@ -68,21 +67,18 @@ func consume(r *node.Node, key string, y *y3.Node, makeNewChild bool) {
}
}

func LoadXML(r io.Reader) (*node.Node, error) {
root, _, err := xml.Load(r, false)
return root, err
func LoadXML(data []byte) (*node.Node, error) {
return xml.Load(data)
}

func DumpXML(n *node.Node) ([]byte, error) {
return xml.Dump(n)
}

func ImportXML(filepath string) (*node.Node, error) {
file, err := os.Open(filepath)
data, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
//goland:noinspection GoUnhandledErrorResult
defer file.Close()
return LoadXML(file)
return LoadXML(data)
}
18 changes: 5 additions & 13 deletions pkg/tree/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@ import (
"bytes"
"encoding/xml"
"github.com/netapp/harvest/v2/pkg/tree/node"
"io"
)

func Load(data io.Reader, log bool) (*node.Node, []byte, error) {
var buf bytes.Buffer
reader := data

if log {
reader = io.TeeReader(data, &buf)
}

func Load(data []byte) (*node.Node, error) {
root := new(node.Node)
dec := xml.NewDecoder(reader)
buf := bytes.NewBuffer(data)
dec := xml.NewDecoder(buf)
if err := dec.Decode(&root); err != nil {
return nil, nil, err
return nil, err
}

return root, buf.Bytes(), nil
return root, nil
}

func Dump(n *node.Node) ([]byte, error) {
Expand Down

0 comments on commit bcf7426

Please sign in to comment.