Commit 95003340 authored by Daniel Wolf's avatar Daniel Wolf
Browse files

Use the best case for the latency in the navigation drawer

Closes #256
parent 6bb80362
......@@ -104,7 +104,7 @@ class MainActivity : NavigationDrawerActivity() {
view.dns2.text = secondaryAddress
}
val latency = DnsSpeedTest(server, log = {}).runTest(1)
val latency = DnsSpeedTest(server, log = {}).runTest(3, DnsSpeedTest.Strategy.BEST_CASE)
runOnUiThread {
view.latency.text = if (latency != null && latency > 0) {
"$latency ms"
......
......@@ -95,22 +95,28 @@ class DnsSpeedTest(val server: DnsServerInformation<*>,
* @param passes The amount of requests to make
* @return The average response time (in ms)
*/
fun runTest(@IntRange(from = 1) passes: Int): Int? {
var ttl = 0
fun runTest(@IntRange(from = 1) passes: Int, strategy: Strategy = Strategy.AVERAGE): Int? {
val TTLs = mutableListOf<Int>()
for (i in 0 until passes) {
if (server is HttpsDnsServerInformation) {
server.serverConfigurations.values.forEach {
ttl += testHttps(it) ?: 0
TTLs += testHttps(it) ?: 0
}
} else {
(server as DnsServerInformation<TLSUpstreamAddress>).servers.forEach {
ttl += testTls(it.address) ?: 0
TTLs += testTls(it.address) ?: 0
}
}
}
return (ttl / passes).let {
if (it <= 0) null else it
return if(strategy == Strategy.BEST_CASE) {
TTLs.minByOrNull {
it
}
} else {
TTLs.sum().let {
if(it <= 0) null else it
}
}
}
......@@ -253,4 +259,8 @@ class DnsSpeedTest(val server: DnsServerInformation<*>,
return res
}
}
enum class Strategy {
AVERAGE, BEST_CASE
}
}
\ 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