From 4e9b263f59b161f67969f76670395c0379e368c1 Mon Sep 17 00:00:00 2001 From: Diretnan Domnan Date: Tue, 26 Oct 2021 23:23:39 +0100 Subject: [PATCH] added simple response time inspector for web driver --- driver/web.go | 78 +++++++++++++++++++++++++++++++++ inspector/rt.go | 42 ++++++++++++++++++ integration/integration_test.go | 14 ++++++ 3 files changed, 134 insertions(+) create mode 100644 driver/web.go create mode 100644 inspector/rt.go diff --git a/driver/web.go b/driver/web.go new file mode 100644 index 0000000..8036115 --- /dev/null +++ b/driver/web.go @@ -0,0 +1,78 @@ +package driver + +import ( + "errors" + "fmt" + "io" + "net/http" + "strconv" + "time" + + log "github.com/sirupsen/logrus" +) + +// Request : use specified request methods for web +type Request string + +const ( + // POST : HTTP post + POST Request = "POST" + // GET : HTTP get + GET Request = "GET" +) + +// Web : Driver for handling ssh executions +type Web struct { + fields + // URL e.g https://google.com + URL string + // Method POST/GET + Method Request + // Body in case of a POST + Body io.Reader +} + +func (d *Web) String() string { + return fmt.Sprintf("%s (%s)", d.URL, d.Method) +} + +func (d *Web) ReadFile(path string) (string, error) { + log.Debug("Cannot read file on web driver") + return ``, errors.New("Cannot read file on web driver") +} + +func (d *Web) RunCommand(command string) (string, error) { + if command == `response` { + var res *http.Response + var err error + start := time.Now() + if d.Method == POST { + res, err = http.Post(d.URL, "application/json", d.Body) + defer res.Body.Close() + } else { + res, err = http.Get(d.URL) + defer res.Body.Close() + } + if err != nil || res.StatusCode < 200 || res.StatusCode > 299 { + message := fmt.Sprintf("Error %s running request: %s", err, string(res.StatusCode)) + return ``, errors.New(message) + } + elapsed := time.Since(start) + return strconv.FormatFloat(elapsed.Seconds(), 'f', 6, 64), nil + } + return ``, errors.New("Cannot read file on web driver") +} + +func (d *Web) GetDetails() string { + return fmt.Sprintf(`Web - %s`, d.String()) +} + +func NewWebForTest() *Web { + return &Web{ + URL: "https://duckduckgo.com", + Method: GET, + fields: fields{ + PollInterval: 5, + }, + } +} diff --git a/inspector/rt.go b/inspector/rt.go new file mode 100644 index 0000000..f8880b4 --- /dev/null +++ b/inspector/rt.go @@ -0,0 +1,42 @@ +package inspector + +import ( + log "github.com/sirupsen/logrus" + "strconv" +) + +// ResponseTimeMetrics : Metrics used by ResponseTime +type ResponseTimeMetrics struct { + Seconds float64 +} + +// ResponseTime : Parsing the `web` output for response time +type ResponseTime struct { + fields + // Values of metrics being read + Values ResponseTimeMetrics +} + +// Parse : run custom parsing on output of the command +func (i *ResponseTime) Parse(output string) { + log.Debug("Parsing ouput string in ResponseTime inspector") + strconv, err := strconv.ParseFloat(output, 64) + if err != nil { + log.Fatal(err) + } + values := ResponseTimeMetrics{ + Seconds: strconv, + } + i.Values = values +} + +// NewResponseTime : Initialize a new ResponseTime instance +func NewResponseTime() *ResponseTime { + return &ResponseTime{ + fields: fields{ + Type: Command, + Command: `response`, + }, + } + +} diff --git a/integration/integration_test.go b/integration/integration_test.go index 563f3df..1425712 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -32,3 +32,17 @@ func TestMemInfoonSSH(t *testing.T) { } fmt.Printf(`%#v`, i.Values) } + +func TestResponseTimeonWeb(t *testing.T) { + d := driver.NewWebForTest() + i := inspector.NewResponseTime() + output, err := d.RunCommand(i.String()) + if err != nil { + t.Error(err) + } + i.Parse(output) + if i.Values.Seconds == 0 { + t.Error("showing response time as 0") + } + fmt.Printf(`%#v`, i.Values) +}