TCP Socket Connection Throughput Tester by Corey Goldberg

Subscribe To Corey's Blog Subscribe To Goldblog goldb.org home

Python | TCP Socket Connection Throughput Tester

by Corey Goldberg - 2008

This is a script used for generating concurrent socket requests to a TCP server. Each thread connects and disconnects from a socket as quickly as possible to measure connection throughput. The throughput rate is displayed on the console during execution. This might be useful for performance benchmarking or load testing.


Download:

tcp_socket_throughput.py


tcp_socket_throughput Script:

#!/usr/bin/env python
#
#  tcp_socket_throughput.py
#  TCP Socket Connection Throughput Tester
#  Corey Goldberg (www.goldb.org), 2008

import sys
import time
import socket
from threading import Thread

host = '192.168.12.151'
port = 80

thread_count = 10  # concurrent sender agents

class Controller:
    def __init__(self):
        self.count_ref = []
    def start(self):
        for i in range(thread_count):
            agent = Agent(self.count_ref)
            agent.setDaemon(True)
            agent.start()
        print 'started %d threads' % (i + 1)
        while True:
            time.sleep(1)
            line = 'connects/sec: %s' % len(self.count_ref)
            self.count_ref[:] = []
            sys.stdout.write(chr(0x08) * len(line))
            sys.stdout.write(line)
            
class Agent(Thread):
    def __init__(self, count_ref):
        Thread.__init__(self)
        self.count_ref = count_ref
    def run(self):
        while True:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                s.connect((host, port))
                s.shutdown(2)
                s.close()
                self.count_ref.append(1)
            except:
                print 'SOCKET ERROR\n'
                
if __name__ == '__main__':
    controller = Controller()
    controller.start()
Copyright © 2006-2008 Corey Goldberg  |