From faba352e3625190224179583efa0e8ca74f14dc5 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 25 May 2022 11:22:40 +0300 Subject: [PATCH] [#3] native: Configure buffer size Default value is increased up to 64 KiB. Signed-off-by: Alex Vanin --- README.md | 2 ++ internal/native/client.go | 42 ++++++++++++++++++++++++++------------- internal/native/native.go | 9 +++++---- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4e045ed..4c8bfd6 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ const neofs_cli = native.connect("s01.neofs.devenv:8080", "") ``` #### Methods +- `setBufferSize(size)`. Sets internal buffer size for data upload and + download. Default is 64 KiB. - `put(container_id, headers, payload)`. Returns dictionary with `success` boolean flag, `object_id` string, and `error` string. - `get(container_id, object_id)`. Returns dictionary with `success` boolean diff --git a/internal/native/client.go b/internal/native/client.go index f7f94cf..f02ffe2 100644 --- a/internal/native/client.go +++ b/internal/native/client.go @@ -30,10 +30,11 @@ import ( type ( Client struct { - vu modules.VU - key ecdsa.PrivateKey - tok session.Object - cli *client.Client + vu modules.VU + key ecdsa.PrivateKey + tok session.Object + cli *client.Client + bufsize int } PutResponse struct { @@ -48,16 +49,28 @@ type ( } PreparedObject struct { - vu modules.VU - key ecdsa.PrivateKey - cli *client.Client + vu modules.VU + key ecdsa.PrivateKey + cli *client.Client + bufsize int hdr object.Object payload []byte } ) -const defaultPutBufferSize = 4 * 1024 +const defaultBufferSize = 64 * 1024 + +func (c *Client) SetBufferSize(size int) { + if size < 0 { + panic("buffer size must be positive") + } + if size == 0 { + c.bufsize = defaultBufferSize + } else { + c.bufsize = size + } +} func (c *Client) Put(inputContainerID string, headers map[string]string, payload goja.ArrayBuffer) PutResponse { var containerID cid.ID @@ -93,7 +106,7 @@ func (c *Client) Put(inputContainerID string, headers map[string]string, payload o.SetOwnerID(&owner) o.SetAttributes(attrs...) - resp, err := put(c.vu, defaultPutBufferSize, c.cli, &tok, &o, payload.Bytes()) + resp, err := put(c.vu, c.bufsize, c.cli, &tok, &o, payload.Bytes()) if err != nil { return PutResponse{Success: false, Error: err.Error()} } @@ -106,7 +119,7 @@ func (c *Client) Put(inputContainerID string, headers map[string]string, payload func (c *Client) Get(inputContainerID, inputObjectID string) GetResponse { var ( - buf = make([]byte, 4*1024) + buf = make([]byte, c.bufsize) sz int ) @@ -216,9 +229,10 @@ func (c *Client) Onsite(inputContainerID string, payload goja.ArrayBuffer) Prepa } return PreparedObject{ - vu: c.vu, - key: c.key, - cli: c.cli, + vu: c.vu, + key: c.key, + cli: c.cli, + bufsize: c.bufsize, hdr: *obj, payload: data, @@ -247,7 +261,7 @@ func (p PreparedObject) Put(headers map[string]string) PutResponse { return PutResponse{Success: false, Error: err.Error()} } - _, err = put(p.vu, defaultPutBufferSize, p.cli, nil, &obj, p.payload) + _, err = put(p.vu, p.bufsize, p.cli, nil, &obj, p.payload) if err != nil { return PutResponse{Success: false, Error: err.Error()} } diff --git a/internal/native/native.go b/internal/native/native.go index 675e22a..ebabc4d 100644 --- a/internal/native/native.go +++ b/internal/native/native.go @@ -116,9 +116,10 @@ func (n *Native) Connect(endpoint, wif string) (*Client, error) { objGetDuration, _ = registry.NewMetric("neofs_obj_get_duration", metrics.Trend, metrics.Time) return &Client{ - vu: n.vu, - key: pk.PrivateKey, - tok: tok, - cli: &cli, + vu: n.vu, + key: pk.PrivateKey, + tok: tok, + cli: &cli, + bufsize: defaultBufferSize, }, nil }