diff --git a/execution/scheduler_ext_test.go b/execution/scheduler_ext_test.go index 611239fe30f..b9f5aeaf34e 100644 --- a/execution/scheduler_ext_test.go +++ b/execution/scheduler_ext_test.go @@ -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( @@ -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 { diff --git a/js/runner.go b/js/runner.go index a3741fc7f86..c4b8d229011 100644 --- a/js/runner.go +++ b/js/runner.go @@ -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 diff --git a/js/runner_test.go b/js/runner_test.go index c0ca818086c..0808547a42f 100644 --- a/js/runner_test.go +++ b/js/runner_test.go @@ -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) @@ -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) diff --git a/lib/netext/dialer.go b/lib/netext/dialer.go index 0d0cf84f60d..4480e9fad64 100644 --- a/lib/netext/dialer.go +++ b/lib/netext/dialer.go @@ -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, @@ -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) {