Skip to content

Commit

Permalink
all: rename PushIfNotCancelled
Browse files Browse the repository at this point in the history
The purpose of PushIfNotCancelled is pushing the sample if the passing
context is done. But its name will make confusion, because a timeout
context also causes the context cancelled.

So changing the name to PushIfNotDone to make it clearer. While at it,
also change the select/ctx.Done pattern to ctx.Err variant. It saves us
some lines of code and maybe slightly better performance.
  • Loading branch information
cuonglm committed Dec 5, 2019
1 parent 0542636 commit b788e2e
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
6 changes: 3 additions & 3 deletions js/modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (*K6) Group(ctx context.Context, name string, fn goja.Callable) (goja.Value
tags["iter"] = strconv.FormatInt(state.Iteration, 10)
}

stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Time: t,
Metric: metrics.GroupDuration,
Tags: stats.IntoSampleTags(&tags),
Expand Down Expand Up @@ -176,10 +176,10 @@ func (*K6) Check(ctx context.Context, arg0, checks goja.Value, extras ...goja.Va
default:
if val.ToBoolean() {
atomic.AddInt64(&check.Passes, 1)
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{Time: t, Metric: metrics.Checks, Tags: sampleTags, Value: 1})
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{Time: t, Metric: metrics.Checks, Tags: sampleTags, Value: 1})
} else {
atomic.AddInt64(&check.Fails, 1)
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{Time: t, Metric: metrics.Checks, Tags: sampleTags, Value: 0})
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{Time: t, Metric: metrics.Checks, Tags: sampleTags, Value: 0})
// A single failure makes the return value false.
succ = false
}
Expand Down
2 changes: 1 addition & 1 deletion js/modules/k6/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (m Metric) Add(ctx context.Context, v goja.Value, addTags ...map[string]str
vfloat = 1.0
}

stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{Time: time.Now(), Metric: m.metric, Value: vfloat, Tags: stats.IntoSampleTags(&tags)})
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{Time: time.Now(), Metric: m.metric, Value: vfloat, Tags: stats.IntoSampleTags(&tags)})
return true, nil
}

Expand Down
8 changes: 4 additions & 4 deletions js/modules/k6/ws/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP

sampleTags := stats.IntoSampleTags(&tags)

stats.PushIfNotCancelled(ctx, state.Samples, stats.ConnectedSamples{
stats.PushIfNotDone(ctx, state.Samples, stats.ConnectedSamples{
Samples: []stats.Sample{
{Metric: metrics.WSSessions, Time: start, Tags: sampleTags, Value: 1},
{Metric: metrics.WSConnecting, Time: start, Tags: sampleTags, Value: connectionDuration},
Expand All @@ -300,7 +300,7 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP
})

for _, msgSentTimestamp := range socket.msgSentTimestamps {
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: metrics.WSMessagesSent,
Time: msgSentTimestamp,
Tags: sampleTags,
Expand All @@ -309,7 +309,7 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP
}

for _, msgReceivedTimestamp := range socket.msgReceivedTimestamps {
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: metrics.WSMessagesReceived,
Time: msgReceivedTimestamp,
Tags: sampleTags,
Expand All @@ -318,7 +318,7 @@ func (*WS) Connect(ctx context.Context, url string, args ...goja.Value) (*WSHTTP
}

for _, pingDelta := range socket.pingTimestamps {
stats.PushIfNotCancelled(ctx, state.Samples, stats.Sample{
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Metric: metrics.WSPing,
Time: pingDelta.pong,
Tags: sampleTags,
Expand Down
2 changes: 1 addition & 1 deletion lib/netext/httpext/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (t *transport) measureAndEmitMetrics(unfReq *unfinishedRequest) *finishedRe
}

trail.SaveSamples(stats.IntoSampleTags(&tags))
stats.PushIfNotCancelled(unfReq.ctx, t.state.Samples, trail)
stats.PushIfNotDone(unfReq.ctx, t.state.Samples, trail)

return result
}
Expand Down
8 changes: 3 additions & 5 deletions stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,11 @@ func GetBufferedSamples(input <-chan SampleContainer) (result []SampleContainer)
}
}

// PushIfNotCancelled first checks if the supplied context is cancelled and doesn't push
// PushIfNotDone first checks if the supplied context is done and doesn't push
// the sample container if it is.
func PushIfNotCancelled(ctx context.Context, output chan<- SampleContainer, sample SampleContainer) bool {
select {
case <-ctx.Done():
func PushIfNotDone(ctx context.Context, output chan<- SampleContainer, sample SampleContainer) bool {
if ctx.Err() != nil {
return false
default: // Do nothing
}
output <- sample
return true
Expand Down

0 comments on commit b788e2e

Please sign in to comment.