Giter VIP home page Giter VIP logo

tcp-latency's Introduction

tcp-latency

PyPI - Python Version PyPI PyPI - License Build Status PyPI - Downloads

About

tcp-latency provides an easy way to measure latency using TCP.

Inspired by other similar tools, tcp-latency comes from the need of running network diagnosis/troubleshooting tasks with Python on serverless infrastructure (as many providers don't include ping/ICMP support) but should work too in any other environment with Python>=36.

Features

  • Runs as a command-line tool or inside your code as a module.
  • Custom parameters for a port, runs, timeout and wait time between runs.
  • IPv4 (e.g 52.26.14.11) and domain (e.g google.com) host support.
  • Human readable output when running as a command-line tool.
  • No external dependencies.
  • Small and extensible.

Usage

tcp-latency can be used both as a module and as a standalone script.

Module

>>> from tcp_latency import measure_latency
>>> measure_latency(host='google.com')
[34.57]
>>> measure_latency(host='52.26.14.11', port=80, runs=10, timeout=2.5)
[433.82, 409.21, 409.25, 307.09, 306.64, 409.45, 306.58, 306.93, 409.25, 409.26]

Note: If omitted, measure_latency() arguments use the same defaults that command-line mode.

Command-line

$ tcplatency -h
usage: tcp-latency [-h] [-p [p]] [-t [t]] [-r [r]] [-w [w]] h

Measure latency using TCP.

positional arguments:
  host

optional arguments:
  -h, --help            show this help message and exit
  -p [p], --port [p]    (default: 443)
  -t [t], --timeout [t]
                        (seconds, float, default: 5)
  -r [r], --runs [r]    number of latency points (int, default: 5)
  -w [w], --wait [w]    between each run (seconds, float, default: 1)
$ tcp-latency google.com
tcp-latency google.com
google.com: tcp seq=0 port=443 timeout=5 time=15.16 ms
google.com: tcp seq=1 port=443 timeout=5 time=15.63 ms
google.com: tcp seq=2 port=443 timeout=5 time=14.86 ms
google.com: tcp seq=3 port=443 timeout=5 time=14.76 ms
google.com: tcp seq=4 port=443 timeout=5 time=14.59 ms
--- google.com tcp-latency statistics ---
5 packets transmitted
rtt min/avg/max = 14.76/15.1025/15.63 ms

$ tcp-latency 52.26.14.11 --port 80 --runs 3 --wait 0.5
tcp-latency 52.26.14.11
52.26.14.11: tcp seq=0 port=80 timeout=5 time=230.2 ms
52.26.14.11: tcp seq=1 port=80 timeout=5 time=228.96 ms
52.26.14.11: tcp seq=2 port=80 timeout=5 time=224.51 ms
--- 52.26.14.11 tcp-latency statistics ---
3 packets transmitted
rtt min/avg/max = 228.96/229.57999999999998/230.2 ms

$ tcp-latency google.com -r 1
tcp-latency google.com
google.com: tcp seq=0 port=443 timeout=5 time=34.36 ms
--- google.com tcp-latency statistics ---
1 packets transmitted

Installation

Via pip:

pip install tcp-latency

How to contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
  2. Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).
  3. Write a test which shows that the bug was fixed or that the feature works as expected.
  4. Send a pull request and bug me until it gets merged and published.

Some things that would be great to have:

  • Add transmitted vs received statistics (e.g packet loss)
  • Add bytes information (ping-like)
  • Add support for machine-readable output (JSON,XML,YAML).
  • Add automated releases with TravisCI.
  • Improve test suite.
  • Improve How to contribute information (pyenv, tox, pre-commit...)

Similar tools

tcp-latency's People

Contributors

atolkachev avatar chinvib66 avatar dgzlopes avatar felipedotcom avatar kronicd avatar strongerxi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

tcp-latency's Issues

CLI error calculating RTT when any latency point is `None`

When executing the CLI with more then one run, if a single latency_point in the latency_points list is None, then when the last run is reached, an error is thrown:

