ghedipunk / PHP-Websockets

A Websockets server written in PHP.
BSD 3-Clause "New" or "Revised" License
913 stars 376 forks source link

Alternative for the current slow filling $effectiveMask #30

Open Xaraknid opened 9 years ago

Xaraknid commented 9 years ago

This is used inside applyMask() use to be able to decode what the client send to browser.

I try to optimize the code, i will pull the complete job once I fix an issue a have with something.

I made this benchtest quickly to test speed of different method to replace the current one where it become very slow, the larger the payload size are. I post here for the help of other person to test it and confirm what should be the method to replace the current one.

warning ! in it current config it take 25sec for the current method to process 64K payload of processing 10K frames. you can change at the start of the file the size of payload here the file https://github.com/Xaraknid/PHP-Websockets/blob/master/bench.php

allmost of the time str_repeat win. here my output time total / min / max / avg / nbloop ( inside a while ) / over (to be remove from effectiveMask) / check (integrity $effectiveMask match the payload size) / rps (potential request per second) / faster by ( number of time faster of slowest method)

Testing current
Testing optimized............
Testing optimizedv2.......
Testing str_repeat..................
Testing str_pad..................
array(5) {
  [0]=>
  array(10) {
    ["total"]=>
    float(0.08144474029541)
    ["name"]=>
    string(10) "str_repeat"
    ["min"]=>
    float(6.9141387939453E-6)
    ["max"]=>
    float(8.4877014160156E-5)
    ["nbloop"]=>
    int(0)
    ["over"]=>
    int(-1)
    ["check"]=>
    int(64123)
    ["avg"]=>
    float(8.144474029541E-6)
    ["rps"]=>
    float(122782.63720565)
    ["faster by "]=>
    float(310.68131520708)
  }
  [1]=>
  array(10) {
    ["total"]=>
    float(0.11458611488342)
    ["name"]=>
    string(11) "optimizedv2"
    ["min"]=>
    float(9.7751617431641E-6)
    ["max"]=>
    float(9.4175338745117E-5)
    ["nbloop"]=>
    int(14)
    ["over"]=>
    int(-1413)
    ["check"]=>
    int(64123)
    ["avg"]=>
    float(1.1458611488342E-5)
    ["rps"]=>
    float(87270.608748484)
    ["faster by "]=>
    float(220.82395460759)
  }
 [2]=>
  array(10) {
    ["total"]=>
    float(0.12534499168396)
    ["name"]=>
    string(9) "optimized"
    ["min"]=>
    float(1.0967254638672E-5)
    ["max"]=>
    float(8.2015991210938E-5)
    ["nbloop"]=>
    int(14)
    ["over"]=>
    int(1413)
    ["check"]=>
    int(64123)
    ["avg"]=>
    float(1.2534499168396E-5)
    ["rps"]=>
    float(79779.813023672)
    ["faster by "]=>
    float(201.8697252418)
  }
  [3]=>
  array(10) {
    ["total"]=>
    float(2.190878868103)
    ["name"]=>
    string(7) "str_pad"
    ["min"]=>
    float(0.00018501281738281)
    ["max"]=>
    float(0.00060606002807617)
    ["nbloop"]=>
    int(0)
    ["over"]=>
    int(0)
    ["check"]=>
    int(64123)
    ["avg"]=>
    float(0.0002190878868103)
    ["rps"]=>
    float(4564.3783166609)
    ["faster by "]=>
    float(11.549410330287)
  }
  [4]=>
  array(9) {
    ["total"]=>
    float(25.303359031677)
    ["name"]=>
    string(7) "current"
    ["min"]=>
    float(0.0023021697998047)
    ["max"]=>
    float(0.0070538520812988)
    ["nbloop"]=>
    int(16031)
    ["over"]=>
    int(1)
    ["check"]=>
    int(64123)
    ["avg"]=>
    float(0.0025303359031677)
    ["rps"]=>
    float(395.20444647215)
  }
}