Skip to content

Commit

Permalink
feat(): update parallel connections and duration settings
Browse files Browse the repository at this point in the history
  • Loading branch information
n33pm committed Jun 10, 2024
1 parent 38c36c8 commit 6772659
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion download.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func startDownloadTest(targets []Target) float64 {
totalTime += result.TotalTime
}

downloadSpeed := (float64(totalSize * uint64(time.Second))) / float64(totalTime) * parallelConnections
downloadSpeed := (float64(totalSize * uint64(time.Second))) / float64(totalTime) * float64(parallelConnections)

log.Debug().Dur("totalTime", totalTime).Uint64("totalSize", totalSize).Float64("speed", downloadSpeed/1e3/1e3*8).Msg("DownloadSpeed")

Expand Down
36 changes: 32 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
"net/http"
"os"
"regexp"
"strconv"
"time"
)

const (
downloadDuration = 30 * time.Second
uploadDuration = 30 * time.Second
uploadBufferSize = 26 * 1024 * 1024
parallelConnections = 5
uploadBufferSize = 26 * 1024 * 1024
)

var (
Expand All @@ -23,6 +21,11 @@ var (

commit = ""
version = "dev"

parallelConnections = 5

downloadDuration = 30 * time.Second
uploadDuration = 30 * time.Second
)

type APIResponse struct {
Expand Down Expand Up @@ -63,15 +66,40 @@ func init() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

// read env vars parallelConnections
if os.Getenv("PARALLEL_CONNECTIONS") != "" {
i, err := strconv.Atoi(os.Getenv("PARALLEL_CONNECTIONS"))
if err != nil {
log.Fatal().Err(err).Msg("Error reading PARALLEL_CONNECTIONS")
}
parallelConnections = i
}
if os.Getenv("DOWNLOAD_DURATION_SEC") != "" {
i, err := strconv.Atoi(os.Getenv("DOWNLOAD_DURATION_SEC"))
if err != nil {
log.Fatal().Err(err).Msg("Error reading DOWNLOAD_DURATION_SEC")
}
downloadDuration = time.Duration(i) * time.Second
}
if os.Getenv("UPLOAD_DURATION_SEC") != "" {
i, err := strconv.Atoi(os.Getenv("UPLOAD_DURATION_SEC"))
if err != nil {
log.Fatal().Err(err).Msg("Error reading UPLOAD_DURATION_SEC")
}
uploadDuration = time.Duration(i) * time.Second
}

}

func main() {

log.Info().Str("version", version).Str("commit", commit).Msg("Starting fast-speedtest")
log.Info().Int("parallelConnections", parallelConnections).Float64("downloadDurationSec", downloadDuration.Seconds()).Float64("uploadDurationSec", uploadDuration.Seconds()).Msg("Config")

//startTest()
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
downloadSpeed, uploadSpeed := startTest()
log.Info().Float64("downloadSpeed", downloadSpeed).Float64("uploadSpeed", uploadSpeed).Msg("Speedtest results")
w.Header().Add("content-type", "text/plain")
w.Write([]byte("# TYPE speedtest_bits_per_second gauge\n"))
w.Write([]byte("# HELP speedtest_bits_per_second Speed measured against fast.com\n"))
Expand Down
2 changes: 1 addition & 1 deletion upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func startUploadTest(targets []Target) float64 {
totalTime += result.TotalTime
}

speed := (float64(totalSize * uint64(time.Second))) / float64(totalTime) * parallelConnections
speed := (float64(totalSize * uint64(time.Second))) / float64(totalTime) * float64(parallelConnections)

log.Debug().Dur("totalTime", totalTime).Uint64("totalSize", totalSize).Float64("speed", speed/1e3/1e3*8).Msg("UploadSpeed")

Expand Down

0 comments on commit 6772659

Please sign in to comment.