2

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()
tyler124
  • 613
  • 1
  • 8
  • 20
  • 2
    " 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. " Classic behaviour of the [Global Interpreter Lock](https://wiki.python.org/moin/GlobalInterpreterLock). Your other use case is more akin to [`multiprocessing`](https://docs.python.org/3/library/multiprocessing.html) where each individual process is restricted by its own GIL. – roganjosh Mar 14 '18 at 21:12
  • Possible duplicate of [Does Python support multithreading? Can it speed up execution time?](https://stackoverflow.com/questions/20939299/does-python-support-multithreading-can-it-speed-up-execution-time) – roganjosh Mar 14 '18 at 21:15
  • 1
    That does appear to be the issue. I created a new process using multiprocess instead and was able to generate the full amount of data. Learned something new today, thanks! – tyler124 Mar 15 '18 at 23:52

0 Answers0