diff mbox series

[bitbake-devel] bitbake-hashclient: Add ping command

Message ID 20240522162943.2956896-1-JPEWhacker@gmail.com
State New
Headers show
Series [bitbake-devel] bitbake-hashclient: Add ping command | expand

Commit Message

Joshua Watt May 22, 2024, 4:29 p.m. UTC
Adds a ping subcommand to bitbake-hashclient which can be useful to
measure connection latency

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
---
 bitbake/bin/bitbake-hashclient | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
diff mbox series

Patch

diff --git a/bitbake/bin/bitbake-hashclient b/bitbake/bin/bitbake-hashclient
index 610787ed2b0..e1e11446448 100755
--- a/bitbake/bin/bitbake-hashclient
+++ b/bitbake/bin/bitbake-hashclient
@@ -16,6 +16,7 @@  import time
 import warnings
 import netrc
 import json
+import statistics
 warnings.simplefilter("default")
 
 try:
@@ -225,6 +226,26 @@  def main():
         print("true" if result else "false")
         return 0
 
+    def handle_ping(args, client):
+        times = []
+        for i in range(1, args.count + 1):
+            print(f"Ping {i} of {args.count}... ", end="")
+            start_time = time.perf_counter()
+            client.ping()
+            elapsed = time.perf_counter() - start_time
+            times.append(elapsed)
+            print(f"{elapsed:.3f}s")
+
+        mean = statistics.mean(times)
+        std_dev = statistics.pstdev(times)
+
+        print("------------------------")
+        print(f"Average round trip time: {mean:.3f}s")
+        print(f"Round trip time std dev: {std_dev:.3f}s")
+        print(f"Min time is:             {min(times):.3f}s")
+        print(f"Max time is:             {max(times):.3f}s")
+        return 0
+
     parser = argparse.ArgumentParser(description='Hash Equivalence Client')
     parser.add_argument('--address', default=DEFAULT_ADDRESS, help='Server address (default "%(default)s")')
     parser.add_argument('--log', default='WARNING', help='Set logging level')
@@ -322,6 +343,10 @@  def main():
     unihash_exists_parser.add_argument("unihash", help="Unihash to check")
     unihash_exists_parser.set_defaults(func=handle_unihash_exists)
 
+    ping_parser = subparsers.add_parser('ping', help="Ping server")
+    ping_parser.add_argument("-n", "--count", type=int, help="Number of pings. Default is %(default)s", default=10)
+    ping_parser.set_defaults(func=handle_ping)
+
     args = parser.parse_args()
 
     logger = logging.getLogger('hashserv')