Skip to content

Commit

Permalink
Merge pull request #6 from JoseThen/cover-latency
Browse files Browse the repository at this point in the history
Show latency measurements for when doing requests
  • Loading branch information
JoseThen authored Jan 3, 2022
2 parents c4b007d + 67d7476 commit 226109e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
16 changes: 6 additions & 10 deletions cmd/exam.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cmd

import (
"fmt"
"net/http"
"os"
"text/tabwriter"
"time"

utils "github.com/JoseThen/checkup/utils"
"github.com/spf13/cobra"
Expand All @@ -25,13 +23,10 @@ var examCmd = &cobra.Command{
auth, _ := cmd.Flags().GetBool("auth")
exam := utils.ReadExam(file)
exitCode := 0
// Setup http Client
var httpClient = &http.Client{
Timeout: time.Second * 10,
}

w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t", "Endpoint", "Code", "Result", "Pass")
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t", "--------", "----", "------", "----")
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t%s\t", "Endpoint", "Code", "Result", "Latency", "Pass")
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t%s\t", "--------", "----", "------", "-------", "----")
for _, test := range exam.Tests {
for _, path := range test.Paths {
// Setup check Request with above variables
Expand All @@ -42,8 +37,9 @@ var examCmd = &cobra.Command{
Auth: auth,
}
checkup := utils.Checkup(*checkRequest)
fmt.Fprintf(w, "\n %s\t%d\t%d\t%v\t", checkup.Endpoint, checkup.Code, checkup.Result, checkup.Pass)
if checkup.Pass == false {
httpClient.CloseIdleConnections()
fmt.Fprintf(w, "\n %s\t%d\t%d\t%dms\t%v\t", checkup.Endpoint, checkup.Code, checkup.Result, checkup.Lantency.Milliseconds(), checkup.Pass)
if !checkup.Pass {
exitCode = 3
}
}
Expand Down
15 changes: 5 additions & 10 deletions cmd/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cmd

import (
"fmt"
"net/http"
"os"
"text/tabwriter"
"time"

utils "github.com/JoseThen/checkup/utils"
"github.com/spf13/cobra"
Expand All @@ -27,23 +25,20 @@ var listenCmd = &cobra.Command{
code, _ := cmd.Flags().GetInt("code")
auth, _ := cmd.Flags().GetBool("auth")
endpoint, _ := cmd.Flags().GetString("endpoint")
// Setting up http Client
var httpClient = &http.Client{
Timeout: time.Second * 10,
}

// Setup check Request with above variables
checkRequest := &utils.CheckupRequest{
Client: httpClient,
Code: code,
Endpoint: endpoint,
Auth: auth,
}
// checkup := utils.Checkup(httpClient, code, endpoint, auth)
checkup := utils.Checkup(*checkRequest)
httpClient.CloseIdleConnections()
w := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t", "Endpoint", "Code", "Result", "Pass")
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t", "--------", "----", "------", "----")
fmt.Fprintf(w, "\n %s\t%d\t%d\t%v\t", checkup.Endpoint, checkup.Code, checkup.Result, checkup.Pass)
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t%s\t", "Endpoint", "Code", "Result", "Latency", "Pass")
fmt.Fprintf(w, "\n %s\t%s\t%s\t%s\t%s\t", "--------", "----", "------", "-------", "----")
fmt.Fprintf(w, "\n %s\t%d\t%d\t%dms\t%v\t", checkup.Endpoint, checkup.Code, checkup.Result, checkup.Lantency.Milliseconds(), checkup.Pass)
w.Flush()
fmt.Println()
if checkup.Pass {
Expand Down
7 changes: 7 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"fmt"
"net/http"
"os"
"time"

"github.com/spf13/cobra"

Expand All @@ -22,6 +24,11 @@ var rootCmd = &cobra.Command{
// Run: func(cmd *cobra.Command, args []string) { },
}

// Setup http Client
var httpClient = &http.Client{
Timeout: time.Second * 10,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
Expand Down
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: build run compile build-linux build-windows build-darwin test

VERSION ?= 0.4.0
VERSION ?= 0.5.0
NAME ?= "checkup"

test:
Expand Down
2 changes: 1 addition & 1 deletion utils/ReadExam.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func ReadExam(file string) ExamFile {
examFile, err := ioutil.ReadFile(file)
ErrorCheck(err)
extension := filepath.Ext(strings.TrimSpace(file))
err = fmt.Errorf("Wrong Extension: %v", extension)
err = fmt.Errorf("wrong Extension: %v", extension)
if extension == ".yaml" || extension == ".yml" {
err = yaml.Unmarshal(examFile, &exam)
}
Expand Down
6 changes: 5 additions & 1 deletion utils/checkup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"net/http"
"os"
"time"
)

// CheckupRequest ... Struct holding all data needed for a checkup
Expand All @@ -19,6 +20,7 @@ type CheckupResults struct {
Endpoint string
Code int
Result int
Lantency time.Duration
Pass bool
}

Expand All @@ -37,16 +39,18 @@ func Checkup(healthForm CheckupRequest) CheckupResults {
healthForm.Client.CheckRedirect = addAuthOnRedirect
}

start := time.Now()
resp, err := healthForm.Client.Do(request)
if err != nil {
ErrorCheck(err)
}
end := time.Since(start)
defer resp.Body.Close()

results := CheckupResults{
Endpoint: healthForm.Endpoint,
Code: healthForm.Code,
Result: resp.StatusCode,
Lantency: end,
Pass: resp.StatusCode == healthForm.Code,
}

Expand Down

0 comments on commit 226109e

Please sign in to comment.