1

I am trying to create a set of websocket clients with the following code:

$server = stream_socket_server("tcp://127.0.0.1:8080");

for ($i = 1; $i <= 50; $i++) {
    var_dump($i);
    stream_socket_client("tcp://127.0.0.1:8080");
}

The first 35, or so, connections are created very fast. Then everything slows down and every step takes 1 second to execute.

Could you explain to me why this behaviour happens? Is it caused by a configuration parameter? Is it a common websocket behaviour?

marcosh
  • 8,780
  • 5
  • 44
  • 74
  • because after 35 requests memory starts exhausting?CPU utilization reaches to almost 100%, network input is maximum? just like DDos. – Coder anonymous May 09 '15 at 18:36
  • Can you add precisions about your php version and operating system ? – Clément Prévost May 09 '15 at 18:37
  • @Coderanonymous from my system monitor I can't see big cpu, memory or network usage – marcosh May 09 '15 at 18:39
  • @ClémentPrévost PHP 5.6.4-4ubuntu6 (cli) (built: Apr 17 2015 15:47:51) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies with Xdebug v2.2.6, Copyright (c) 2002-2014, by Derick Rethans – marcosh May 09 '15 at 18:39
  • Interesting fact: event with multiple connecting scripts (containing only stream_socket_client) after some point in time, connecting gets slower. stream_socket_server implementation slow down? – anonymous May 09 '15 at 21:18
  • @JurgisGregov I am thinkng the same thing. I could be some kind of protection against massive amount of simultaneous connections – marcosh May 09 '15 at 21:24

1 Answers1

1

Working hypothesis: pending connections slows down the port

<?php
$server = stream_socket_server("tcp://127.0.0.1:8080");
for ($i = 1; $i <= 50; $i++) {
    var_dump($i);
    stream_socket_client("tcp://127.0.0.1:8080");
    stream_socket_accept($server);
}

Supplement:

In case listening party is server.c (modified not to accepting connections). Connections slow down after 27% of specified backlog. http://www.linuxhowtos.org/data/6/server.c

<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, "127.0.0.1", 8081);
$backlog = 500; // less than /proc/sys/net/core/somaxconn
socket_listen($socket, $backlog); 

for ($i = 1; $i <= 500; $i++) {
    var_dump($i);
    stream_socket_client("tcp://127.0.0.1:8081");
}
anonymous
  • 121
  • 8
  • Sorry I'm able to comment just now. You're working hypothesis seems to work until I'm not checking if the connection was succesfull. If I listen to the "connected" event, I get no results using stream_socket_accept, while I get them without using it. Ah, Actually I'm using Ratchet as a server, maybe this could help – marcosh May 11 '15 at 18:23
  • I don't see how problem is relevant for Ratchet – anonymous May 13 '15 at 08:14
  • Ratchet uses ReactPhp server that is created with a stream_socket_server, and them I need to create php clients to connect to the server – marcosh May 13 '15 at 14:05
  • I meant - no apparent slowdown in Ratchet – anonymous May 13 '15 at 15:51