Created
January 14, 2010 05:17
-
-
Save saivenkat/276914 to your computer and use it in GitHub Desktop.
UDP load generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspired fromCorey Goldberg's load gen work. | |
import sys | |
import time | |
import socket | |
from threading import Thread | |
host = '127.0.0.1' | |
port = 21567 | |
data = "This is a test packet" | |
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_DGRAM) | |
try: | |
if (s.sendto(data, (host, port))): | |
s.close() | |
self.count_ref.append(1) | |
except Exception, ex: | |
print ex | |
print 'SOCKET ERROR\n' | |
if __name__ == '__main__': | |
controller = Controller() | |
controller.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment