elad / node-cluster-socket.io

Writeup on how to make node.js cluster and socket.io play nice
421 stars 63 forks source link

Use farmhash in the example & benchmarks #27

Closed elithrar closed 7 years ago

elithrar commented 7 years ago

Google's farmhash (Node.js port) is an extremely fast hash with good quality, especially given that IPs don't tend to have an even distribution (private ranges, multicast, geographical allocation biases).

Running the benchmark gives good results and is faster than even a simple loop (due to the fact that FarmHash is written in C++):

~/Desktop/node-cluster-socket.io(master ✗) node benchmark 4
benchmarking int31...
  time (ms): 1057
  scatter: { '0': 249393, '1': 250495, '2': 250467, '3': 249645 }
benchmarking numeric_real...
  time (ms): 566
  scatter: { '0': 248780, '1': 250423, '2': 251212, '3': 249585 }
benchmarking simple_regex...
  time (ms): 307
  scatter: { '0': 247841, '1': 248563, '2': 252151, '3': 251445 }
benchmarking farmhash32...
  time (ms): 268
  scatter: { '0': 249802, '1': 249522, '2': 249922, '3': 250754 }
benchmarking simple_loop...
  time (ms): 532
  scatter: { '0': 247841, '1': 248563, '2': 252151, '3': 251445 }

Of course, this is nitpicking, but useful if you are dealing with high levels of traffic where Node's cluster package is most useful.

Provided this is OK, I can submit a PR w/ results + some prose to explain.

MickL commented 7 years ago

I tried it, too and farmhash seems to be the fastest thats for sure. Also using IPv6 none of the hash methods does deliver good results, except for farmhash.

IPv6
----------
benchmarking int31...
  time (ms): 417
  scatter: { '0': 568888, '1': 220419, '2': 94915, '3': 115778 }
benchmarking numeric_real...
  time (ms): 826
  scatter: { NaN: 1000000 }
benchmarking simple_regex...
  time (ms): 704
  scatter: { NaN: 1000000 }
benchmarking simple_loop...
  time (ms): 1292
  scatter: { '0': 891094, '1': 35005, '2': 38494, '3': 35407 }
benchmarking farmhash...
  time (ms): 316
  scatter: { '0': 250239, '1': 249119, '2': 250724, '3': 249918 }
elad commented 7 years ago

@MickL merged your pull request, thanks!