|
42 | 42 | from bittensor_cli.src.bittensor import utils
|
43 | 43 | from bittensor_cli.src.bittensor.balances import Balance
|
44 | 44 | from bittensor_cli.src.bittensor.chain_data import SubnetHyperparameters
|
45 |
| -from bittensor_cli.src.bittensor.subtensor_interface import SubtensorInterface |
| 45 | +from bittensor_cli.src.bittensor.subtensor_interface import ( |
| 46 | + SubtensorInterface, |
| 47 | + best_connection, |
| 48 | +) |
46 | 49 | from bittensor_cli.src.bittensor.utils import (
|
47 | 50 | console,
|
48 | 51 | err_console,
|
@@ -1048,6 +1051,9 @@ def __init__(self):
|
1048 | 1051 | "remove", rich_help_panel=HELP_PANELS["LIQUIDITY"]["LIQUIDITY_MGMT"]
|
1049 | 1052 | )(self.liquidity_remove)
|
1050 | 1053 |
|
| 1054 | + # utils app |
| 1055 | + self.utils_app.command("latency")(self.best_connection) |
| 1056 | + |
1051 | 1057 | def generate_command_tree(self) -> Tree:
|
1052 | 1058 | """
|
1053 | 1059 | Generates a rich.Tree of the commands, subcommands, and groups of this app
|
@@ -6326,6 +6332,57 @@ def convert(
|
6326 | 6332 | f"{Balance.from_tao(tao).rao}{Balance.rao_unit}",
|
6327 | 6333 | )
|
6328 | 6334 |
|
| 6335 | + def best_connection( |
| 6336 | + self, |
| 6337 | + additional_networks: Optional[list[str]] = typer.Option( |
| 6338 | + None, |
| 6339 | + "--network", |
| 6340 | + help="Network(s) to test for the best connection", |
| 6341 | + ), |
| 6342 | + ): |
| 6343 | + f""" |
| 6344 | + This command will give you the latency of all finney-like network in additional to any additional networks you specify via the {arg__("--network")} flag |
| 6345 | + |
| 6346 | + The results are two-fold. One column is the overall time to initialise a connection, send a request, and wait for the result. The second column measures single ping-pong speed once connected. |
| 6347 | + |
| 6348 | + EXAMPLE |
| 6349 | + |
| 6350 | + [green]$[/green] btcli utils latency --network ws://189.234.12.45 --network wss://mysubtensor.duckdns.org |
| 6351 | +
|
| 6352 | + """ |
| 6353 | + additional_networks = additional_networks or [] |
| 6354 | + if any(not x.startswith("ws") for x in additional_networks): |
| 6355 | + err_console.print( |
| 6356 | + "Invalid network endpoint. Ensure you are specifying a valid websocket endpoint.", |
| 6357 | + ) |
| 6358 | + return False |
| 6359 | + results: dict[str, list[float]] = self._run_command( |
| 6360 | + best_connection(Constants.finney_nodes + (additional_networks or [])) |
| 6361 | + ) |
| 6362 | + sorted_results = { |
| 6363 | + k: v for k, v in sorted(results.items(), key=lambda item: item[1][0]) |
| 6364 | + } |
| 6365 | + table = Table( |
| 6366 | + Column("Network"), |
| 6367 | + Column("End to End Latency", style="cyan"), |
| 6368 | + Column("Single Request Ping", style="cyan"), |
| 6369 | + title="Connection Latencies (seconds)", |
| 6370 | + caption="lower value is faster", |
| 6371 | + ) |
| 6372 | + for n_name, (overall_latency, single_request) in sorted_results.items(): |
| 6373 | + table.add_row(n_name, str(overall_latency), str(single_request)) |
| 6374 | + console.print(table) |
| 6375 | + fastest = next(iter(sorted_results.keys())) |
| 6376 | + if conf_net := self.config.get("network", ""): |
| 6377 | + if not conf_net.startswith("ws") and conf_net in Constants.networks: |
| 6378 | + conf_net = Constants.network_map[conf_net] |
| 6379 | + if conf_net != fastest: |
| 6380 | + console.print( |
| 6381 | + f"The fastest network is {fastest}. You currently have {conf_net} selected as your default network." |
| 6382 | + f"\nYou can update this with {arg__(f'btcli config set --network {fastest}')}" |
| 6383 | + ) |
| 6384 | + return True |
| 6385 | + |
6329 | 6386 | def run(self):
|
6330 | 6387 | self.app()
|
6331 | 6388 |
|
|
0 commit comments