-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- update the client code of the timesync and speedtest examples in `/…
…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
Showing
4 changed files
with
48 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |