Skip to content

Commit

Permalink
Print connection duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 13, 2022
1 parent 65e7761 commit 5bc8967
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
7 changes: 4 additions & 3 deletions core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
"os/exec"
"runtime"
"strconv"

"github.com/jedib0t/go-pretty/v6/table"
)
Expand Down Expand Up @@ -33,9 +34,9 @@ func (p *Printer) ToTable(results *[]TestResult) {
t := table.NewWriter()
t.SetStyle(table.StyleColoredBright)
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Address", "Status"})
for i, r := range *results {
t.AppendRow(table.Row{i, r.url.Host, r.status})
t.AppendHeader(table.Row{"Address", "Connection", "Status"})
for _, r := range *results {
t.AppendRow(table.Row{r.url.Host, strconv.FormatInt(r.duration.Milliseconds(), 10) + "ms", r.status})
}

t.AppendSeparator()
Expand Down
23 changes: 16 additions & 7 deletions core/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package core

import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"regexp"
"time"
)

Expand Down Expand Up @@ -34,18 +35,26 @@ func GetStringFromURL(url string) (string, error) {
}

type TestResult struct {
url url.URL
status string
url url.URL
status string
duration time.Duration
}

func Test(url *url.URL, timeoutSeconds int, out chan TestResult) {
timeOut := time.Duration(timeoutSeconds) * time.Second
_, err := net.DialTimeout(url.Scheme, url.Host, timeOut)
timeout := time.Duration(timeoutSeconds) * time.Second
tp := NewTransport(timeout)

_, err := tp.Dial(url.Scheme, url.Host)

if err != nil {
out <- TestResult{url: *url, status: err.Error()}
out <- TestResult{url: *url, status: formatError(err, url), duration: tp.ConnDuration()}
return
}

out <- TestResult{url: *url, status: "OK"}
out <- TestResult{url: *url, status: "OK", duration: tp.Duration()}
}

func formatError(err error, url *url.URL) string {
m := regexp.MustCompile(fmt.Sprintf(`(net/http: )| \(.*\)|(dial .* %s: )`, url.Host))
return m.ReplaceAllString(err.Error(), "")
}

0 comments on commit 5bc8967

Please sign in to comment.