2022-05-12 19:37:38 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 14:53:17 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
|
|
|
frostfsecdsa "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/crypto/ecdsa"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2024-01-23 15:21:51 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/xk6-frostfs/internal/stats"
|
2022-05-12 19:37:38 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"go.k6.io/k6/js/modules"
|
|
|
|
"go.k6.io/k6/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RootModule is the global module object type. It is instantiated once per test
|
2022-12-29 14:52:55 +00:00
|
|
|
// run and will be used to create k6/x/frostfs/native module instances for each VU.
|
2022-05-12 19:37:38 +00:00
|
|
|
type RootModule struct{}
|
|
|
|
|
|
|
|
// Native represents an instance of the module for every VU.
|
|
|
|
type Native struct {
|
|
|
|
vu modules.VU
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure the interfaces are implemented correctly.
|
|
|
|
var (
|
|
|
|
_ modules.Instance = &Native{}
|
|
|
|
_ modules.Module = &RootModule{}
|
|
|
|
|
2024-01-24 09:44:16 +00:00
|
|
|
objPutSuccess, objPutFails, objPutDuration, objPutData *metrics.Metric
|
|
|
|
objGetSuccess, objGetFails, objGetDuration, objGetData *metrics.Metric
|
|
|
|
objDeleteSuccess, objDeleteFails, objDeleteDuration *metrics.Metric
|
|
|
|
cnrPutTotal, cnrPutFails, cnrPutDuration *metrics.Metric
|
2022-05-12 19:37:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2022-12-29 14:52:55 +00:00
|
|
|
modules.Register("k6/x/frostfs/native", new(RootModule))
|
2022-05-12 19:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewModuleInstance implements the modules.Module interface and returns
|
|
|
|
// a new instance for each VU.
|
|
|
|
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance {
|
|
|
|
mi := &Native{vu: vu}
|
|
|
|
return mi
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exports implements the modules.Instance interface and returns the exports
|
|
|
|
// of the JS module.
|
|
|
|
func (n *Native) Exports() modules.Exports {
|
|
|
|
return modules.Exports{Default: n}
|
|
|
|
}
|
|
|
|
|
2023-07-07 10:16:54 +00:00
|
|
|
func (n *Native) Connect(endpoint, hexPrivateKey string, dialTimeout, streamTimeout int, prepareLocally bool) (*Client, error) {
|
2022-05-12 19:37:38 +00:00
|
|
|
var (
|
|
|
|
cli client.Client
|
|
|
|
pk *keys.PrivateKey
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
pk, err = keys.NewPrivateKey()
|
2022-06-17 12:50:24 +00:00
|
|
|
if len(hexPrivateKey) != 0 {
|
|
|
|
pk, err = keys.NewPrivateKeyFromHex(hexPrivateKey)
|
2022-05-12 19:37:38 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var prmInit client.PrmInit
|
2024-05-05 18:28:29 +00:00
|
|
|
prmInit.Key = pk.PrivateKey
|
2022-05-12 19:37:38 +00:00
|
|
|
cli.Init(prmInit)
|
|
|
|
|
|
|
|
var prmDial client.PrmDial
|
2024-05-05 18:28:29 +00:00
|
|
|
prmDial.Endpoint = endpoint
|
2022-12-21 10:04:08 +00:00
|
|
|
|
2023-01-10 07:29:50 +00:00
|
|
|
if dialTimeout > 0 {
|
2024-05-05 18:28:29 +00:00
|
|
|
prmDial.DialTimeout = time.Duration(dialTimeout) * time.Second
|
2023-01-10 07:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if streamTimeout > 0 {
|
2024-05-05 18:28:29 +00:00
|
|
|
prmDial.StreamTimeout = time.Duration(streamTimeout) * time.Second
|
2023-01-10 07:29:50 +00:00
|
|
|
}
|
2022-05-12 19:37:38 +00:00
|
|
|
|
2023-05-23 08:16:59 +00:00
|
|
|
err = cli.Dial(n.vu.Context(), prmDial)
|
2022-05-12 19:37:38 +00:00
|
|
|
if err != nil {
|
2023-01-11 16:43:22 +00:00
|
|
|
return nil, fmt.Errorf("dial endpoint: %s %w", endpoint, err)
|
2022-05-12 19:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// generate session token
|
|
|
|
exp := uint64(math.MaxUint64)
|
2024-01-12 16:28:13 +00:00
|
|
|
sessionResp, err := cli.SessionCreate(n.vu.Context(), client.PrmSessionCreate{
|
|
|
|
Expiration: exp,
|
|
|
|
})
|
2022-05-12 19:37:38 +00:00
|
|
|
if err != nil {
|
2023-01-11 16:43:22 +00:00
|
|
|
return nil, fmt.Errorf("dial endpoint: %s %w", endpoint, err)
|
2022-05-12 19:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var id uuid.UUID
|
|
|
|
err = id.UnmarshalBinary(sessionResp.ID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("session token: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-12-29 14:52:55 +00:00
|
|
|
var key frostfsecdsa.PublicKey
|
2022-05-12 19:37:38 +00:00
|
|
|
err = key.Decode(sessionResp.PublicKey())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid public session key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var tok session.Object
|
|
|
|
|
|
|
|
tok.SetID(id)
|
|
|
|
tok.SetAuthKey(&key)
|
|
|
|
tok.SetExp(exp)
|
|
|
|
|
|
|
|
// register metrics
|
2024-01-23 15:21:51 +00:00
|
|
|
|
2024-01-24 09:44:16 +00:00
|
|
|
objPutSuccess, _ = stats.Registry.NewMetric("frostfs_obj_put_success", metrics.Counter)
|
2024-01-23 15:21:51 +00:00
|
|
|
objPutFails, _ = stats.Registry.NewMetric("frostfs_obj_put_fails", metrics.Counter)
|
|
|
|
objPutDuration, _ = stats.Registry.NewMetric("frostfs_obj_put_duration", metrics.Trend, metrics.Time)
|
2024-01-24 09:44:16 +00:00
|
|
|
objPutData, _ = stats.Registry.NewMetric("frostfs_obj_put_bytes", metrics.Counter, metrics.Data)
|
2024-01-23 15:21:51 +00:00
|
|
|
|
2024-01-24 09:44:16 +00:00
|
|
|
objGetSuccess, _ = stats.Registry.NewMetric("frostfs_obj_get_success", metrics.Counter)
|
2024-01-23 15:21:51 +00:00
|
|
|
objGetFails, _ = stats.Registry.NewMetric("frostfs_obj_get_fails", metrics.Counter)
|
|
|
|
objGetDuration, _ = stats.Registry.NewMetric("frostfs_obj_get_duration", metrics.Trend, metrics.Time)
|
2024-01-24 09:44:16 +00:00
|
|
|
objGetData, _ = stats.Registry.NewMetric("frostfs_obj_get_bytes", metrics.Counter, metrics.Data)
|
2024-01-23 15:21:51 +00:00
|
|
|
|
2024-01-24 09:44:16 +00:00
|
|
|
objDeleteSuccess, _ = stats.Registry.NewMetric("frostfs_obj_delete_success", metrics.Counter)
|
2024-01-23 15:21:51 +00:00
|
|
|
objDeleteFails, _ = stats.Registry.NewMetric("frostfs_obj_delete_fails", metrics.Counter)
|
|
|
|
objDeleteDuration, _ = stats.Registry.NewMetric("frostfs_obj_delete_duration", metrics.Trend, metrics.Time)
|
|
|
|
|
|
|
|
cnrPutTotal, _ = stats.Registry.NewMetric("frostfs_cnr_put_total", metrics.Counter)
|
|
|
|
cnrPutFails, _ = stats.Registry.NewMetric("frostfs_cnr_put_fails", metrics.Counter)
|
|
|
|
cnrPutDuration, _ = stats.Registry.NewMetric("frostfs_cnr_put_duration", metrics.Trend, metrics.Time)
|
2022-06-17 12:50:24 +00:00
|
|
|
|
2022-05-12 19:37:38 +00:00
|
|
|
return &Client{
|
2023-07-07 10:16:54 +00:00
|
|
|
vu: n.vu,
|
|
|
|
key: pk.PrivateKey,
|
|
|
|
tok: tok,
|
|
|
|
cli: &cli,
|
|
|
|
prepareLocally: prepareLocally,
|
2022-05-12 19:37:38 +00:00
|
|
|
}, nil
|
|
|
|
}
|