Ejemplo n.º 1
0
def run_speedtest() -> (dict, float, float):
    st.shutdown_event = threading.Event()
    socket.setdefaulttimeout(SOCKET_TIMEOUT)
    st.build_user_agent()
    config = getConfig()
    if not config:
        return {}, -1, -1
    servers = st.closestServers(config['client'])
    best = getBestServer(servers, st.build_user_agent())
    # print(best)

    urls = []
    for size in [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]:
        for i in range(0, 4):
            urls.append('%s/random%sx%s.jpg' % (os.path.dirname(best['url']), size, size))
    dlspeed = st.downloadSpeed(urls, True) * 8 / 1024 / 1024
    # print(dlspeed)

    sizes = []
    for size in [int(.25 * 1024 * 1024), int(.5 * 1024 * 1024)]:
        for i in range(0, 25):
            sizes.append(size)
    ulspeed = st.uploadSpeed(best['url'], sizes, True) * 8 / 1024 / 1024
    # print(ulspeed)

    return best, dlspeed, ulspeed
Ejemplo n.º 2
0
def testSpeed(urls):
    speedtest_cli.shutdown_event = threading.Event()
    signal.signal(signal.SIGINT, speedtest_cli.ctrl_c)

    print "Start to test download speed: "
    dlspeed = speedtest_cli.downloadSpeed(urls)
    dlspeed = (dlspeed / 1000 / 1000)
    print('Download: %0.2f M%s/s' % (dlspeed, 'B'))

    return dlspeed
Ejemplo n.º 3
0
def main():
    speedtest_cli.shutdown_event = threading.Event()

    parser = argparse.ArgumentParser()
    parser.add_argument('--timeout', default=10, type=int,
                        help='HTTP timeout in seconds. Default 10')
    parser.add_argument('--secure', action='store_true',
                        help='Use HTTPS instead of HTTP when communicating '
                             'with speedtest.net operated servers')
    parser.add_argument('--graphite-host', type=str, default='localhost',
                        help='The graphite host to publish results to')
    parser.add_argument('--graphite-port', type=int, default=2003,
                        help='The graphite (line-receiver) port to publish results to')
    parser.add_argument('--graphite-prefix', type=str, default="speedtest",
                        help='The graphite metrics prefix')
    args = parser.parse_args()

    socket.setdefaulttimeout(args.timeout)
    speedtest_cli.build_user_agent()

    if args.secure:
        speedtest_cli.scheme = 'https'

    try:
        config = speedtest_cli.getConfig()
    except speedtest_cli.URLError:
        print('Cannot retrieve speedtest configuration')
        sys.exit(1)

    servers = speedtest_cli.closestServers(config['client'])
    best = speedtest_cli.getBestServer(servers)

    print("Using speedtest.net server hosted by {sponsor} ({name}).\n"
          "Host: {host}\nDistance: {d:0.1f} km\nLatency: {latency} ms".format(**best))

    sizes = [350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000]
    urls = []
    for size in sizes:
        for i in range(0, 4):
            urls.append('%s/random%sx%s.jpg' %
                        (os.path.dirname(best['url']), size, size))
    dlspeed = speedtest_cli.downloadSpeed(urls, True)
    print('Download: %0.2f Mbit/s' % ((dlspeed / 1000 / 1000) * 8))

    sizesizes = [int(.25 * 1000 * 1000), int(.5 * 1000 * 1000)]
    sizes = []
    for size in sizesizes:
        for i in range(0, 25):
            sizes.append(size)
    ulspeed = speedtest_cli.uploadSpeed(best['url'], sizes, True)
    print('Upload: %0.2f Mbit/s' % ((ulspeed/ 1000 / 1000) * 8))

    t = int(time.time())
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((args.graphite_host, args.graphite_port))
    s.send(
        "{prefix}.latency {latency} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        latency=best['latency'],
        timestamp=t,
    ))
    s.send(
        "{prefix}.distance {distance} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        distance=best['d'],
        timestamp=t,
    ))
    s.send(
        "{prefix}.upload {upload} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        upload=ulspeed,
        timestamp=t,
    ))
    s.send(
        "{prefix}.download {download} {timestamp}\n".format(
        prefix=args.graphite_prefix,
        download=dlspeed,
        timestamp=t,
    ))
    s.close()