> tcp_latency example.does.not.exist
tcp-latency example.does.not.exist
example.does.not.exist: tcp seq=0 port=443 timeout=5 failed
example.does.not.exist: tcp seq=1 port=443 timeout=5 failed
example.does.not.exist: tcp seq=2 port=443 timeout=5 failed
example.does.not.exist: tcp seq=3 port=443 timeout=5 failed
example.does.not.exist: tcp seq=4 port=443 timeout=5 failed
--- example.does.not.exist tcp-latency statistics ---
5 packets transmitted
Traceback (most recent call last):
  File "tcp_latency/tcp_latency.py", line 154, in <module>
    _main()
  File "tcp_latency/tcp_latency.py", line 143, in _main
    measure_latency(
  File "tcp_latency/tcp_latency.py", line 91, in measure_latency
    f'rtt min/avg/max = {min(latency_points)}/{mean(latency_points)}/{max(latency_points)} ms',   # noqa: E501
TypeError: '<' not supported between instances of 'NoneType' and 'NoneType'

None of the three methods - min, mean, or max - are able to accept iterables that contain None.

The behavior is the same when just one of the points are None and the rest are valid floatss.

Due to another issue, the last latency_point is not included in RTT calculations, therefore if only the last point that is None, this error is not thrown.

Add transmitted vs received statistics

Transmitted, received, packet loss... Example from ping:

--- google.com ping statistics ---
39 packets transmitted, 39 received, 0% packet loss, time 38045ms

Timer() issue

Please consider to use time.time() instead of timer ().
Thank you for sharing your code!!

RTT time is incorrect while using multithreading

In python, I created a thread list and append the 40 times ping works measure_latency(host, port) into the list, then the result was like:

CH. 3 =  15.075 ms
CH. 17 =  20.111 ms
CH. 16 =  20.111 ms
CH. 4 =  20.593 ms
CH. 9 =  20.111 ms
CH. 14 =  21.094 ms
CH. 6 =  20.595 ms
CH. 8 =  20.595 ms
CH. 7 =  24.604 ms
CH. 11 =  20.111 ms
CH. 13 =  24.606 ms
CH. 10 =  25.609 ms
CH. 12 =  20.094 ms
CH. 2 =  25.104 ms
CH. 15 =  25.107 ms
CH. 35 =  25.105 ms
CH. 32 =  29.118 ms
CH. 1 =  25.104 ms
CH. 29 =  29.117 ms
CH. 24 =  33.128 ms
CH. 5 =  35.132 ms
CH. 31 =  29.117 ms
CH. 20 =  34.632 ms
CH. 21 =  35.633 ms
CH. 22 =  33.628 ms
CH. 36 =  34.129 ms
CH. 30 =  38.139 ms
CH. 26 =  38.641 ms
CH. 18 =  38.641 ms
CH. 25 =  39.142 ms
CH. 33 =  36.134 ms
CH. 28 =  38.641 ms
CH. 40 =  43.153 ms
CH. 34 =  35.132 ms
CH. 37 =  44.158 ms
CH. 23 =  25.607 ms
CH. 38 =  42.652 ms
CH. 39 =  44.158 ms
CH. 19 =  34.632 ms
CH. 27 =  39.142 ms

The RTT time above somehow progressively increases.

But without multithreaded, the result will be like:

CH. 1 =  22.148 ms
CH. 2 =  10.528 ms
CH. 3 =  11.267 ms
CH. 4 =  20.77 ms
CH. 5 =  22.03 ms
CH. 6 =  20.608 ms
CH. 7 =  22.036 ms
CH. 8 =  21.07 ms
CH. 9 =  11.059 ms
CH. 10 =  10.388 ms
CH. 11 =  11.491 ms
CH. 12 =  21.23 ms
CH. 13 =  21.141 ms
CH. 14 =  10.883 ms
CH. 15 =  21.39 ms
CH. 16 =  10.012 ms
CH. 17 =  11.529 ms
CH. 18 =  22.938 ms
CH. 19 =  21.064 ms
CH. 20 =  10.528 ms
CH. 21 =  20.711 ms
CH. 22 =  21.616 ms
CH. 23 =  21.658 ms
CH. 24 =  20.516 ms
CH. 25 =  21.279 ms
CH. 26 =  10.48 ms
CH. 27 =  22.067 ms
CH. 28 =  21.048 ms
CH. 29 =  10.035 ms
CH. 30 =  21.028 ms
CH. 31 =  11.028 ms
CH. 32 =  21.891 ms
CH. 33 =  21.369 ms
CH. 34 =  10.487 ms
CH. 35 =  21.774 ms
CH. 36 =  20.57 ms
CH. 37 =  11.006 ms
CH. 38 =  10.755 ms
CH. 39 =  21.569 ms
CH. 40 =  10.053 ms

It seems very normal without multithreaded.
I don't know why the result is incorrect in multithread, any help would be appreciated!

human output shows wrong amount of transmitted packets

Thanks a lot for this useful library/tool!

It says 2 out of 3 packets transmitted successfully but there are 3 results, can this be a bug?

In [58]: tcp_latency.measure_latency("google.com", runs=3, human_output=True)
tcp-latency google.com
google.com: tcp seq=0 port=443 timeout=5 time=14.383041998371482 ms
google.com: tcp seq=1 port=443 timeout=5 time=13.783250004053116 ms
google.com: tcp seq=2 port=443 timeout=5 time=14.244875172153115 ms
--- google.com tcp-latency statistics ---
2 out of 3 packets transmitted successfully
rtt min/avg/max = 13.783250004053116/14.083146001212299/14.383041998371482 ms
Out[58]: [14.383041998371482, 13.783250004053116, 14.244875172153115]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.