github.com/aavshr/aws-sdk-go@v1.41.3/awstesting/integration/performance/s3GetObject/logger.go (about)

     1  //go:build integration && perftest
     2  // +build integration,perftest
     3  
     4  package main
     5  
     6  import (
     7  	"encoding/csv"
     8  	"fmt"
     9  	"io"
    10  	"os"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  )
    15  
    16  type Logger struct {
    17  	out *csv.Writer
    18  }
    19  
    20  func NewLogger(writer io.Writer) *Logger {
    21  	l := &Logger{
    22  		out: csv.NewWriter(writer),
    23  	}
    24  
    25  	err := l.out.Write([]string{
    26  		"ID", "Attempt",
    27  		"Latency",
    28  		"DNSStart", "DNSDone", "DNSDur",
    29  		"ConnectStart", "ConnectDone", "ConnectDur",
    30  		"TLSStart", "TLSDone", "TLSDur",
    31  		"WriteReq", "RespFirstByte", "WaitRespFirstByte",
    32  		"Error",
    33  	})
    34  	if err != nil {
    35  		fmt.Fprintf(os.Stderr, "failed to write header trace, %v\n", err)
    36  	}
    37  
    38  	return l
    39  }
    40  
    41  func (l *Logger) RecordTrace(trace *RequestTrace) {
    42  	req := RequestReport{
    43  		ID:           trace.ID,
    44  		TotalLatency: durToMSString(trace.TotalLatency()),
    45  		Retries:      trace.Retries(),
    46  	}
    47  
    48  	for i, a := range trace.Attempts() {
    49  		attempt := AttemptReport{
    50  			Reused:     a.Reused,
    51  			SDKMarshal: durToMSString(a.SendStart.Sub(a.Start)),
    52  			ReqWritten: durToMSString(a.RequestWritten.Sub(a.SendStart)),
    53  			Latency:    durToMSString(a.Finish.Sub(a.Start)),
    54  			Err:        a.Err,
    55  		}
    56  
    57  		if !a.FirstResponseByte.IsZero() {
    58  			attempt.RespFirstByte = durToMSString(a.FirstResponseByte.Sub(a.SendStart))
    59  			attempt.WaitRespFirstByte = durToMSString(a.FirstResponseByte.Sub(a.RequestWritten))
    60  		}
    61  
    62  		if !a.Reused {
    63  			attempt.DNSStart = durToMSString(a.DNSStart.Sub(a.SendStart))
    64  			attempt.DNSDone = durToMSString(a.DNSDone.Sub(a.SendStart))
    65  			attempt.DNS = durToMSString(a.DNSDone.Sub(a.DNSStart))
    66  
    67  			attempt.ConnectStart = durToMSString(a.ConnectStart.Sub(a.SendStart))
    68  			attempt.ConnectDone = durToMSString(a.ConnectDone.Sub(a.SendStart))
    69  			attempt.Connect = durToMSString(a.ConnectDone.Sub(a.ConnectStart))
    70  
    71  			attempt.TLSHandshakeStart = durToMSString(a.TLSHandshakeStart.Sub(a.SendStart))
    72  			attempt.TLSHandshakeDone = durToMSString(a.TLSHandshakeDone.Sub(a.SendStart))
    73  			attempt.TLSHandshake = durToMSString(a.TLSHandshakeDone.Sub(a.TLSHandshakeStart))
    74  		}
    75  
    76  		req.Attempts = append(req.Attempts, attempt)
    77  
    78  		var reqErr string
    79  		if attempt.Err != nil {
    80  			reqErr = strings.Replace(attempt.Err.Error(), "\n", `\n`, -1)
    81  		}
    82  		err := l.out.Write([]string{
    83  			strconv.Itoa(int(req.ID)),
    84  			strconv.Itoa(i + 1),
    85  			attempt.Latency,
    86  			attempt.DNSStart, attempt.DNSDone, attempt.DNS,
    87  			attempt.ConnectStart, attempt.ConnectDone, attempt.Connect,
    88  			attempt.TLSHandshakeStart, attempt.TLSHandshakeDone, attempt.TLSHandshake,
    89  			attempt.ReqWritten,
    90  			attempt.RespFirstByte,
    91  			attempt.WaitRespFirstByte,
    92  			reqErr,
    93  		})
    94  		if err != nil {
    95  			fmt.Fprintf(os.Stderr, "failed to write request trace, %v\n", err)
    96  		}
    97  		l.out.Flush()
    98  	}
    99  }
   100  
   101  func durToMSString(v time.Duration) string {
   102  	ms := float64(v) / float64(time.Millisecond)
   103  	return fmt.Sprintf("%0.6f", ms)
   104  }
   105  
   106  type RequestReport struct {
   107  	ID           int64
   108  	TotalLatency string
   109  	Retries      int
   110  
   111  	Attempts []AttemptReport
   112  }
   113  type AttemptReport struct {
   114  	Reused bool
   115  	Err    error
   116  
   117  	SDKMarshal string `json:",omitempty"`
   118  
   119  	DNSStart string `json:",omitempty"`
   120  	DNSDone  string `json:",omitempty"`
   121  	DNS      string `json:",omitempty"`
   122  
   123  	ConnectStart string `json:",omitempty"`
   124  	ConnectDone  string `json:",omitempty"`
   125  	Connect      string `json:",omitempty"`
   126  
   127  	TLSHandshakeStart string `json:",omitempty"`
   128  	TLSHandshakeDone  string `json:",omitempty"`
   129  	TLSHandshake      string `json:",omitempty"`
   130  
   131  	ReqWritten        string `json:",omitempty"`
   132  	RespFirstByte     string `json:",omitempty"`
   133  	WaitRespFirstByte string `json:",omitempty"`
   134  	Latency           string `json:",omitempty"`
   135  }