github.com/codingfuture/orig-energi3@v0.8.4/swarm/pot/pot_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package pot
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"math/rand"
    24  	"runtime"
    25  	"sync"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/ethereum/go-ethereum/swarm/log"
    30  )
    31  
    32  const (
    33  	maxEachNeighbourTests = 420
    34  	maxEachNeighbour      = 420
    35  	maxSwap               = 420
    36  	maxSwapTests          = 420
    37  )
    38  
    39  // func init() {
    40  // 	log.Root().SetHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(false))))
    41  // }
    42  
    43  type testAddr struct {
    44  	a []byte
    45  	i int
    46  }
    47  
    48  func newTestAddr(s string, i int) *testAddr {
    49  	return &testAddr{NewAddressFromString(s), i}
    50  }
    51  
    52  func (a *testAddr) Address() []byte {
    53  	return a.a
    54  }
    55  
    56  func (a *testAddr) String() string {
    57  	return Label(a.a)
    58  }
    59  
    60  func randomTestAddr(n int, i int) *testAddr {
    61  	v := RandomAddress().Bin()[:n]
    62  	return newTestAddr(v, i)
    63  }
    64  
    65  func randomtestAddr(n int, i int) *testAddr {
    66  	v := RandomAddress().Bin()[:n]
    67  	return newTestAddr(v, i)
    68  }
    69  
    70  func indexes(t *Pot) (i []int) {
    71  	t.Each(func(v Val) bool {
    72  		a := v.(*testAddr)
    73  		i = append(i, a.i)
    74  		return true
    75  	})
    76  	return i
    77  }
    78  
    79  func testAdd(t *Pot, pof Pof, j int, values ...string) (_ *Pot, n int, f bool) {
    80  	for i, val := range values {
    81  		t, n, f = Add(t, newTestAddr(val, i+j), pof)
    82  	}
    83  	return t, n, f
    84  }
    85  
    86  // removing non-existing element from pot
    87  func TestPotRemoveNonExisting(t *testing.T) {
    88  	pof := DefaultPof(8)
    89  	n := NewPot(newTestAddr("00111100", 0), 0)
    90  	n, _, _ = Remove(n, newTestAddr("00000101", 0), pof)
    91  	exp := "00111100"
    92  	got := Label(n.Pin())
    93  	if got[:8] != exp {
    94  		t.Fatalf("incorrect pinned value. Expected %v, got %v", exp, got[:8])
    95  	}
    96  }
    97  
    98  // this test creates hierarchical pot tree, and therefore any child node will have
    99  // child_po = parent_po + 1.
   100  // then removes a node from the middle of the tree.
   101  func TestPotRemoveSameBin(t *testing.T) {
   102  	pof := DefaultPof(8)
   103  	n := NewPot(newTestAddr("11111111", 0), 0)
   104  	n, _, _ = testAdd(n, pof, 1, "00000000", "01000000", "01100000", "01110000", "01111000")
   105  	n, _, _ = Remove(n, newTestAddr("01110000", 0), pof)
   106  	inds := indexes(n)
   107  	goti := n.Size()
   108  	expi := 5
   109  	if goti != expi {
   110  		t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
   111  	}
   112  	inds = indexes(n)
   113  	got := fmt.Sprintf("%v", inds)
   114  	exp := "[5 3 2 1 0]"
   115  	if got != exp {
   116  		t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
   117  	}
   118  }
   119  
   120  // this test creates a flat pot tree (all the elements are leafs of one root),
   121  // and therefore they all have the same po.
   122  // then removes an arbitrary element from the pot.
   123  func TestPotRemoveDifferentBins(t *testing.T) {
   124  	pof := DefaultPof(8)
   125  	n := NewPot(newTestAddr("11111111", 0), 0)
   126  	n, _, _ = testAdd(n, pof, 1, "00000000", "10000000", "11000000", "11100000", "11110000")
   127  	n, _, _ = Remove(n, newTestAddr("11100000", 0), pof)
   128  	inds := indexes(n)
   129  	goti := n.Size()
   130  	expi := 5
   131  	if goti != expi {
   132  		t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
   133  	}
   134  	inds = indexes(n)
   135  	got := fmt.Sprintf("%v", inds)
   136  	exp := "[1 2 3 5 0]"
   137  	if got != exp {
   138  		t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
   139  	}
   140  	n, _, _ = testAdd(n, pof, 4, "11100000")
   141  	inds = indexes(n)
   142  	got = fmt.Sprintf("%v", inds)
   143  	exp = "[1 2 3 4 5 0]"
   144  	if got != exp {
   145  		t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
   146  	}
   147  }
   148  
   149  func TestPotAdd(t *testing.T) {
   150  	pof := DefaultPof(8)
   151  	n := NewPot(newTestAddr("00111100", 0), 0)
   152  	// Pin set correctly
   153  	exp := "00111100"
   154  	got := Label(n.Pin())[:8]
   155  	if got != exp {
   156  		t.Fatalf("incorrect pinned value. Expected %v, got %v", exp, got)
   157  	}
   158  	// check size
   159  	goti := n.Size()
   160  	expi := 1
   161  	if goti != expi {
   162  		t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
   163  	}
   164  
   165  	n, _, _ = testAdd(n, pof, 1, "01111100", "00111100", "01111100", "00011100")
   166  	// check size
   167  	goti = n.Size()
   168  	expi = 3
   169  	if goti != expi {
   170  		t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
   171  	}
   172  	inds := indexes(n)
   173  	got = fmt.Sprintf("%v", inds)
   174  	exp = "[3 4 2]"
   175  	if got != exp {
   176  		t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
   177  	}
   178  }
   179  
   180  func TestPotRemove(t *testing.T) {
   181  	pof := DefaultPof(8)
   182  	n := NewPot(newTestAddr("00111100", 0), 0)
   183  	n, _, _ = Remove(n, newTestAddr("00111100", 0), pof)
   184  	exp := "<nil>"
   185  	got := Label(n.Pin())
   186  	if got != exp {
   187  		t.Fatalf("incorrect pinned value. Expected %v, got %v", exp, got)
   188  	}
   189  	n, _, _ = testAdd(n, pof, 1, "00000000", "01111100", "00111100", "00011100")
   190  	n, _, _ = Remove(n, newTestAddr("00111100", 0), pof)
   191  	goti := n.Size()
   192  	expi := 3
   193  	if goti != expi {
   194  		t.Fatalf("incorrect number of elements in Pot. Expected %v, got %v", expi, goti)
   195  	}
   196  	inds := indexes(n)
   197  	got = fmt.Sprintf("%v", inds)
   198  	exp = "[2 4 1]"
   199  	if got != exp {
   200  		t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
   201  	}
   202  	n, _, _ = Remove(n, newTestAddr("00111100", 0), pof) // remove again same element
   203  	inds = indexes(n)
   204  	got = fmt.Sprintf("%v", inds)
   205  	if got != exp {
   206  		t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
   207  	}
   208  	n, _, _ = Remove(n, newTestAddr("00000000", 0), pof) // remove the first element
   209  	inds = indexes(n)
   210  	got = fmt.Sprintf("%v", inds)
   211  	exp = "[2 4]"
   212  	if got != exp {
   213  		t.Fatalf("incorrect indexes in iteration over Pot. Expected %v, got %v", exp, got)
   214  	}
   215  }
   216  
   217  func TestPotSwap(t *testing.T) {
   218  	for i := 0; i < maxSwapTests; i++ {
   219  		alen := maxkeylen
   220  		pof := DefaultPof(alen)
   221  		max := rand.Intn(maxSwap)
   222  
   223  		n := NewPot(nil, 0)
   224  		var m []*testAddr
   225  		var found bool
   226  		for j := 0; j < 2*max; {
   227  			v := randomtestAddr(alen, j)
   228  			n, _, found = Add(n, v, pof)
   229  			if !found {
   230  				m = append(m, v)
   231  				j++
   232  			}
   233  		}
   234  		k := make(map[string]*testAddr)
   235  		for j := 0; j < max; {
   236  			v := randomtestAddr(alen, 1)
   237  			_, found := k[Label(v)]
   238  			if !found {
   239  				k[Label(v)] = v
   240  				j++
   241  			}
   242  		}
   243  		for _, v := range k {
   244  			m = append(m, v)
   245  		}
   246  		f := func(v Val) Val {
   247  			tv := v.(*testAddr)
   248  			if tv.i < max {
   249  				return nil
   250  			}
   251  			tv.i = 0
   252  			return v
   253  		}
   254  		for _, val := range m {
   255  			n, _, _, _ = Swap(n, val, pof, func(v Val) Val {
   256  				if v == nil {
   257  					return val
   258  				}
   259  				return f(v)
   260  			})
   261  		}
   262  		sum := 0
   263  		n.Each(func(v Val) bool {
   264  			if v == nil {
   265  				return true
   266  			}
   267  			sum++
   268  			tv := v.(*testAddr)
   269  			if tv.i > 1 {
   270  				t.Fatalf("item value incorrect, expected 0, got %v", tv.i)
   271  			}
   272  			return true
   273  		})
   274  		if sum != 2*max {
   275  			t.Fatalf("incorrect number of elements. expected %v, got %v", 2*max, sum)
   276  		}
   277  		if sum != n.Size() {
   278  			t.Fatalf("incorrect size. expected %v, got %v", sum, n.Size())
   279  		}
   280  	}
   281  }
   282  
   283  func checkPo(val Val, pof Pof) func(Val, int) error {
   284  	return func(v Val, po int) error {
   285  		// check the po
   286  		exp, _ := pof(val, v, 0)
   287  		if po != exp {
   288  			return fmt.Errorf("incorrect prox order for item %v in neighbour iteration for %v. Expected %v, got %v", v, val, exp, po)
   289  		}
   290  		return nil
   291  	}
   292  }
   293  
   294  func checkOrder(val Val) func(Val, int) error {
   295  	po := maxkeylen
   296  	return func(v Val, p int) error {
   297  		if po < p {
   298  			return fmt.Errorf("incorrect order for item %v in neighbour iteration for %v. PO %v > %v (previous max)", v, val, p, po)
   299  		}
   300  		po = p
   301  		return nil
   302  	}
   303  }
   304  
   305  func checkValues(m map[string]bool, val Val) func(Val, int) error {
   306  	return func(v Val, po int) error {
   307  		duplicate, ok := m[Label(v)]
   308  		if !ok {
   309  			return fmt.Errorf("alien value %v", v)
   310  		}
   311  		if duplicate {
   312  			return fmt.Errorf("duplicate value returned: %v", v)
   313  		}
   314  		m[Label(v)] = true
   315  		return nil
   316  	}
   317  }
   318  
   319  var errNoCount = errors.New("not count")
   320  
   321  func testPotEachNeighbour(n *Pot, pof Pof, val Val, expCount int, fs ...func(Val, int) error) error {
   322  	var err error
   323  	var count int
   324  	n.EachNeighbour(val, pof, func(v Val, po int) bool {
   325  		for _, f := range fs {
   326  			err = f(v, po)
   327  			if err != nil {
   328  				return err.Error() == errNoCount.Error()
   329  			}
   330  		}
   331  		count++
   332  		return count != expCount
   333  	})
   334  	if err == nil && count < expCount {
   335  		return fmt.Errorf("not enough neighbours returned, expected %v, got %v", expCount, count)
   336  	}
   337  	return err
   338  }
   339  
   340  const (
   341  	mergeTestCount  = 5
   342  	mergeTestChoose = 5
   343  )
   344  
   345  func TestPotMergeCommon(t *testing.T) {
   346  	vs := make([]*testAddr, mergeTestCount)
   347  	for i := 0; i < maxEachNeighbourTests; i++ {
   348  		alen := maxkeylen
   349  		pof := DefaultPof(alen)
   350  
   351  		for j := 0; j < len(vs); j++ {
   352  			vs[j] = randomtestAddr(alen, j)
   353  		}
   354  		max0 := rand.Intn(mergeTestChoose) + 1
   355  		max1 := rand.Intn(mergeTestChoose) + 1
   356  		n0 := NewPot(nil, 0)
   357  		n1 := NewPot(nil, 0)
   358  		log.Trace(fmt.Sprintf("round %v: %v - %v", i, max0, max1))
   359  		m := make(map[string]bool)
   360  		var found bool
   361  		for j := 0; j < max0; {
   362  			r := rand.Intn(max0)
   363  			v := vs[r]
   364  			n0, _, found = Add(n0, v, pof)
   365  			if !found {
   366  				m[Label(v)] = false
   367  				j++
   368  			}
   369  		}
   370  		expAdded := 0
   371  
   372  		for j := 0; j < max1; {
   373  			r := rand.Intn(max1)
   374  			v := vs[r]
   375  			n1, _, found = Add(n1, v, pof)
   376  			if !found {
   377  				j++
   378  			}
   379  			_, found = m[Label(v)]
   380  			if !found {
   381  				expAdded++
   382  				m[Label(v)] = false
   383  			}
   384  		}
   385  		if i < 6 {
   386  			continue
   387  		}
   388  		expSize := len(m)
   389  		log.Trace(fmt.Sprintf("%v-0: pin: %v, size: %v", i, n0.Pin(), max0))
   390  		log.Trace(fmt.Sprintf("%v-1: pin: %v, size: %v", i, n1.Pin(), max1))
   391  		log.Trace(fmt.Sprintf("%v: merged tree size: %v, newly added: %v", i, expSize, expAdded))
   392  		n, common := Union(n0, n1, pof)
   393  		added := n1.Size() - common
   394  		size := n.Size()
   395  
   396  		if expSize != size {
   397  			t.Fatalf("%v: incorrect number of elements in merged pot, expected %v, got %v\n%v", i, expSize, size, n)
   398  		}
   399  		if expAdded != added {
   400  			t.Fatalf("%v: incorrect number of added elements in merged pot, expected %v, got %v", i, expAdded, added)
   401  		}
   402  		if !checkDuplicates(n) {
   403  			t.Fatalf("%v: merged pot contains duplicates: \n%v", i, n)
   404  		}
   405  		for k := range m {
   406  			_, _, found = Add(n, newTestAddr(k, 0), pof)
   407  			if !found {
   408  				t.Fatalf("%v: merged pot (size:%v, added: %v) missing element %v", i, size, added, k)
   409  			}
   410  		}
   411  	}
   412  }
   413  
   414  func TestPotMergeScale(t *testing.T) {
   415  	for i := 0; i < maxEachNeighbourTests; i++ {
   416  		alen := maxkeylen
   417  		pof := DefaultPof(alen)
   418  		max0 := rand.Intn(maxEachNeighbour) + 1
   419  		max1 := rand.Intn(maxEachNeighbour) + 1
   420  		n0 := NewPot(nil, 0)
   421  		n1 := NewPot(nil, 0)
   422  		log.Trace(fmt.Sprintf("round %v: %v - %v", i, max0, max1))
   423  		m := make(map[string]bool)
   424  		var found bool
   425  		for j := 0; j < max0; {
   426  			v := randomtestAddr(alen, j)
   427  			n0, _, found = Add(n0, v, pof)
   428  			if !found {
   429  				m[Label(v)] = false
   430  				j++
   431  			}
   432  		}
   433  		expAdded := 0
   434  
   435  		for j := 0; j < max1; {
   436  			v := randomtestAddr(alen, j)
   437  			n1, _, found = Add(n1, v, pof)
   438  			if !found {
   439  				j++
   440  			}
   441  			_, found = m[Label(v)]
   442  			if !found {
   443  				expAdded++
   444  				m[Label(v)] = false
   445  			}
   446  		}
   447  		if i < 6 {
   448  			continue
   449  		}
   450  		expSize := len(m)
   451  		log.Trace(fmt.Sprintf("%v-0: pin: %v, size: %v", i, n0.Pin(), max0))
   452  		log.Trace(fmt.Sprintf("%v-1: pin: %v, size: %v", i, n1.Pin(), max1))
   453  		log.Trace(fmt.Sprintf("%v: merged tree size: %v, newly added: %v", i, expSize, expAdded))
   454  		n, common := Union(n0, n1, pof)
   455  		added := n1.Size() - common
   456  		size := n.Size()
   457  
   458  		if expSize != size {
   459  			t.Fatalf("%v: incorrect number of elements in merged pot, expected %v, got %v", i, expSize, size)
   460  		}
   461  		if expAdded != added {
   462  			t.Fatalf("%v: incorrect number of added elements in merged pot, expected %v, got %v", i, expAdded, added)
   463  		}
   464  		if !checkDuplicates(n) {
   465  			t.Fatalf("%v: merged pot contains duplicates", i)
   466  		}
   467  		for k := range m {
   468  			_, _, found = Add(n, newTestAddr(k, 0), pof)
   469  			if !found {
   470  				t.Fatalf("%v: merged pot (size:%v, added: %v) missing element %v", i, size, added, k)
   471  			}
   472  		}
   473  	}
   474  }
   475  
   476  func checkDuplicates(t *Pot) bool {
   477  	po := -1
   478  	for _, c := range t.bins {
   479  		if c == nil {
   480  			return false
   481  		}
   482  		if c.po <= po || !checkDuplicates(c) {
   483  			return false
   484  		}
   485  		po = c.po
   486  	}
   487  	return true
   488  }
   489  
   490  func TestPotEachNeighbourSync(t *testing.T) {
   491  	for i := 0; i < maxEachNeighbourTests; i++ {
   492  		alen := maxkeylen
   493  		pof := DefaultPof(maxkeylen)
   494  		max := rand.Intn(maxEachNeighbour/2) + maxEachNeighbour/2
   495  		pin := randomTestAddr(alen, 0)
   496  		n := NewPot(pin, 0)
   497  		m := make(map[string]bool)
   498  		m[Label(pin)] = false
   499  		for j := 1; j <= max; j++ {
   500  			v := randomTestAddr(alen, j)
   501  			n, _, _ = Add(n, v, pof)
   502  			m[Label(v)] = false
   503  		}
   504  
   505  		size := n.Size()
   506  		if size < 2 {
   507  			continue
   508  		}
   509  		count := rand.Intn(size/2) + size/2
   510  		val := randomTestAddr(alen, max+1)
   511  		log.Trace(fmt.Sprintf("%v: pin: %v, size: %v, val: %v, count: %v", i, n.Pin(), size, val, count))
   512  		err := testPotEachNeighbour(n, pof, val, count, checkPo(val, pof), checkOrder(val), checkValues(m, val))
   513  		if err != nil {
   514  			t.Fatal(err)
   515  		}
   516  		minPoFound := alen
   517  		maxPoNotFound := 0
   518  		for k, found := range m {
   519  			po, _ := pof(val, newTestAddr(k, 0), 0)
   520  			if found {
   521  				if po < minPoFound {
   522  					minPoFound = po
   523  				}
   524  			} else {
   525  				if po > maxPoNotFound {
   526  					maxPoNotFound = po
   527  				}
   528  			}
   529  		}
   530  		if minPoFound < maxPoNotFound {
   531  			t.Fatalf("incorrect neighbours returned: found one with PO %v < there was one not found with PO %v", minPoFound, maxPoNotFound)
   532  		}
   533  	}
   534  }
   535  
   536  func TestPotEachNeighbourAsync(t *testing.T) {
   537  	for i := 0; i < maxEachNeighbourTests; i++ {
   538  		max := rand.Intn(maxEachNeighbour/2) + maxEachNeighbour/2
   539  		alen := maxkeylen
   540  		pof := DefaultPof(alen)
   541  		n := NewPot(randomTestAddr(alen, 0), 0)
   542  		size := 1
   543  		var found bool
   544  		for j := 1; j <= max; j++ {
   545  			v := randomTestAddr(alen, j)
   546  			n, _, found = Add(n, v, pof)
   547  			if !found {
   548  				size++
   549  			}
   550  		}
   551  		if size != n.Size() {
   552  			t.Fatal(n)
   553  		}
   554  		if size < 2 {
   555  			continue
   556  		}
   557  		count := rand.Intn(size/2) + size/2
   558  		val := randomTestAddr(alen, max+1)
   559  
   560  		mu := sync.Mutex{}
   561  		m := make(map[string]bool)
   562  		maxPos := rand.Intn(alen)
   563  		log.Trace(fmt.Sprintf("%v: pin: %v, size: %v, val: %v, count: %v, maxPos: %v", i, n.Pin(), size, val, count, maxPos))
   564  		msize := 0
   565  		remember := func(v Val, po int) error {
   566  			if po > maxPos {
   567  				return errNoCount
   568  			}
   569  			m[Label(v)] = true
   570  			msize++
   571  			return nil
   572  		}
   573  		if i == 0 {
   574  			continue
   575  		}
   576  		testPotEachNeighbour(n, pof, val, count, remember)
   577  		d := 0
   578  		forget := func(v Val, po int) {
   579  			mu.Lock()
   580  			defer mu.Unlock()
   581  			d++
   582  			delete(m, Label(v))
   583  		}
   584  
   585  		n.EachNeighbourAsync(val, pof, count, maxPos, forget, true)
   586  		if d != msize {
   587  			t.Fatalf("incorrect number of neighbour calls in async iterator. expected %v, got %v", msize, d)
   588  		}
   589  		if len(m) != 0 {
   590  			t.Fatalf("incorrect neighbour calls in async iterator. %v items missed:\n%v", len(m), n)
   591  		}
   592  	}
   593  }
   594  
   595  func benchmarkEachNeighbourSync(t *testing.B, max, count int, d time.Duration) {
   596  	t.ReportAllocs()
   597  	alen := maxkeylen
   598  	pof := DefaultPof(alen)
   599  	pin := randomTestAddr(alen, 0)
   600  	n := NewPot(pin, 0)
   601  	var found bool
   602  	for j := 1; j <= max; {
   603  		v := randomTestAddr(alen, j)
   604  		n, _, found = Add(n, v, pof)
   605  		if !found {
   606  			j++
   607  		}
   608  	}
   609  	t.ResetTimer()
   610  	for i := 0; i < t.N; i++ {
   611  		val := randomTestAddr(alen, max+1)
   612  		m := 0
   613  		n.EachNeighbour(val, pof, func(v Val, po int) bool {
   614  			time.Sleep(d)
   615  			m++
   616  			return m != count
   617  		})
   618  	}
   619  	t.StopTimer()
   620  	stats := new(runtime.MemStats)
   621  	runtime.ReadMemStats(stats)
   622  }
   623  
   624  func benchmarkEachNeighbourAsync(t *testing.B, max, count int, d time.Duration) {
   625  	t.ReportAllocs()
   626  	alen := maxkeylen
   627  	pof := DefaultPof(alen)
   628  	pin := randomTestAddr(alen, 0)
   629  	n := NewPot(pin, 0)
   630  	var found bool
   631  	for j := 1; j <= max; {
   632  		v := randomTestAddr(alen, j)
   633  		n, _, found = Add(n, v, pof)
   634  		if !found {
   635  			j++
   636  		}
   637  	}
   638  	t.ResetTimer()
   639  	for i := 0; i < t.N; i++ {
   640  		val := randomTestAddr(alen, max+1)
   641  		n.EachNeighbourAsync(val, pof, count, alen, func(v Val, po int) {
   642  			time.Sleep(d)
   643  		}, true)
   644  	}
   645  	t.StopTimer()
   646  	stats := new(runtime.MemStats)
   647  	runtime.ReadMemStats(stats)
   648  }
   649  
   650  func BenchmarkEachNeighbourSync_3_1_0(t *testing.B) {
   651  	benchmarkEachNeighbourSync(t, 1000, 10, 1*time.Microsecond)
   652  }
   653  func BenchmarkEachNeighboursAsync_3_1_0(t *testing.B) {
   654  	benchmarkEachNeighbourAsync(t, 1000, 10, 1*time.Microsecond)
   655  }
   656  func BenchmarkEachNeighbourSync_3_2_0(t *testing.B) {
   657  	benchmarkEachNeighbourSync(t, 1000, 100, 1*time.Microsecond)
   658  }
   659  func BenchmarkEachNeighboursAsync_3_2_0(t *testing.B) {
   660  	benchmarkEachNeighbourAsync(t, 1000, 100, 1*time.Microsecond)
   661  }
   662  func BenchmarkEachNeighbourSync_3_3_0(t *testing.B) {
   663  	benchmarkEachNeighbourSync(t, 1000, 1000, 1*time.Microsecond)
   664  }
   665  func BenchmarkEachNeighboursAsync_3_3_0(t *testing.B) {
   666  	benchmarkEachNeighbourAsync(t, 1000, 1000, 1*time.Microsecond)
   667  }
   668  
   669  func BenchmarkEachNeighbourSync_3_1_1(t *testing.B) {
   670  	benchmarkEachNeighbourSync(t, 1000, 10, 2*time.Microsecond)
   671  }
   672  func BenchmarkEachNeighboursAsync_3_1_1(t *testing.B) {
   673  	benchmarkEachNeighbourAsync(t, 1000, 10, 2*time.Microsecond)
   674  }
   675  func BenchmarkEachNeighbourSync_3_2_1(t *testing.B) {
   676  	benchmarkEachNeighbourSync(t, 1000, 100, 2*time.Microsecond)
   677  }
   678  func BenchmarkEachNeighboursAsync_3_2_1(t *testing.B) {
   679  	benchmarkEachNeighbourAsync(t, 1000, 100, 2*time.Microsecond)
   680  }
   681  func BenchmarkEachNeighbourSync_3_3_1(t *testing.B) {
   682  	benchmarkEachNeighbourSync(t, 1000, 1000, 2*time.Microsecond)
   683  }
   684  func BenchmarkEachNeighboursAsync_3_3_1(t *testing.B) {
   685  	benchmarkEachNeighbourAsync(t, 1000, 1000, 2*time.Microsecond)
   686  }
   687  
   688  func BenchmarkEachNeighbourSync_3_1_2(t *testing.B) {
   689  	benchmarkEachNeighbourSync(t, 1000, 10, 4*time.Microsecond)
   690  }
   691  func BenchmarkEachNeighboursAsync_3_1_2(t *testing.B) {
   692  	benchmarkEachNeighbourAsync(t, 1000, 10, 4*time.Microsecond)
   693  }
   694  func BenchmarkEachNeighbourSync_3_2_2(t *testing.B) {
   695  	benchmarkEachNeighbourSync(t, 1000, 100, 4*time.Microsecond)
   696  }
   697  func BenchmarkEachNeighboursAsync_3_2_2(t *testing.B) {
   698  	benchmarkEachNeighbourAsync(t, 1000, 100, 4*time.Microsecond)
   699  }
   700  func BenchmarkEachNeighbourSync_3_3_2(t *testing.B) {
   701  	benchmarkEachNeighbourSync(t, 1000, 1000, 4*time.Microsecond)
   702  }
   703  func BenchmarkEachNeighboursAsync_3_3_2(t *testing.B) {
   704  	benchmarkEachNeighbourAsync(t, 1000, 1000, 4*time.Microsecond)
   705  }
   706  
   707  func BenchmarkEachNeighbourSync_3_1_3(t *testing.B) {
   708  	benchmarkEachNeighbourSync(t, 1000, 10, 8*time.Microsecond)
   709  }
   710  func BenchmarkEachNeighboursAsync_3_1_3(t *testing.B) {
   711  	benchmarkEachNeighbourAsync(t, 1000, 10, 8*time.Microsecond)
   712  }
   713  func BenchmarkEachNeighbourSync_3_2_3(t *testing.B) {
   714  	benchmarkEachNeighbourSync(t, 1000, 100, 8*time.Microsecond)
   715  }
   716  func BenchmarkEachNeighboursAsync_3_2_3(t *testing.B) {
   717  	benchmarkEachNeighbourAsync(t, 1000, 100, 8*time.Microsecond)
   718  }
   719  func BenchmarkEachNeighbourSync_3_3_3(t *testing.B) {
   720  	benchmarkEachNeighbourSync(t, 1000, 1000, 8*time.Microsecond)
   721  }
   722  func BenchmarkEachNeighboursAsync_3_3_3(t *testing.B) {
   723  	benchmarkEachNeighbourAsync(t, 1000, 1000, 8*time.Microsecond)
   724  }
   725  
   726  func BenchmarkEachNeighbourSync_3_1_4(t *testing.B) {
   727  	benchmarkEachNeighbourSync(t, 1000, 10, 16*time.Microsecond)
   728  }
   729  func BenchmarkEachNeighboursAsync_3_1_4(t *testing.B) {
   730  	benchmarkEachNeighbourAsync(t, 1000, 10, 16*time.Microsecond)
   731  }
   732  func BenchmarkEachNeighbourSync_3_2_4(t *testing.B) {
   733  	benchmarkEachNeighbourSync(t, 1000, 100, 16*time.Microsecond)
   734  }
   735  func BenchmarkEachNeighboursAsync_3_2_4(t *testing.B) {
   736  	benchmarkEachNeighbourAsync(t, 1000, 100, 16*time.Microsecond)
   737  }
   738  func BenchmarkEachNeighbourSync_3_3_4(t *testing.B) {
   739  	benchmarkEachNeighbourSync(t, 1000, 1000, 16*time.Microsecond)
   740  }
   741  func BenchmarkEachNeighboursAsync_3_3_4(t *testing.B) {
   742  	benchmarkEachNeighbourAsync(t, 1000, 1000, 16*time.Microsecond)
   743  }