# Bottleneck when receiving UDP packets?

**URL:** https://discourse.julialang.org/t/bottleneck-when-receiving-udp-packets/41425
**Category:** General Usage
**Tags:** networking
**Created:** [June 15, 2020, 8:52am UTC](https://discourse.julialang.org/t/bottleneck-when-receiving-udp-packets/41425 "2020-06-15T08:52:14Z")
**Posts on this page:** 1
**Showing post:** 22

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [June 15, 2020, 8:36pm UTC](https://discourse.julialang.org/t/bottleneck-when-receiving-udp-packets/41425/22 "2020-06-15T20:36:12Z")

</div>

On the sending side, I think sleeping after every packet is the issue. the [julia sleep function is only accurate to within about 1ms](https://discourse.julialang.org/t/accuracy-of-sleep/5546), there’s a usleep function you can Ccall which could be better.

I don’t think this is actually on the receive end, it’s the sending. Just my current guess.

I’d suggest to figure out how many packets you’re supposed to send per 5ms, send those, and then sleep 5ms. yes it’s bursty, but figure even at 100k packets per second, in 5ms we’re only talking 500 packets per loop, and 100k packets per second is crazy high for most purposes. If you’re talking 10k packets per second, you’d be sending 50 packets per time through the loop, then check how long to sleep, then sleep that long, and start over.

EDIT: indeed when I do this:

```julia

function send_data(sock, target, port, data, packets_per_second)
    delta_t = 1 / packets_per_second
    stopat = time() + delta_t
    pp5m = round(packets_per_second * 5/1000)
    while true
        now = time()
        for i in 1:pp5m
            send(sock, target, port, data)
        end
        after = time()
        
        pausefor = now + .005 - after
        if pausefor < 0
            # error("Can't keep up with UDP packet rate ($pausefor s)")
            print(".")
            continue
        end
        sleep(pausefor)
    end
end

```

I have no problem running your sender with 10000 packets per second with no overruns on my linux desktop and the receiver says:

```julia
Counting UDP packets for 20 seconds
150601 UDP packets recieved

```

or about 7500 pps

So it’s not a super accurate method of timing, but basically the issue is that you’re sending one packet and sleeping rather than sleeping less often or calling `usleep`

@tamasgal

---

_[View the full topic](https://discourse.julialang.org/t/bottleneck-when-receiving-udp-packets/41425)._
