diff --git a/runner/go.mod b/runner/go.mod index b666aea..2c0daec 100644 --- a/runner/go.mod +++ b/runner/go.mod @@ -2,9 +2,13 @@ module github.com/benchttp/desktop/runner go 1.17 -require github.com/benchttp/engine v0.1.1 +require ( + github.com/benchttp/sdk v0.1.1 +) require ( golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/benchttp/sdk => ../../engine diff --git a/runner/httpclient/handle.go b/runner/httpclient/handle.go index bdc0f7a..eb5e1df 100644 --- a/runner/httpclient/handle.go +++ b/runner/httpclient/handle.go @@ -2,13 +2,12 @@ package httpclient import ( "errors" - "io" "log" "net/http" "time" - "github.com/benchttp/engine/configparse" - "github.com/benchttp/engine/runner" + "github.com/benchttp/sdk/benchttp" + "github.com/benchttp/sdk/configio" "github.com/benchttp/desktop/runner/httpclient/response" ) @@ -16,28 +15,24 @@ import ( func handle(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") - b, err := io.ReadAll(r.Body) - if err != nil { - internalError(w, err) - return - } + runner := benchttp.DefaultRunner() + runner.OnProgress = streamProgress(w) - cfg, err := configparse.JSON(b) - if err != nil { + if err := configio.NewJSONDecoder(r.Body).DecodeRunner(&runner); err != nil { clientError(w, err) return } - rep, err := runner.New(streamProgress(w)).Run(r.Context(), cfg) - var invalidConfigError *runner.InvalidConfigError + report, err := runner.Run(r.Context()) + var errInvalidRunner *benchttp.InvalidRunnerError switch { case err == nil: // Pass through. - case err == runner.ErrCanceled: + case err == benchttp.ErrCanceled: clientError(w, err) return - case errors.As(err, &invalidConfigError): - clientError(w, invalidConfigError.Errors...) + case errors.As(err, &errInvalidRunner): + clientError(w, errInvalidRunner.Errors...) return default: internalError(w, err) @@ -49,14 +44,14 @@ func handle(w http.ResponseWriter, r *http.Request) { // The issue is likely on the read side (front-end), but this is // the easiest fix for now. time.Sleep(10 * time.Millisecond) - if err := response.Report(rep).EncodeJSON(w); err != nil { + if err := response.Report(report).EncodeJSON(w); err != nil { internalError(w, err) return } } -func streamProgress(w http.ResponseWriter) func(runner.RecordingProgress) { - return func(progress runner.RecordingProgress) { +func streamProgress(w http.ResponseWriter) func(benchttp.RecordingProgress) { + return func(progress benchttp.RecordingProgress) { if err := response.Progress(progress).EncodeJSON(w); err != nil { internalError(w, err) } diff --git a/runner/httpclient/response/progress.go b/runner/httpclient/response/progress.go index 7a3674b..be93ab5 100644 --- a/runner/httpclient/response/progress.go +++ b/runner/httpclient/response/progress.go @@ -3,10 +3,10 @@ package response import ( "time" - "github.com/benchttp/engine/runner" + "github.com/benchttp/sdk/benchttp" ) -func Progress(in runner.RecordingProgress) Response { +func Progress(in benchttp.RecordingProgress) Response { return newResponse(progressResponse{ Done: in.Done, Error: in.Error, diff --git a/runner/httpclient/response/report.go b/runner/httpclient/response/report.go index c4ba4aa..52a4c33 100644 --- a/runner/httpclient/response/report.go +++ b/runner/httpclient/response/report.go @@ -3,10 +3,10 @@ package response import ( "time" - "github.com/benchttp/engine/runner" + "github.com/benchttp/sdk/benchttp" ) -func Report(rep *runner.Report) Response { +func Report(rep *benchttp.Report) Response { return newResponse(reportResponse{ Metadata: metadataResponse{ FinishedAt: rep.Metadata.FinishedAt, @@ -87,7 +87,7 @@ type requestFailureResponse struct { Reason string `json:"reason"` } -func toTestResultsResponse(testResults []runner.TestCaseResult) []testResultResponse { +func toTestResultsResponse(testResults []benchttp.TestCaseResult) []testResultResponse { resp := make([]testResultResponse, len(testResults)) for i, r := range testResults { resp[i] = testResultResponse{ @@ -104,7 +104,7 @@ func toTestResultsResponse(testResults []runner.TestCaseResult) []testResultResp return resp } -func toTimeStatsResponse(stats runner.MetricsTimeStats) timeStatsResponse { +func toTimeStatsResponse(stats benchttp.MetricsTimeStats) timeStatsResponse { return timeStatsResponse{ Min: stats.Min, Max: stats.Max, @@ -116,7 +116,7 @@ func toTimeStatsResponse(stats runner.MetricsTimeStats) timeStatsResponse { } } -func toRequestEventTimesResponse(in map[string]runner.MetricsTimeStats) map[string]timeStatsResponse { +func toRequestEventTimesResponse(in map[string]benchttp.MetricsTimeStats) map[string]timeStatsResponse { resp := map[string]timeStatsResponse{} for k, v := range in { resp[k] = toTimeStatsResponse(v)