82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package app
|
|
|
|
import (
|
|
dnscfgpkg "selective-vpn-api/app/dnscfg"
|
|
"time"
|
|
)
|
|
|
|
var dnsBenchmarkDefaultDomains = append([]string(nil), dnscfgpkg.BenchmarkDefaultDomains...)
|
|
|
|
const (
|
|
dnsBenchmarkProfileQuick = "quick"
|
|
dnsBenchmarkProfileLoad = "load"
|
|
)
|
|
|
|
type dnsBenchmarkOptions = dnscfgpkg.BenchmarkOptions
|
|
|
|
func normalizeBenchmarkUpstreams(in []DNSBenchmarkUpstream) []string {
|
|
if len(in) == 0 {
|
|
return nil
|
|
}
|
|
raw := make([]string, 0, len(in))
|
|
for _, item := range in {
|
|
raw = append(raw, item.Addr)
|
|
}
|
|
return dnscfgpkg.NormalizeBenchmarkUpstreamStrings(raw, normalizeDNSUpstream)
|
|
}
|
|
|
|
func benchmarkDNSUpstream(upstream string, domains []string, timeout time.Duration, attempts int, opts dnsBenchmarkOptions) DNSBenchmarkResult {
|
|
classify := func(err error) string {
|
|
switch classifyDNSError(err) {
|
|
case dnsErrorNXDomain:
|
|
return dnscfgpkg.BenchmarkErrorNXDomain
|
|
case dnsErrorTimeout:
|
|
return dnscfgpkg.BenchmarkErrorTimeout
|
|
case dnsErrorTemporary:
|
|
return dnscfgpkg.BenchmarkErrorTemporary
|
|
default:
|
|
return dnscfgpkg.BenchmarkErrorOther
|
|
}
|
|
}
|
|
pkgRes := dnscfgpkg.BenchmarkDNSUpstream(upstream, domains, timeout, attempts, opts, dnsLookupAOnce, classify)
|
|
return DNSBenchmarkResult{
|
|
Upstream: pkgRes.Upstream,
|
|
Attempts: pkgRes.Attempts,
|
|
OK: pkgRes.OK,
|
|
Fail: pkgRes.Fail,
|
|
NXDomain: pkgRes.NXDomain,
|
|
Timeout: pkgRes.Timeout,
|
|
Temporary: pkgRes.Temporary,
|
|
Other: pkgRes.Other,
|
|
AvgMS: pkgRes.AvgMS,
|
|
P95MS: pkgRes.P95MS,
|
|
Score: pkgRes.Score,
|
|
Color: pkgRes.Color,
|
|
}
|
|
}
|
|
|
|
func dnsLookupAOnce(host string, upstream string, timeout time.Duration) ([]string, error) {
|
|
return dnscfgpkg.DNSLookupAOnce(host, upstream, timeout, splitDNS, isPrivateIPv4)
|
|
}
|
|
|
|
func benchmarkTopN(results []DNSBenchmarkResult, n int, fallback []string) []string {
|
|
pkgResults := make([]dnscfgpkg.BenchmarkResult, 0, len(results))
|
|
for _, item := range results {
|
|
pkgResults = append(pkgResults, dnscfgpkg.BenchmarkResult{
|
|
Upstream: item.Upstream,
|
|
Attempts: item.Attempts,
|
|
OK: item.OK,
|
|
Fail: item.Fail,
|
|
NXDomain: item.NXDomain,
|
|
Timeout: item.Timeout,
|
|
Temporary: item.Temporary,
|
|
Other: item.Other,
|
|
AvgMS: item.AvgMS,
|
|
P95MS: item.P95MS,
|
|
Score: item.Score,
|
|
Color: item.Color,
|
|
})
|
|
}
|
|
return dnscfgpkg.BenchmarkTopN(pkgResults, n, fallback)
|
|
}
|