-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added simple response time inspector for web driver
- Loading branch information
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
}, | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters