Skip to content

Instantly share code, notes, and snippets.

@dario-l
Forked from Aaronontheweb/mumurhash.linq
Created February 18, 2024 18:22
Show Gist options
  • Save dario-l/c7faf9f7173b90a623f498b05a5f5464 to your computer and use it in GitHub Desktop.
Save dario-l/c7faf9f7173b90a623f498b05a5f5464 to your computer and use it in GitHub Desktop.
LINQPad Query to Visualize MurmurHash Distributions
// requires the `Akka` NuGet package to be installed in the query
int entitiesPerNodeFactor = 100;
List<string> nodes = Enumerable.Range(0, 10).Select(c => $"hostname-{c}").ToList();
Dictionary<string, int> entityToNodeAllocations = nodes.ToDictionary(c => c, k => 0);
var entityIds = Enumerable.Range(0, nodes.Count * entitiesPerNodeFactor).Select(e => $"entity-{e}");
foreach(var e in entityIds){
var hashCode = MurmurHash.StringHash(e);
var selectedNode = Math.Abs(hashCode % nodes.Count);
var nodeId = nodes[selectedNode];
entityToNodeAllocations[nodeId]++; // increase node alloc count
}
entityToNodeAllocations.Chart(c => c.Key, v => v.Value, LINQPad.Util.SeriesType.Pie).Dump();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment