Skip to content

Commit

Permalink
Broke the Engine out into a new 'core' package
Browse files Browse the repository at this point in the history
  • Loading branch information
liclac committed Jun 12, 2017
1 parent 45a0a6b commit be5a432
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 138 deletions.
8 changes: 4 additions & 4 deletions api/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ package common
import (
"context"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/core"
)

type ContextKey int

const ctxKeyEngine = ContextKey(1)

func WithEngine(ctx context.Context, engine *lib.Engine) context.Context {
func WithEngine(ctx context.Context, engine *core.Engine) context.Context {
return context.WithValue(ctx, ctxKeyEngine, engine)
}

func GetEngine(ctx context.Context) *lib.Engine {
return ctx.Value(ctxKeyEngine).(*lib.Engine)
func GetEngine(ctx context.Context) *core.Engine {
return ctx.Value(ctxKeyEngine).(*core.Engine)
}
6 changes: 3 additions & 3 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/GeertJohan/go.rice"
"github.com/loadimpact/k6/api/common"
"github.com/loadimpact/k6/api/v1"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/core"
log "github.com/sirupsen/logrus"
"github.com/urfave/negroni"
)
Expand All @@ -42,7 +42,7 @@ func NewHandler() http.Handler {
return mux
}

func ListenAndServe(addr string, engine *lib.Engine) error {
func ListenAndServe(addr string, engine *core.Engine) error {
mux := NewHandler()

n := negroni.New()
Expand All @@ -63,7 +63,7 @@ func NewLogger(l *log.Logger) negroni.HandlerFunc {
}
}

func WithEngine(engine *lib.Engine) negroni.HandlerFunc {
func WithEngine(engine *core.Engine) negroni.HandlerFunc {
return negroni.HandlerFunc(func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
r = r.WithContext(common.WithEngine(r.Context(), engine))
next(rw, r)
Expand Down
3 changes: 2 additions & 1 deletion api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"

"github.com/loadimpact/k6/api/common"
"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/lib"
log "github.com/sirupsen/logrus"
logtest "github.com/sirupsen/logrus/hooks/test"
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestLogger(t *testing.T) {
}

func TestWithEngine(t *testing.T) {
engine, err := lib.NewEngine(nil, lib.Options{})
engine, err := core.NewEngine(nil, lib.Options{})
if !assert.NoError(t, err) {
return
}
Expand Down
3 changes: 2 additions & 1 deletion api/v1/group_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http/httptest"
"testing"

"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/lib"
"github.com/manyminds/api2go/jsonapi"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -53,7 +54,7 @@ func TestGetGroups(t *testing.T) {
g2, err := g1.Group("group 2")
assert.NoError(t, err)

engine, err := lib.NewEngine(groupDummyRunner{g0}, lib.Options{})
engine, err := core.NewEngine(groupDummyRunner{g0}, lib.Options{})
assert.NoError(t, err)

t.Run("list", func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions api/v1/metric_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http/httptest"
"testing"

"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
"github.com/manyminds/api2go/jsonapi"
Expand All @@ -34,7 +35,7 @@ import (
)

func TestGetMetrics(t *testing.T) {
engine, err := lib.NewEngine(nil, lib.Options{})
engine, err := core.NewEngine(nil, lib.Options{})
assert.NoError(t, err)

engine.Metrics = map[string]*stats.Metric{
Expand Down Expand Up @@ -73,7 +74,7 @@ func TestGetMetrics(t *testing.T) {
}

func TestGetMetric(t *testing.T) {
engine, err := lib.NewEngine(nil, lib.Options{})
engine, err := core.NewEngine(nil, lib.Options{})
assert.NoError(t, err)

engine.Metrics = map[string]*stats.Metric{
Expand Down
4 changes: 2 additions & 2 deletions api/v1/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import (
"testing"

"github.com/loadimpact/k6/api/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/core"
"github.com/stretchr/testify/assert"
)

func newRequestWithEngine(engine *lib.Engine, method, target string, body io.Reader) *http.Request {
func newRequestWithEngine(engine *core.Engine, method, target string, body io.Reader) *http.Request {
r := httptest.NewRequest(method, target, body)
return r.WithContext(common.WithEngine(r.Context(), engine))
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
package v1

import (
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/core"
"gopkg.in/guregu/null.v3"
)

Expand All @@ -35,7 +35,7 @@ type Status struct {
Tainted bool `json:"tainted"`
}

func NewStatus(engine *lib.Engine) Status {
func NewStatus(engine *core.Engine) Status {
return Status{
Paused: null.BoolFrom(engine.IsPaused()),
VUs: null.IntFrom(engine.GetVUs()),
Expand Down
5 changes: 3 additions & 2 deletions api/v1/status_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import (
"net/http/httptest"
"testing"

"github.com/loadimpact/k6/core"
"github.com/loadimpact/k6/lib"
"github.com/manyminds/api2go/jsonapi"
"github.com/stretchr/testify/assert"
"gopkg.in/guregu/null.v3"
)

func TestGetStatus(t *testing.T) {
engine, err := lib.NewEngine(nil, lib.Options{})
engine, err := core.NewEngine(nil, lib.Options{})
assert.NoError(t, err)

rw := httptest.NewRecorder()
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestPatchStatus(t *testing.T) {

for name, indata := range testdata {
t.Run(name, func(t *testing.T) {
engine, err := lib.NewEngine(nil, lib.Options{})
engine, err := core.NewEngine(nil, lib.Options{})
assert.NoError(t, err)

body, err := jsonapi.Marshal(indata.Status)
Expand Down
23 changes: 12 additions & 11 deletions lib/engine.go → core/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

package lib
package core

import (
"context"
Expand All @@ -28,6 +28,7 @@ import (
"sync/atomic"
"time"

"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/stats"
"github.com/pkg/errors"
Expand All @@ -47,7 +48,7 @@ const (
)

type vuEntry struct {
VU VU
VU lib.VU
Cancel context.CancelFunc

Samples []stats.Sample
Expand All @@ -57,12 +58,12 @@ type vuEntry struct {

// The Engine is the beating heart of K6.
type Engine struct {
Runner Runner
Options Options
Collector Collector
Runner lib.Runner
Options lib.Options
Collector lib.Collector
Logger *log.Logger

Stages []Stage
Stages []lib.Stage
Metrics map[string]*stats.Metric
MetricsLock sync.RWMutex

Expand Down Expand Up @@ -101,7 +102,7 @@ type Engine struct {
cutoff time.Time
}

func NewEngine(r Runner, o Options) (*Engine, error) {
func NewEngine(r lib.Runner, o lib.Options) (*Engine, error) {
e := &Engine{
Runner: r,
Options: o,
Expand All @@ -126,9 +127,9 @@ func NewEngine(r Runner, o Options) (*Engine, error) {
if o.Stages != nil {
e.Stages = o.Stages
} else if o.Duration.Valid && o.Duration.Duration > 0 {
e.Stages = []Stage{{Duration: o.Duration}}
e.Stages = []lib.Stage{{Duration: o.Duration}}
} else {
e.Stages = []Stage{{}}
e.Stages = []lib.Stage{{}}
}

e.thresholds = o.Thresholds
Expand Down Expand Up @@ -497,9 +498,9 @@ func (e *Engine) processStages(dT time.Duration) (bool, error) {
to := stage.Target.Int64
t := 1.0
if stage.Duration.Duration > 0 {
t = Clampf(float64(e.atTime-e.atStageSince)/float64(stage.Duration.Duration), 0.0, 1.0)
t = lib.Clampf(float64(e.atTime-e.atStageSince)/float64(stage.Duration.Duration), 0.0, 1.0)
}
vus := Lerp(from, to, t)
vus := lib.Lerp(from, to, t)
if e.vus != vus {
e.Logger.WithFields(log.Fields{"from": e.vus, "to": vus}).Debug("processStages: interpolating...")
if err := e.setVUsNoLock(vus); err != nil {
Expand Down
Loading

0 comments on commit be5a432

Please sign in to comment.