<commit_before>package tests

import (
  "github.com/lxfontes/roxter"
  "testing"
)

func Test_RingStatic(t *testing.T) {
  r := roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.AddNode("node3", 1)
  r.Bake()

  // these are statically mapped and totally random
  k1 := r.Hash("key1")
  k2 := r.Hash("keyd")
  k3 := r.Hash("key2")

  if k1 != "node1" {
    t.Fatal("Invalid node assignment: k1")
  }

  if k2 != "node2" {
    t.Fatal("Invalid node assignment: k2")
  }

  if k3 != "node3" {
    t.Fatal("Invalid node assignment: k3")
  }

}

func Test_RingConsistency(t *testing.T) {
  r := roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.AddNode("node3", 1)
  r.Bake()

  // these are statically mapped and totally random
  k1 := r.Hash("key1")
  k2 := r.Hash("keyd")
  k3 := r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node3" {
    t.Fatal("Consistency failed: first pass")
  }

  //adding nodes out of order, but keeping naming consistent
  r = roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node3", 1)
  r.AddNode("node2", 1)
  r.Bake()

  k1 = r.Hash("key1")
  k2 = r.Hash("keyd")
  k3 = r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node3" {
    t.Fatal("Consistency failed: second pass", k1, k2, k3)
  }

}

func Test_RingRebalance(t *testing.T) {
  r := roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.AddNode("node3", 1)
  r.Bake()

  // these are statically mapped and totally random
  k1 := r.Hash("key1")
  k2 := r.Hash("keyd")
  k3 := r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node3" {
    t.Fatal("Rebalance failed: first pass")
  }

  //adding nodes out of order, but keeping naming consistent
  r = roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.Bake()

  k1 = r.Hash("key1")
  k2 = r.Hash("keyd")
  //k3 should rebalance
  k3 = r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node1" {
    t.Fatal("Rebalance failed: second pass", k1, k2, k3)
  }

}
<commit_msg>look at that, guess ketama might be too slow !!<commit_after>package tests

import (
  "fmt"
  "github.com/lxfontes/roxter"
  "github.com/stathat/consistent"
  "testing"
)

func Test_RingStatic(t *testing.T) {
  r := roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.AddNode("node3", 1)
  r.Bake()

  // these are statically mapped and totally random
  k1 := r.Hash("key1")
  k2 := r.Hash("keyd")
  k3 := r.Hash("key2")

  if k1 != "node1" {
    t.Fatal("Invalid node assignment: k1")
  }

  if k2 != "node2" {
    t.Fatal("Invalid node assignment: k2")
  }

  if k3 != "node3" {
    t.Fatal("Invalid node assignment: k3")
  }

}

func Test_RingConsistency(t *testing.T) {
  r := roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.AddNode("node3", 1)
  r.Bake()

  // these are statically mapped and totally random
  k1 := r.Hash("key1")
  k2 := r.Hash("keyd")
  k3 := r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node3" {
    t.Fatal("Consistency failed: first pass")
  }

  //adding nodes out of order, but keeping naming consistent
  r = roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node3", 1)
  r.AddNode("node2", 1)
  r.Bake()

  k1 = r.Hash("key1")
  k2 = r.Hash("keyd")
  k3 = r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node3" {
    t.Fatal("Consistency failed: second pass", k1, k2, k3)
  }

}

func Test_RingRebalance(t *testing.T) {
  r := roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.AddNode("node3", 1)
  r.Bake()

  // these are statically mapped and totally random
  k1 := r.Hash("key1")
  k2 := r.Hash("keyd")
  k3 := r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node3" {
    t.Fatal("Rebalance failed: first pass")
  }

  //adding nodes out of order, but keeping naming consistent
  r = roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.Bake()

  k1 = r.Hash("key1")
  k2 = r.Hash("keyd")
  //k3 should rebalance
  k3 = r.Hash("key2")

  if k1 != "node1" || k2 != "node2" || k3 != "node1" {
    t.Fatal("Rebalance failed: second pass", k1, k2, k3)
  }

}

func Benchmark_Ketama(b *testing.B) {

  r := roxter.NewRing(100)
  r.AddNode("node1", 1)
  r.AddNode("node2", 1)
  r.AddNode("node3", 1)
  r.Bake()

  for i := 0; i < b.N; i++ {
    r.Hash(fmt.Sprint("hash", i))
  }

}

func Benchmark_StatHat(b *testing.B) {
  c := consistent.New()
  c.Add("node1")
  c.Add("node2")
  c.Add("node3")

  for n := 0; n < b.N; n++ {
    b.Log(c.Get(fmt.Sprint("hash", n)))
  }
}

{'fraction_non_alphanumeric': 0.1992595818815331, 'fraction_numerical': 0.05008710801393728, 'mean_word_length': 3.0538393645189763, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1265268', 'n_tokens_mistral': 2129, 'n_tokens_neox': 1995, 'n_words': 447}