Skip to content

Commit

Permalink
go/oasis-node/txsource: Add runtime message requests to runtime workload
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Nov 12, 2020
1 parent f342014 commit 4209836
Showing 1 changed file with 66 additions and 10 deletions.
76 changes: 66 additions & 10 deletions go/oasis-node/cmd/debug/txsource/workload/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ const (
// CfgRuntimeID is the runtime workload runtime ID.
CfgRuntimeID = "runtime.runtime_id"

// Weights to select between requests types.
runtimeDoInsertRequestWeight = 2
runtimeDoGetRequestTypeWeight = 1
runtimeDoRemoveRequestTypeWeight = 2

// Ratio of insert requests that should be an upsert.
runtimeInsertExistingRatio = 0.3
// Ratio of get requests that should get an existing key.
Expand All @@ -41,6 +36,24 @@ const (
runtimeRequestTimeout = 120 * time.Second
)

// Possible request types.
type runtimeRequest uint8

const (
runtimeRequestInsert runtimeRequest = 0
runtimeRequestGet runtimeRequest = 1
runtimeRequestRemove runtimeRequest = 2
runtimeRequestMessage runtimeRequest = 3
)

// Weights to select between requests types.
var runtimeRequestWeights = map[runtimeRequest]int{
runtimeRequestInsert: 2,
runtimeRequestGet: 1,
runtimeRequestRemove: 2,
runtimeRequestMessage: 1,
}

// RuntimeFlags are the runtime workload flags.
var RuntimeFlags = flag.NewFlagSet("", flag.ContinueOnError)

Expand Down Expand Up @@ -271,6 +284,28 @@ func (r *runtime) doRemoveRequest(ctx context.Context, rng *rand.Rand, rtc runti
return nil
}

func (r *runtime) doMessageRequest(ctx context.Context, rng *rand.Rand, rtc runtimeClient.RuntimeClient) error {
// Submit request.
req := &runtimeTransaction.TxnCall{
Method: "message",
Args: rng.Uint64(),
}
rsp, err := r.submitRuntimeRquest(ctx, rtc, req)
if err != nil {
r.logger.Error("Submit message request failure",
"request", req,
"err", err,
)
return fmt.Errorf("submit message request failed: %w", err)
}

r.logger.Debug("message request success",
"request", req,
"response", rsp,
)
return nil
}

// Implements Workload.
func (r *runtime) NeedsFunds() bool {
return false
Expand Down Expand Up @@ -307,21 +342,42 @@ func (r *runtime) Run(
return fmt.Errorf("failed waiting for 2nd epoch: %w", err)
}

var totalWeight int
for _, w := range runtimeRequestWeights {
totalWeight = totalWeight + w
}

for {
p := rng.Intn(runtimeDoInsertRequestWeight + runtimeDoGetRequestTypeWeight + runtimeDoRemoveRequestTypeWeight)
switch {
case p < runtimeDoInsertRequestWeight:
// Determine which request to perform based on the configured weight table.
p := rng.Intn(totalWeight)
var (
cw int
request runtimeRequest
)
for r, w := range runtimeRequestWeights {
if cw = cw + w; p < cw {
request = r
break
}
}

switch request {
case runtimeRequestInsert:
if err := r.doInsertRequest(ctx, rng, rtc, rng.Float64() < runtimeInsertExistingRatio); err != nil {
return fmt.Errorf("doInsertRequest failure: %w", err)
}
case p < runtimeDoInsertRequestWeight+runtimeDoGetRequestTypeWeight:
case runtimeRequestGet:
if err := r.doGetRequest(ctx, rng, rtc, rng.Float64() < runtimeGetExistingRatio); err != nil {
return fmt.Errorf("doGetRequest failure: %w", err)
}
case p < runtimeDoInsertRequestWeight+runtimeDoGetRequestTypeWeight+runtimeDoRemoveRequestTypeWeight:
case runtimeRequestRemove:
if err := r.doRemoveRequest(ctx, rng, rtc, rng.Float64() < runtimeRemoveExistingRatio); err != nil {
return fmt.Errorf("doRemoveRequest failure: %w", err)
}
case runtimeRequestMessage:
if err := r.doMessageRequest(ctx, rng, rtc); err != nil {
return fmt.Errorf("doMessageRequest failure: %w", err)
}
default:
return fmt.Errorf("unimplemented")
}
Expand Down

0 comments on commit 4209836

Please sign in to comment.