Skip to content

Commit

Permalink
added simple response time inspector for web driver
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Oct 26, 2021
1 parent 2f77db9 commit 4e9b263
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
78 changes: 78 additions & 0 deletions driver/web.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
42 changes: 42 additions & 0 deletions inspector/rt.go
Original file line number Diff line number Diff line change
@@ -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`,
},
}

}
14 changes: 14 additions & 0 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 4e9b263

Please sign in to comment.