Skip to content

Commit

Permalink
refactor: split fast.Run()
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Apr 4, 2021
1 parent 712de31 commit 8d77bf9
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions internal/fast/fast.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import (
"github.com/chromedp/cdproto/emulation"
)

func Run() {
type Fast struct {
Up string
Down string
UpUnit string
DownUnit string
}

func Measure() (*Fast, error) {
ctx, cancel := chromedp.NewContext(
context.Background(),
chromedp.WithLogf(log.Printf),
Expand All @@ -20,34 +27,33 @@ func Run() {
ctx, cancel = context.WithTimeout(ctx, 60*time.Second)
defer cancel()

start := time.Now()

var (
up string
dw string
upunit string
dwunit string
)

err := chromedp.Run(ctx,
fast := new(Fast)
err := chromedp.Run(ctx,
emulation.SetUserAgentOverride(`chromedp/chromedp v0.6.10`),
chromedp.Navigate(`https://fast.com`),
chromedp.ScrollIntoView(`footer`),
chromedp.WaitVisible(`#speed-value.succeeded`),
chromedp.Text(`#speed-value.succeeded`, &dw, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.Text(`#speed-units.succeeded`, &dwunit, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.Text(`#speed-value.succeeded`, &fast.Down, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.Text(`#speed-units.succeeded`, &fast.DownUnit, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.Click(`#show-more-details-link`),
chromedp.WaitVisible(`#upload-value.succeeded`),
chromedp.Text(`#upload-value.succeeded`, &up, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.Text(`#upload-units.succeeded`, &upunit, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.Text(`#upload-value.succeeded`, &fast.Up, chromedp.NodeVisible, chromedp.ByQuery),
chromedp.Text(`#upload-units.succeeded`, &fast.UpUnit, chromedp.NodeVisible, chromedp.ByQuery),
)

return fast, err
}

func Run() {
start := time.Now()

fast, err := Measure()
if err != nil {
log.Fatal(err)
}

fmt.Printf("\033[36mdownload speed:\033[m \033[32m%s\033[m %s\n", dw, dwunit)
fmt.Printf("\033[36mupload speed:\033[m \033[31m%s\033[m %s\n", up, upunit)
fmt.Printf("\033[36mdownload speed:\033[m \033[32m%s\033[m %s\n", fast.Down, fast.DownUnit)
fmt.Printf("\033[36mupload speed:\033[m \033[31m%s\033[m %s\n", fast.Up, fast.UpUnit)
fmt.Printf("\n")
fmt.Printf("\033[36m> took: \033[33m%f\033[m secs\n", time.Since(start).Seconds())
}

0 comments on commit 8d77bf9

Please sign in to comment.