Skip to content

Commit

Permalink
- update the client code of the timesync and speedtest examples in `/…
Browse files Browse the repository at this point in the history
…examples/1_timesync/` and `/examples/2_speedtest/`, to the new version of the timesync.

- shorten the redundant formatting of the test result's `UncertainValue`s.
- example server and client codes were tested to confirm to be working as before.
  • Loading branch information
omar-azmi committed Sep 4, 2024
1 parent e76cbb8 commit 7cf737d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 43 deletions.
6 changes: 4 additions & 2 deletions examples/1_timesync/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { applyClientPlugin } from "../../src/plugins/timesync.ts"
import { applyClientPlugin, summarizeTimesyncStats } from "../../src/plugins/timesync.ts"
import { Sock } from "../../src/sock.ts"
import { urlPathname } from "./deps.ts"
import { domainName, timesyncStatFormatter } from "./deps_client.ts"
Expand All @@ -20,7 +20,9 @@ const
const
get_server_time = applyClientPlugin(client_sock, "perf"),
run_get_server_time = () => {
get_server_time(20, 5).then((stats) => {
get_server_time(20).then((results) => {
results.splice(0, 5)
const stats = summarizeTimesyncStats(results)
printJsonToWebpage(timesyncStatFormatter(stats))
})
}
Expand Down
31 changes: 18 additions & 13 deletions examples/1_timesync/deps_client.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
/** dependencies of the client code. */

import type { UncertainValue } from "../../src/plugins/deps.ts"
import type { TimesyncStats } from "../../src/plugins/timesync.ts"

const number_formatter = (value: number) => {
return value.toFixed(3)
}
const
number_formatter = (value: number) => {
return value.toFixed(3)
},
time_formatter = (value: number, error: number) => (`${number_formatter(value)} ± ${number_formatter(error)} ms`),
uncertain_stats_formatter = (
stats: Record<string, UncertainValue> | Object,
formatter: (value: UncertainValue["value"], error: UncertainValue["error"]) => string,
) => {
return Object.fromEntries(
Object.entries(stats).map(([entry, uncertain_value]) => {
const { value, error } = uncertain_value
return [entry, formatter(value, error)]
})
)
}

export const
domainName = globalThis.location?.host as string,
timesyncStatFormatter = (stats: TimesyncStats) => {
const
[means, stdevs] = stats,
means_str = means.map(number_formatter),
stdevs_str = stdevs.map(number_formatter)
return {
serverUplinkOffsetTime: `${means_str[0]} ± ${stdevs_str[0]} ms`,
serverDownlinkOffsetTime: `${means_str[1]} ± ${stdevs_str[1]} ms`,
serverProcessTime: `${means_str[2]} ± ${stdevs_str[2]} ms`,
returnTripTime: `${means_str[3]} ± ${stdevs_str[3]} ms`,
}
return uncertain_stats_formatter(stats, time_formatter)
}
11 changes: 7 additions & 4 deletions examples/2_speedtest/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { applyClientPlugin as speedtest_applyClientPlugin, summarizeSpeedtestStats } from "../../src/plugins/speedtest.ts"
import { applyClientPlugin as timesync_applyClientPlugin } from "../../src/plugins/timesync.ts"
import { summarizeTimesyncStats, applyClientPlugin as timesync_applyClientPlugin } from "../../src/plugins/timesync.ts"
import { Sock } from "../../src/sock.ts"
import { urlPathname } from "./deps.ts"
import { domainName, speedtestStatFormatter, timesyncStatFormatter } from "./deps_client.ts"
Expand All @@ -22,7 +22,9 @@ const
get_server_time = timesync_applyClientPlugin(client_sock, "perf"),
get_speedtest = speedtest_applyClientPlugin(client_sock, "perf", get_server_time),
run_get_server_time = () => {
get_server_time(20, 5).then((stats) => {
get_server_time(20).then((results) => {
results.splice(0, 5)
const stats = summarizeTimesyncStats(results)
printJsonToWebpage(timesyncStatFormatter(stats))
})
},
Expand All @@ -32,8 +34,9 @@ const
["down", 2 * 1024 ** 2],
["up", 2 * 1024 ** 2],
["up", 2 * 1024 ** 2],
]).then((stats) => {
printJsonToWebpage(speedtestStatFormatter(summarizeSpeedtestStats(stats)))
]).then((results) => {
const stats = summarizeSpeedtestStats(results)
printJsonToWebpage(speedtestStatFormatter(stats))
})
}

Expand Down
43 changes: 19 additions & 24 deletions examples/2_speedtest/deps_client.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
/** dependencies of the client code. */

import type { UncertainValue } from "../../src/plugins/deps.ts"
import type { SpeedtestStats } from "../../src/plugins/speedtest.ts"
import type { TimesyncStats } from "../../src/plugins/timesync.ts"

const
number_formatter = (value: number) => (value.toFixed(3)),
bytes_per_sec_to_mbps = (value: number) => (8 * value / (1024 ** 2))
time_formatter = (value: number, error: number) => (`${number_formatter(value)} ± ${number_formatter(error)} ms`),
uncertain_stats_formatter = (
stats: Record<string, UncertainValue> | Object,
formatter: (value: UncertainValue["value"], error: UncertainValue["error"]) => string,
) => {
return Object.fromEntries(
Object.entries(stats).map(([entry, uncertain_value]) => {
const { value, error } = uncertain_value
return [entry, formatter(value, error)]
})
)
},
bytes_per_sec_to_mbps = (value: number) => (8 * value / (1024 ** 2)),
speed_formatter = (value: number, error: number) => (
`${number_formatter(bytes_per_sec_to_mbps(value))} ± ${number_formatter(bytes_per_sec_to_mbps(error))} Mbps`
)

export const
domainName = globalThis.location?.host as string,
timesyncStatFormatter = (stats: TimesyncStats) => {
const
[means, stdevs] = stats,
means_str = means.map(number_formatter),
stdevs_str = stdevs.map(number_formatter)
return {
serverUplinkOffsetTime: `${means_str[0]} ± ${stdevs_str[0]} ms`,
serverDownlinkOffsetTime: `${means_str[1]} ± ${stdevs_str[1]} ms`,
serverProcessTime: `${means_str[2]} ± ${stdevs_str[2]} ms`,
returnTripTime: `${means_str[3]} ± ${stdevs_str[3]} ms`,
}
return uncertain_stats_formatter(stats, time_formatter)
},
speedtestStatFormatter = (stats: SpeedtestStats) => {
const {
downlinkSpeed: { value: dl, error: dl_stdev },
uplinkSpeed: { value: ul, error: ul_stdev },
} = stats
const
dl_mbps = number_formatter(bytes_per_sec_to_mbps(dl)),
dl_stdev_mbps = number_formatter(bytes_per_sec_to_mbps(dl_stdev)),
ul_mbps = number_formatter(bytes_per_sec_to_mbps(ul)),
ul_stdev_mbps = number_formatter(bytes_per_sec_to_mbps(ul_stdev))
return {
downlinkSpeed: `${dl_mbps} ± ${dl_stdev_mbps} Mbps`,
uplinkSpeed: `${ul_mbps} ± ${ul_stdev_mbps} Mbps`,
}
return uncertain_stats_formatter(stats, speed_formatter)
}

0 comments on commit 7cf737d

Please sign in to comment.