Skip to content

Commit

Permalink
chore: refactor benchmark, server
Browse files Browse the repository at this point in the history
  • Loading branch information
soulteary committed Nov 17, 2022
1 parent db9b254 commit 7f3ad1f
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"syscall"
"time"

"github.com/soulteary/apt-proxy/internal/server"
"github.com/soulteary/apt-proxy/pkg/httpcache"
"github.com/soulteary/apt-proxy/pkg/httplog"
"github.com/soulteary/apt-proxy/server"
)

type AppFlags struct {
Expand Down Expand Up @@ -96,5 +96,5 @@ func Daemon(appFlags *AppFlags) {

initLogger(*appFlags, ap)

StartServer(&*appFlags, ap)
StartServer(appFlags, ap)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mirrors
package benchmarks

import (
"errors"
Expand All @@ -8,7 +8,13 @@ import (
"time"
)

func benchmark(base string, query string, times int) (time.Duration, error) {
const (
BENCHMARK_MAX_TIMEOUT = 15 // 15 seconds, detect resource timeout
BENCHMARK_MAX_TRIES = 3 // times, maximum number of attempts
BENCHMARK_DETECT_TIMEOUT = 3 // 3 seconds, for select fast mirror
)

func Benchmark(base string, query string, times int) (time.Duration, error) {
var sum int64

timeout := time.Duration(BENCHMARK_MAX_TIMEOUT * time.Second)
Expand Down Expand Up @@ -46,7 +52,7 @@ func GetTheFastestMirror(mirrors []string, testUrl string) (string, error) {
// kick off all benchmarks in parallel
for _, url := range mirrors {
go func(u string) {
duration, err := benchmark(u, testUrl, BENCHMARK_MAX_TRIES)
duration, err := Benchmark(u, testUrl, BENCHMARK_MAX_TRIES)
if err == nil {
ch <- benchmarkResult{u, duration}
}
Expand Down
27 changes: 27 additions & 0 deletions internal/benchmark/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package benchmarks_test

import (
"log"
"testing"

Benchmark "github.com/soulteary/apt-proxy/internal/benchmark"
Mirrors "github.com/soulteary/apt-proxy/internal/mirrors"
)

func TestResourceBenchmark(t *testing.T) {
const resourcePath = ""
_, err := Benchmark.Benchmark(Mirrors.UBUNTU_GEO_MIRROR_API, resourcePath, Benchmark.BENCHMARK_MAX_TRIES)
if err != nil {
t.Fatal(err)
}
}

func TestMirrorsBenchmark(t *testing.T) {
mirrors := Mirrors.GetGeoMirrorUrlsByMode(Mirrors.TYPE_LINUX_DISTROS_UBUNTU)
mirror, err := Benchmark.GetTheFastestMirror(mirrors, Mirrors.UBUNTU_BENCHMAKR_URL)
if err != nil {
t.Fatal(err)
}

log.Printf("Fastest mirror is %s", mirror)
}
24 changes: 0 additions & 24 deletions internal/mirrors/benchmark_test.go

This file was deleted.

6 changes: 0 additions & 6 deletions internal/mirrors/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ type Rule struct {
Rewrite bool
}

const (
BENCHMARK_MAX_TIMEOUT = 15 // 15 seconds, detect resource timeout
BENCHMARK_MAX_TRIES = 3 // times, maximum number of attempts
BENCHMARK_DETECT_TIMEOUT = 3 // 3 seconds, for select fast mirror
)

// DEBIAN
const (
DEBIAN_BENCHMAKR_URL = "dists/bullseye/main/binary-amd64/Release"
Expand Down
5 changes: 3 additions & 2 deletions internal/rewriter/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/url"
"regexp"

Benchmark "github.com/soulteary/apt-proxy/internal/benchmark"
Mirrors "github.com/soulteary/apt-proxy/internal/mirrors"
"github.com/soulteary/apt-proxy/state"
)
Expand Down Expand Up @@ -46,7 +47,7 @@ func getRewriterForDebian() *URLRewriter {
}

mirrors := Mirrors.GetGeoMirrorUrlsByMode(Mirrors.TYPE_LINUX_DISTROS_DEBIAN)
fastest, err := Mirrors.GetTheFastestMirror(mirrors, benchmarkUrl)
fastest, err := Benchmark.GetTheFastestMirror(mirrors, benchmarkUrl)
if err != nil {
log.Println("Error finding fastest mirror", err)
}
Expand All @@ -72,7 +73,7 @@ func getRewriterForUbuntu() *URLRewriter {
}

mirrors := Mirrors.GetGeoMirrorUrlsByMode(Mirrors.TYPE_LINUX_DISTROS_UBUNTU)
fastest, err := Mirrors.GetTheFastestMirror(mirrors, benchmarkUrl)
fastest, err := Benchmark.GetTheFastestMirror(mirrors, benchmarkUrl)
if err != nil {
log.Println("Error finding fastest mirror", err)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/rewriter/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

Benchmark "github.com/soulteary/apt-proxy/internal/benchmark"
Mirrors "github.com/soulteary/apt-proxy/internal/mirrors"
"github.com/soulteary/apt-proxy/state"
)
Expand Down Expand Up @@ -42,7 +43,7 @@ func TestGetRewriterForUbuntu(t *testing.T) {

func TestCreateNewRewritersForUbuntu(t *testing.T) {
ap := *CreateNewRewriters(Mirrors.TYPE_LINUX_DISTROS_UBUNTU)
time.Sleep((Mirrors.BENCHMARK_DETECT_TIMEOUT / 2) * time.Second)
time.Sleep((Benchmark.BENCHMARK_DETECT_TIMEOUT / 2) * time.Second)

if len(ap.ubuntu.mirror.Path) == 0 {
t.Fatal("No mirrors found")
Expand All @@ -55,7 +56,7 @@ func TestCreateNewRewritersForUbuntu(t *testing.T) {

func TestCreateNewRewritersForDebian(t *testing.T) {
ap := *CreateNewRewriters(Mirrors.TYPE_LINUX_DISTROS_DEBIAN)
time.Sleep((Mirrors.BENCHMARK_DETECT_TIMEOUT / 2) * time.Second)
time.Sleep((Benchmark.BENCHMARK_DETECT_TIMEOUT / 2) * time.Second)

if len(ap.debian.mirror.Path) == 0 {
t.Fatal("No mirrors found")
Expand All @@ -68,7 +69,7 @@ func TestCreateNewRewritersForDebian(t *testing.T) {

func TestCreateNewRewritersForAll(t *testing.T) {
ap := *CreateNewRewriters(Mirrors.TYPE_LINUX_ALL_DISTROS)
time.Sleep((Mirrors.BENCHMARK_DETECT_TIMEOUT / 2) * time.Second)
time.Sleep((Benchmark.BENCHMARK_DETECT_TIMEOUT / 2) * time.Second)

if len(ap.debian.mirror.Path) == 0 || len(ap.ubuntu.mirror.Host) == 0 {
t.Fatal("No mirrors found")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 7f3ad1f

Please sign in to comment.