Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support fallback browser paths with brave #6

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 52 additions & 26 deletions internal/fast/fast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@ package fast

import (
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"runtime"
"strings"
"time"

"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/chromedp"
)

// Browsers is list of non default browsers by OS
var Browsers = map[string][]string{
"windows": {"", `C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe`},
"darwin": {"", `/Applications/Brave Browser.app/Contents/MacOS/BraveBrowser`},
"*": {"", `brave`, `brave-browser`},
}

// NoColor tells if color should be used
var NoColor = os.Getenv("NO_COLOR") != "" || os.Getenv("TERM") == "dumb"

// Fast represents measurement structure
type Fast struct {
Up string
Expand All @@ -22,18 +36,9 @@ type Fast struct {
// Measure does the main job.
// It returns *Fast and error
func Measure(noUp bool) (*Fast, error) {
ctx, cancel := chromedp.NewContext(
context.Background(),
chromedp.WithLogf(log.Printf),
)
defer cancel()

ctx, cancel = context.WithTimeout(ctx, 180*time.Second)
defer cancel()

fast := new(Fast)
cmds := []chromedp.Action{
emulation.SetUserAgentOverride(`chromedp/chromedp v0.6.10`),
emulation.SetUserAgentOverride(`chromedp/chromedp v0.8.6`),
chromedp.Navigate(`https://fast.com`),
chromedp.ScrollIntoView(`footer`),
chromedp.WaitVisible(`#speed-value.succeeded`),
Expand All @@ -49,11 +54,38 @@ func Measure(noUp bool) (*Fast, error) {
)
}

err := chromedp.Run(ctx, cmds...)
browsers := Browsers["*"]
if paths, ok := Browsers[runtime.GOOS]; ok {
browsers = paths
}

var err error
for _, browser := range browsers {
err = doMeasure(browser, fast, cmds...)
if err == nil || !errors.Is(err, exec.ErrNotFound) {
break
}
}

return fast, err
}

func doMeasure(browser string, fast *Fast, cmds ...chromedp.Action) error {
opts := chromedp.DefaultExecAllocatorOptions[:]
if browser != "" {
opts = append(opts, chromedp.ExecPath(browser))
}

allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel := chromedp.NewContext(allocCtx, chromedp.WithLogf(log.Printf))
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 180*time.Second)
defer cancel()

return chromedp.Run(ctx, cmds...)
}

// Run is the ready to use API.
// For customization call Measure().
func Run(noUp bool) {
Expand All @@ -71,22 +103,16 @@ func Run(noUp bool) {
func Out(fast *Fast, start time.Time) {
hasUp := fast.Up != "" && fast.UpUnit != ""

// No color for windows
if os.PathSeparator == '\\' {
fmt.Printf("download speed: %s %s\n", fast.Down, fast.DownUnit)
if hasUp {
fmt.Printf("upload speed: %s %s\n", fast.Up, fast.UpUnit)
}
fmt.Printf("\n")
fmt.Printf("> took: %f secs\n", time.Since(start).Seconds())

return
out := fmt.Sprintf("\033[36mdownload speed:\033[m \033[32m%s\033[m %s\n", fast.Down, fast.DownUnit)
if hasUp {
out += fmt.Sprintf("\033[36mupload speed: \033[m \033[31m%s\033[m %s\n", fast.Up, fast.UpUnit)
}
out += fmt.Sprintf("\n\033[36m> took: \033[33m%f\033[m secs\n", time.Since(start).Seconds())

fmt.Printf("\033[36mdownload speed:\033[m \033[32m%s\033[m %s\n", fast.Down, fast.DownUnit)
if hasUp {
fmt.Printf("\033[36mupload speed:\033[m \033[31m%s\033[m %s\n", fast.Up, fast.UpUnit)
if NoColor {
out = strings.ReplaceAll(strings.ReplaceAll(out, "\033[36m", ""), "\033[m", "")
out = strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(out, "\033[33m", ""), "\033[32m", ""), "\033[31m", "")
}
fmt.Printf("\n")
fmt.Printf("\033[36m> took: \033[33m%f\033[m secs\n", time.Since(start).Seconds())

fmt.Print(out)
}