Commit 8f38329a authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Use a weighted average for the navigation drawer latency (#260)

parent d665464e
......@@ -104,7 +104,7 @@ class MainActivity : NavigationDrawerActivity() {
view.dns2.text = secondaryAddress
}
val latency = DnsSpeedTest(server, log = {}).runTest(3, DnsSpeedTest.Strategy.BEST_CASE)
val latency = DnsSpeedTest(server, log = {}).runTest(3, DnsSpeedTest.Strategy.WEIGHTED_AVERAGE)
runOnUiThread {
view.latency.text = if (latency != null && latency > 0) {
"$latency ms"
......
......@@ -98,7 +98,7 @@ class DnsSpeedTest(val server: DnsServerInformation<*>,
fun runTest(@IntRange(from = 1) passes: Int, strategy: Strategy = Strategy.AVERAGE): Int? {
val latencies = mutableListOf<Int>()
var firstPass = strategy == Strategy.AVERAGE
var firstPass = strategy != Strategy.BEST_CASE
for (i in 0 until passes) {
if (server is HttpsDnsServerInformation) {
server.serverConfigurations.values.forEach {
......@@ -117,9 +117,18 @@ class DnsSpeedTest(val server: DnsServerInformation<*>,
latencies.minByOrNull {
it
}
} else {
} else if(strategy == Strategy.AVERAGE){
latencies.sum().let {
if(it <= 0) null else it
}?.div(passes)
} else {
var pos = 0
latencies.sumBy {
// Weight first responses less (min 80%)
val weight = maxOf(100, minOf(80, 100-(passes - pos++)*5))
(it*weight)/100
}.let {
if(it <= 0) null else it
}
}
}
......@@ -265,6 +274,6 @@ class DnsSpeedTest(val server: DnsServerInformation<*>,
}
enum class Strategy {
AVERAGE, BEST_CASE
AVERAGE, BEST_CASE, WEIGHTED_AVERAGE
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment