I have a program where I'm sending a list of packets out on a Scapy level 2 socket.
def burstPackets(packetList, socket, rateHz, executeTimes):
rate = Decimal(1)/Decimal(rateHz)
for i in range(0, executeTimes):
for packet in packetList:
socket.send(packet)
time.sleep(rate)
def executeRecordingTest():
DFI_PACKET_SIZE = 1250
DFI_PACKET_RATE_HZ = 3000
DFI_SECONDS_OF_PACKETS_TO_GENERATE = 30
DFI_THREAD_EXECUTION_CYCLES = 1
packetList3328 = generatePacketList(3328, DFI_PACKET_SIZE, DFI_PACKET_RATE_HZ, DFI_SECONDS_OF_PACKETS_TO_GENERATE)
myIfaces=ifaces.data.keys()
ODN_INTERFACE = myIfaces[1]
socket3328 = conf.L2socket(iface=ODN_INTERFACE)
burstPackets(packetList3328, socket3328, DFI_PACKET_RATE_HZ, DFI_THREAD_EXECUTION_CYCLES)
I can run multiple instances of this code by opening up multiple command line windows and running the same python script in each one of them. Each separate instance of the Python script is able to generate ~30Mbps of packets, with 3 windows generating ~90Mbps. However, i'd like to generate a large amount of packets in a single script using threading. I used the code below to try to generate packets using 2 separate threads at what should be ~30Mbps in each thread. However it seems like each thread running took a performance penalty, with each thread only generating ~15Mbps of data. How do the multiple instances not incur the performance penalty that running multiple threads do in this case?
def burstPackets(packetList, socket, rateHz, executeTimes):
rate = Decimal(1)/Decimal(rateHz)
for i in range(0, executeTimes):
for packet in packetList:
socket.send(packet)
time.sleep(rate)
def executeRecordingTest():
DFI_PACKET_SIZE = 1250
DFI_PACKET_RATE_HZ = 3000
DFI_SECONDS_OF_PACKETS_TO_GENERATE = 30
DFI_THREAD_EXECUTION_CYCLES = 1
packetList3328 = generatePacketList(3328, DFI_PACKET_SIZE, DFI_PACKET_RATE_HZ, DFI_SECONDS_OF_PACKETS_TO_GENERATE)
myIfaces=ifaces.data.keys()
ODN_INTERFACE = myIfaces[1]
socket3328 = conf.L2socket(iface=ODN_INTERFACE)
socket3328_2 = conf.L2socket(iface=ODN_INTERFACE)
thread1 = threading.Thread(target=burstPackets, args=(packetList3328, socket3328, DFI_PACKET_RATE_HZ, DFI_THREAD_EXECUTION_CYCLES) )
thread2 = threading.Thread(target=burstPackets, args=(packetList3328, socket3328_2, DFI_PACKET_RATE_HZ, DFI_THREAD_EXECUTION_CYCLES) )
thread1.start()
thread2.start()
thread1.join()
thread2.join()