Skip to content

Commit

Permalink
Revert "fix: remove RTT tracking from client/transport in favor of di…
Browse files Browse the repository at this point in the history
…rect tra…"
  • Loading branch information
fzipi authored Sep 27, 2022
1 parent fcf6dfd commit cf181f5
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
16 changes: 16 additions & 0 deletions ftwhttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (c *Client) NewConnection(d Destination) error {
connection: netConn,
protocol: d.Protocol,
readTimeout: c.config.ReadTimeout,
duration: NewRoundTripTime(),
}
}

Expand All @@ -85,3 +86,18 @@ func (c *Client) Do(req Request) (*Response, error) {

return response, err
}

// GetRoundTripTime returns the time taken from the initial send till receiving the full response
func (c *Client) GetRoundTripTime() *RoundTripTime {
return c.Transport.GetTrackedTime()
}

// StartTrackingTime sets the timer to start transactions. This will be the starting time in logs.
func (c *Client) StartTrackingTime() {
c.Transport.StartTrackingTime()
}

// StopTrackingTime stops the timer. When looking at logs, we will read up to this one.
func (c *Client) StopTrackingTime() {
c.Transport.StopTrackingTime()
}
14 changes: 14 additions & 0 deletions ftwhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,25 @@ func TestGetTrackedTime(t *testing.T) {
t.Logf("This should not error")
}

c.StartTrackingTime()

resp, err := c.Do(*req)

c.StopTrackingTime()

if err != nil {
t.Logf("This should not error")
}

if resp.Parsed.StatusCode != 200 {
t.Logf("Error in calling website")
}

rtt := c.GetRoundTripTime()

if rtt.RoundTripDuration() < 0 {
t.Logf("Error getting RTT")
}
}

func TestClientMultipartFormDataRequest(t *testing.T) {
Expand Down Expand Up @@ -125,8 +135,12 @@ Some-file-test-here
t.Logf("This should not error")
}

c.StartTrackingTime()

resp, err := c.Do(*req)

c.StopTrackingTime()

if err != nil {
t.Logf("This should not error")
}
Expand Down
15 changes: 15 additions & 0 deletions ftwhttp/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ func DestinationFromString(urlString string) (*Destination, error) {
return d, nil
}

// StartTrackingTime initializes timer
func (c *Connection) StartTrackingTime() {
c.duration.StartTracking()
}

// StopTrackingTime stops timer
func (c *Connection) StopTrackingTime() {
c.duration.StopTracking()
}

// GetTrackedTime will return the time since the request started and the response was parsed
func (c *Connection) GetTrackedTime() *RoundTripTime {
return c.duration
}

func (c *Connection) send(data []byte) (int, error) {
var err error
var sent int
Expand Down
39 changes: 39 additions & 0 deletions ftwhttp/rtt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ftwhttp

import "time"

// NewRoundTripTime initializes a roundtriptime struct
func NewRoundTripTime() *RoundTripTime {
rtt := &RoundTripTime{
begin: time.Now(),
end: time.Now(),
}

return rtt
}

// StartTracking sets the initial time to Now
func (rtt *RoundTripTime) StartTracking() {
rtt.begin = time.Now()
}

// StopTracking sets the finish time to Now
func (rtt *RoundTripTime) StopTracking() {
now := time.Now()
rtt.end = now.Add(50 * time.Millisecond)
}

// StartTime returns the time when this round trip started
func (rtt *RoundTripTime) StartTime() time.Time {
return rtt.begin
}

// StopTime returns the time when this round trip was stopped
func (rtt *RoundTripTime) StopTime() time.Time {
return rtt.end
}

// RoundTripDuration gives the total time spent in this roundtrip
func (rtt *RoundTripTime) RoundTripDuration() time.Duration {
return rtt.end.Sub(rtt.begin)
}
1 change: 1 addition & 0 deletions ftwhttp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Connection struct {
connection net.Conn
protocol string
readTimeout time.Duration
duration *RoundTripTime
}

// RoundTripTime abstracts the time a transaction takes
Expand Down
5 changes: 3 additions & 2 deletions runner/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,11 @@ func RunStage(runContext *TestRunContext, ftwCheck *check.FTWCheck, testCase tes
if err != nil && !expectedOutput.ExpectError {
log.Fatal().Caller().Err(err).Msgf("can't connect to destination %+v", dest)
}
runContext.Client.StartTrackingTime()

reqStartTime := time.Now()
response, responseErr := runContext.Client.Do(*req)
roundTripTime := time.Since(reqStartTime)

runContext.Client.StopTrackingTime()
if responseErr != nil && !expectedOutput.ExpectError {
log.Fatal().Caller().Err(responseErr).Msgf("failed sending request to destination %+v", dest)
}
Expand All @@ -159,6 +159,7 @@ func RunStage(runContext *TestRunContext, ftwCheck *check.FTWCheck, testCase tes
// now get the test result based on output
testResult := checkResult(ftwCheck, response, responseErr)

roundTripTime := runContext.Client.GetRoundTripTime().RoundTripDuration()
stageTime := time.Since(stageStartTime)

addResultToStats(testResult, testCase.TestTitle, &runContext.Stats)
Expand Down

0 comments on commit cf181f5

Please sign in to comment.