IPC performance: Named Pipe vs Socket

后端 未结 10 1516
自闭症患者
自闭症患者 2020-11-28 01:39

Everyone seems to say named pipes are faster than sockets IPC. How much faster are they? I would prefer to use sockets because they can do two-way communication and are very

相关标签:
10条回答
  • 2020-11-28 02:00

    As often, numbers says more than feeling, here are some data: Pipe vs Unix Socket Performance (opendmx.net).

    This benchmark shows a difference of about 12 to 15% faster speed for pipes.

    0 讨论(0)
  • 2020-11-28 02:05

    One problem with sockets is that they do not have a way to flush the buffer. There is something called the Nagle algorithm which collects all data and flushes it after 40ms. So if it is responsiveness and not bandwidth you might be better off with a pipe.

    You can disable the Nagle with the socket option TCP_NODELAY but then the reading end will never receive two short messages in one single read call.

    So test it, i ended up with none of this and implemented memory mapped based queues with pthread mutex and semaphore in shared memory, avoiding a lot of kernel system calls (but today they aren't very slow anymore).

    0 讨论(0)
  • 2020-11-28 02:06

    Named pipes and sockets are not functionally equivalent; sockets provide more features (they are bidirectional, for a start).

    We cannot tell you which will perform better, but I strongly suspect it doesn't matter.

    Unix domain sockets will do pretty much what tcp sockets will, but only on the local machine and with (perhaps a bit) lower overhead.

    If a Unix socket isn't fast enough and you're transferring a lot of data, consider using shared memory between your client and server (which is a LOT more complicated to set up).

    Unix and NT both have "Named pipes" but they are totally different in feature set.

    0 讨论(0)
  • 2020-11-28 02:07

    Best results you'll get with Shared Memory solution.

    Named pipes are only 16% better than TCP sockets.

    Results are get with IPC benchmarking:

    • System: Linux (Linux ubuntu 4.4.0 x86_64 i7-6700K 4.00GHz)
    • Message: 128 bytes
    • Messages count: 1000000

    Pipe benchmark:

    Message size:       128
    Message count:      1000000
    Total duration:     27367.454 ms
    Average duration:   27.319 us
    Minimum duration:   5.888 us
    Maximum duration:   15763.712 us
    Standard deviation: 26.664 us
    Message rate:       36539 msg/s
    

    FIFOs (named pipes) benchmark:

    Message size:       128
    Message count:      1000000
    Total duration:     38100.093 ms
    Average duration:   38.025 us
    Minimum duration:   6.656 us
    Maximum duration:   27415.040 us
    Standard deviation: 91.614 us
    Message rate:       26246 msg/s
    

    Message Queue benchmark:

    Message size:       128
    Message count:      1000000
    Total duration:     14723.159 ms
    Average duration:   14.675 us
    Minimum duration:   3.840 us
    Maximum duration:   17437.184 us
    Standard deviation: 53.615 us
    Message rate:       67920 msg/s
    

    Shared Memory benchmark:

    Message size:       128
    Message count:      1000000
    Total duration:     261.650 ms
    Average duration:   0.238 us
    Minimum duration:   0.000 us
    Maximum duration:   10092.032 us
    Standard deviation: 22.095 us
    Message rate:       3821893 msg/s
    

    TCP sockets benchmark:

    Message size:       128
    Message count:      1000000
    Total duration:     44477.257 ms
    Average duration:   44.391 us
    Minimum duration:   11.520 us
    Maximum duration:   15863.296 us
    Standard deviation: 44.905 us
    Message rate:       22483 msg/s
    

    Unix domain sockets benchmark:

    Message size:       128
    Message count:      1000000
    Total duration:     24579.846 ms
    Average duration:   24.531 us
    Minimum duration:   2.560 us
    Maximum duration:   15932.928 us
    Standard deviation: 37.854 us
    Message rate:       40683 msg/s
    

    ZeroMQ benchmark:

    Message size:       128
    Message count:      1000000
    Total duration:     64872.327 ms
    Average duration:   64.808 us
    Minimum duration:   23.552 us
    Maximum duration:   16443.392 us
    Standard deviation: 133.483 us
    Message rate:       15414 msg/s
    
    0 讨论(0)
  • 2020-11-28 02:08

    Keep in mind that sockets does not necessarily mean IP (and TCP or UDP). You can also use UNIX sockets (PF_UNIX), which offer a noticeable performance improvement over connecting to 127.0.0.1

    0 讨论(0)
  • 2020-11-28 02:11

    If you do not need speed, sockets are the easiest way to go!

    If what you are looking at is speed, the fastest solution is shared Memory, not named pipes.

    0 讨论(0)
提交回复
热议问题