Skip to content

Commit

Permalink
js: stop emitting iterations data in the dialer
Browse files Browse the repository at this point in the history
This is an artifact of both data sent/received and iterations being in
the same SampleContainer. So where one was emitted we needed to emit the
other.

That hasn't been a requirement for a while as evident by the dropping of
NetTrail with no breaking in outputs.

This also stops emitting `iteration_duration` in not default functions
(teardown, setup) as it already does for `iterations`.

Closes #1605
Part of #1250
  • Loading branch information
mstoykov committed Aug 22, 2024
1 parent 739e49d commit c7599e4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 45 deletions.
43 changes: 37 additions & 6 deletions execution/scheduler_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ func TestRealTimeAndSetupTeardownMetrics(t *testing.T) {
Value: expValue,
}
}
getDummyTrail := func(group string, emitIterations bool, addExpTags ...string) metrics.SampleContainer {
getNetworkSamples := func(group string, addExpTags ...string) metrics.SampleContainer {
expTags := []string{"group", group}
expTags = append(expTags, addExpTags...)
dialer := netext.NewDialer(
Expand All @@ -1294,25 +1294,56 @@ func TestRealTimeAndSetupTeardownMetrics(t *testing.T) {
)

ctm := metrics.TagsAndMeta{Tags: getTags(piState.Registry, expTags...)}
return dialer.GetTrail(time.Now(), time.Now(), true, emitIterations, ctm, piState.BuiltinMetrics)
return dialer.Sample(time.Now(), ctm, piState.BuiltinMetrics)
}

getIterationsSamples := func(group string, addExpTags ...string) metrics.SampleContainer {
expTags := []string{"group", group}
expTags = append(expTags, addExpTags...)
ctm := metrics.TagsAndMeta{Tags: getTags(piState.Registry, expTags...)}
startTime := time.Now()
endTime := time.Now()

return metrics.Samples([]metrics.Sample{
{
TimeSeries: metrics.TimeSeries{
Metric: piState.BuiltinMetrics.IterationDuration,
Tags: ctm.Tags,
},
Time: endTime,
Metadata: ctm.Metadata,
Value: metrics.D(endTime.Sub(startTime)),
},
{
TimeSeries: metrics.TimeSeries{
Metric: piState.BuiltinMetrics.Iterations,
Tags: ctm.Tags,
},
Time: endTime,
Metadata: ctm.Metadata,
Value: 1,
},
})
}

// Initially give a long time (5s) for the execScheduler to start
expectIn(0, 5000, getSample(1, testCounter, "group", "::setup", "place", "setupBeforeSleep"))
expectIn(900, 1100, getSample(2, testCounter, "group", "::setup", "place", "setupAfterSleep"))
expectIn(0, 100, getDummyTrail("::setup", false))
expectIn(0, 100, getNetworkSamples("::setup"))

expectIn(0, 100, getSample(5, testCounter, "group", "", "place", "defaultBeforeSleep", "scenario", "default"))
expectIn(900, 1100, getSample(6, testCounter, "group", "", "place", "defaultAfterSleep", "scenario", "default"))
expectIn(0, 100, getDummyTrail("", true, "scenario", "default"))
expectIn(0, 100, getNetworkSamples("", "scenario", "default"))
expectIn(0, 100, getIterationsSamples("", "scenario", "default"))

expectIn(0, 100, getSample(5, testCounter, "group", "", "place", "defaultBeforeSleep", "scenario", "default"))
expectIn(900, 1100, getSample(6, testCounter, "group", "", "place", "defaultAfterSleep", "scenario", "default"))
expectIn(0, 100, getDummyTrail("", true, "scenario", "default"))
expectIn(0, 100, getNetworkSamples("", "scenario", "default"))
expectIn(0, 100, getIterationsSamples("", "scenario", "default"))

expectIn(0, 1000, getSample(3, testCounter, "group", "::teardown", "place", "teardownBeforeSleep"))
expectIn(900, 1100, getSample(4, testCounter, "group", "::teardown", "place", "teardownAfterSleep"))
expectIn(0, 100, getDummyTrail("::teardown", false))
expectIn(0, 100, getNetworkSamples("::teardown"))

for {
select {
Expand Down
35 changes: 32 additions & 3 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,15 +865,44 @@ func (u *VU) runFn(
u.Transport.CloseIdleConnections()
}

u.state.Samples <- u.Dialer.GetTrail(
startTime, endTime, isFullIteration,
isDefault, u.state.Tags.GetCurrentValues(), u.Runner.preInitState.BuiltinMetrics)
builtinMetrics := u.Runner.preInitState.BuiltinMetrics
ctm := u.state.Tags.GetCurrentValues()
u.state.Samples <- u.Dialer.Sample(endTime, ctm, builtinMetrics)

if isFullIteration && isDefault {
u.state.Samples <- iterationSamples(startTime, endTime, ctm, builtinMetrics)
}

v = unPromisify(v)

return v, isFullIteration, endTime.Sub(startTime), err
}

func iterationSamples(
startTime, endTime time.Time, ctm metrics.TagsAndMeta, builtinMetrics *metrics.BuiltinMetrics,
) metrics.Samples {
return metrics.Samples([]metrics.Sample{
{
TimeSeries: metrics.TimeSeries{
Metric: builtinMetrics.IterationDuration,
Tags: ctm.Tags,
},
Time: endTime,
Metadata: ctm.Metadata,
Value: metrics.D(endTime.Sub(startTime)),
},
{
TimeSeries: metrics.TimeSeries{
Metric: builtinMetrics.Iterations,
Tags: ctm.Tags,
},
Time: endTime,
Metadata: ctm.Metadata,
Value: 1,
},
})
}

func (u *ActiveVU) incrIteration() {
u.iteration++
u.state.Iteration = u.iteration
Expand Down
8 changes: 4 additions & 4 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,9 @@ func TestVUIntegrationMetrics(t *testing.T) {
require.NoError(t, err)
sampleCount := 0
builtinMetrics := r.preInitState.BuiltinMetrics
for i, sampleC := range metrics.GetBufferedSamples(samples) {
for j, s := range sampleC.GetSamples() {
sampleCount++
switch i + j {
for _, sampleC := range metrics.GetBufferedSamples(samples) {
for _, s := range sampleC.GetSamples() {
switch sampleCount {
case 0:
assert.Equal(t, 5.0, s.Value)
assert.Equal(t, "my_metric", s.Metric.Name)
Expand All @@ -814,6 +813,7 @@ func TestVUIntegrationMetrics(t *testing.T) {
assert.Same(t, builtinMetrics.Iterations, s.Metric, "`iterations` sample is after `iteration_duration`")
assert.Equal(t, float64(1), s.Value)
}
sampleCount++
}
}
assert.Equal(t, sampleCount, 5)
Expand Down
37 changes: 5 additions & 32 deletions lib/netext/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,14 @@ func (d *Dialer) DialContext(ctx context.Context, proto, addr string) (net.Conn,
return conn, err
}

// GetTrail creates a new NetTrail instance with the Dialer
// Sample creates a new NetTrail instance with the Dialer
// sent and received data metrics and the supplied times and tags.
// TODO: Refactor this according to
// https://github.com/k6io/k6/pull/1203#discussion_r337938370
func (d *Dialer) GetTrail(
startTime, endTime time.Time, fullIteration bool, emitIterations bool, ctm metrics.TagsAndMeta,
builtinMetrics *metrics.BuiltinMetrics,
func (d *Dialer) Sample(
endTime time.Time, ctm metrics.TagsAndMeta, builtinMetrics *metrics.BuiltinMetrics,
) metrics.SampleContainer {
bytesWritten := atomic.SwapInt64(&d.BytesWritten, 0)
bytesRead := atomic.SwapInt64(&d.BytesRead, 0)
samples := []metrics.Sample{
return metrics.Samples([]metrics.Sample{
{
TimeSeries: metrics.TimeSeries{
Metric: builtinMetrics.DataSent,
Expand All @@ -98,31 +95,7 @@ func (d *Dialer) GetTrail(
Metadata: ctm.Metadata,
Value: float64(bytesRead),
},
}
if fullIteration {
samples = append(samples, metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: builtinMetrics.IterationDuration,
Tags: ctm.Tags,
},
Time: endTime,
Metadata: ctm.Metadata,
Value: metrics.D(endTime.Sub(startTime)),
})
if emitIterations {
samples = append(samples, metrics.Sample{
TimeSeries: metrics.TimeSeries{
Metric: builtinMetrics.Iterations,
Tags: ctm.Tags,
},
Time: endTime,
Metadata: ctm.Metadata,
Value: 1,
})
}
}

return metrics.Samples(samples)
})
}

func (d *Dialer) getDialAddr(addr string) (string, error) {
Expand Down

0 comments on commit c7599e4

Please sign in to comment.