github.com/titanous/docker@v1.4.1/pkg/truncindex/truncindex_test.go (about)

     1  package truncindex
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/utils"
     8  )
     9  
    10  // Test the behavior of TruncIndex, an index for querying IDs from a non-conflicting prefix.
    11  func TestTruncIndex(t *testing.T) {
    12  	ids := []string{}
    13  	index := NewTruncIndex(ids)
    14  	// Get on an empty index
    15  	if _, err := index.Get("foobar"); err == nil {
    16  		t.Fatal("Get on an empty index should return an error")
    17  	}
    18  
    19  	// Spaces should be illegal in an id
    20  	if err := index.Add("I have a space"); err == nil {
    21  		t.Fatalf("Adding an id with ' ' should return an error")
    22  	}
    23  
    24  	id := "99b36c2c326ccc11e726eee6ee78a0baf166ef96"
    25  	// Add an id
    26  	if err := index.Add(id); err != nil {
    27  		t.Fatal(err)
    28  	}
    29  
    30  	// Add an empty id (should fail)
    31  	if err := index.Add(""); err == nil {
    32  		t.Fatalf("Adding an empty id should return an error")
    33  	}
    34  
    35  	// Get a non-existing id
    36  	assertIndexGet(t, index, "abracadabra", "", true)
    37  	// Get an empty id
    38  	assertIndexGet(t, index, "", "", true)
    39  	// Get the exact id
    40  	assertIndexGet(t, index, id, id, false)
    41  	// The first letter should match
    42  	assertIndexGet(t, index, id[:1], id, false)
    43  	// The first half should match
    44  	assertIndexGet(t, index, id[:len(id)/2], id, false)
    45  	// The second half should NOT match
    46  	assertIndexGet(t, index, id[len(id)/2:], "", true)
    47  
    48  	id2 := id[:6] + "blabla"
    49  	// Add an id
    50  	if err := index.Add(id2); err != nil {
    51  		t.Fatal(err)
    52  	}
    53  	// Both exact IDs should work
    54  	assertIndexGet(t, index, id, id, false)
    55  	assertIndexGet(t, index, id2, id2, false)
    56  
    57  	// 6 characters or less should conflict
    58  	assertIndexGet(t, index, id[:6], "", true)
    59  	assertIndexGet(t, index, id[:4], "", true)
    60  	assertIndexGet(t, index, id[:1], "", true)
    61  
    62  	// 7 characters should NOT conflict
    63  	assertIndexGet(t, index, id[:7], id, false)
    64  	assertIndexGet(t, index, id2[:7], id2, false)
    65  
    66  	// Deleting a non-existing id should return an error
    67  	if err := index.Delete("non-existing"); err == nil {
    68  		t.Fatalf("Deleting a non-existing id should return an error")
    69  	}
    70  
    71  	// Deleting an empty id should return an error
    72  	if err := index.Delete(""); err == nil {
    73  		t.Fatal("Deleting an empty id should return an error")
    74  	}
    75  
    76  	// Deleting id2 should remove conflicts
    77  	if err := index.Delete(id2); err != nil {
    78  		t.Fatal(err)
    79  	}
    80  	// id2 should no longer work
    81  	assertIndexGet(t, index, id2, "", true)
    82  	assertIndexGet(t, index, id2[:7], "", true)
    83  	assertIndexGet(t, index, id2[:11], "", true)
    84  
    85  	// conflicts between id and id2 should be gone
    86  	assertIndexGet(t, index, id[:6], id, false)
    87  	assertIndexGet(t, index, id[:4], id, false)
    88  	assertIndexGet(t, index, id[:1], id, false)
    89  
    90  	// non-conflicting substrings should still not conflict
    91  	assertIndexGet(t, index, id[:7], id, false)
    92  	assertIndexGet(t, index, id[:15], id, false)
    93  	assertIndexGet(t, index, id, id, false)
    94  }
    95  
    96  func assertIndexGet(t *testing.T, index *TruncIndex, input, expectedResult string, expectError bool) {
    97  	if result, err := index.Get(input); err != nil && !expectError {
    98  		t.Fatalf("Unexpected error getting '%s': %s", input, err)
    99  	} else if err == nil && expectError {
   100  		t.Fatalf("Getting '%s' should return an error, not '%s'", input, result)
   101  	} else if result != expectedResult {
   102  		t.Fatalf("Getting '%s' returned '%s' instead of '%s'", input, result, expectedResult)
   103  	}
   104  }
   105  
   106  func BenchmarkTruncIndexAdd100(b *testing.B) {
   107  	var testSet []string
   108  	for i := 0; i < 100; i++ {
   109  		testSet = append(testSet, utils.GenerateRandomID())
   110  	}
   111  	b.ResetTimer()
   112  	for i := 0; i < b.N; i++ {
   113  		index := NewTruncIndex([]string{})
   114  		for _, id := range testSet {
   115  			if err := index.Add(id); err != nil {
   116  				b.Fatal(err)
   117  			}
   118  		}
   119  	}
   120  }
   121  
   122  func BenchmarkTruncIndexAdd250(b *testing.B) {
   123  	var testSet []string
   124  	for i := 0; i < 250; i++ {
   125  		testSet = append(testSet, utils.GenerateRandomID())
   126  	}
   127  	b.ResetTimer()
   128  	for i := 0; i < b.N; i++ {
   129  		index := NewTruncIndex([]string{})
   130  		for _, id := range testSet {
   131  			if err := index.Add(id); err != nil {
   132  				b.Fatal(err)
   133  			}
   134  		}
   135  	}
   136  }
   137  
   138  func BenchmarkTruncIndexAdd500(b *testing.B) {
   139  	var testSet []string
   140  	for i := 0; i < 500; i++ {
   141  		testSet = append(testSet, utils.GenerateRandomID())
   142  	}
   143  	b.ResetTimer()
   144  	for i := 0; i < b.N; i++ {
   145  		index := NewTruncIndex([]string{})
   146  		for _, id := range testSet {
   147  			if err := index.Add(id); err != nil {
   148  				b.Fatal(err)
   149  			}
   150  		}
   151  	}
   152  }
   153  
   154  func BenchmarkTruncIndexGet100(b *testing.B) {
   155  	var testSet []string
   156  	var testKeys []string
   157  	for i := 0; i < 100; i++ {
   158  		testSet = append(testSet, utils.GenerateRandomID())
   159  	}
   160  	index := NewTruncIndex([]string{})
   161  	for _, id := range testSet {
   162  		if err := index.Add(id); err != nil {
   163  			b.Fatal(err)
   164  		}
   165  		l := rand.Intn(12) + 12
   166  		testKeys = append(testKeys, id[:l])
   167  	}
   168  	b.ResetTimer()
   169  	for i := 0; i < b.N; i++ {
   170  		for _, id := range testKeys {
   171  			if res, err := index.Get(id); err != nil {
   172  				b.Fatal(res, err)
   173  			}
   174  		}
   175  	}
   176  }
   177  
   178  func BenchmarkTruncIndexGet250(b *testing.B) {
   179  	var testSet []string
   180  	var testKeys []string
   181  	for i := 0; i < 250; i++ {
   182  		testSet = append(testSet, utils.GenerateRandomID())
   183  	}
   184  	index := NewTruncIndex([]string{})
   185  	for _, id := range testSet {
   186  		if err := index.Add(id); err != nil {
   187  			b.Fatal(err)
   188  		}
   189  		l := rand.Intn(12) + 12
   190  		testKeys = append(testKeys, id[:l])
   191  	}
   192  	b.ResetTimer()
   193  	for i := 0; i < b.N; i++ {
   194  		for _, id := range testKeys {
   195  			if res, err := index.Get(id); err != nil {
   196  				b.Fatal(res, err)
   197  			}
   198  		}
   199  	}
   200  }
   201  
   202  func BenchmarkTruncIndexGet500(b *testing.B) {
   203  	var testSet []string
   204  	var testKeys []string
   205  	for i := 0; i < 500; i++ {
   206  		testSet = append(testSet, utils.GenerateRandomID())
   207  	}
   208  	index := NewTruncIndex([]string{})
   209  	for _, id := range testSet {
   210  		if err := index.Add(id); err != nil {
   211  			b.Fatal(err)
   212  		}
   213  		l := rand.Intn(12) + 12
   214  		testKeys = append(testKeys, id[:l])
   215  	}
   216  	b.ResetTimer()
   217  	for i := 0; i < b.N; i++ {
   218  		for _, id := range testKeys {
   219  			if res, err := index.Get(id); err != nil {
   220  				b.Fatal(res, err)
   221  			}
   222  		}
   223  	}
   224  }
   225  
   226  func BenchmarkTruncIndexDelete100(b *testing.B) {
   227  	var testSet []string
   228  	for i := 0; i < 100; i++ {
   229  		testSet = append(testSet, utils.GenerateRandomID())
   230  	}
   231  	b.ResetTimer()
   232  	for i := 0; i < b.N; i++ {
   233  		b.StopTimer()
   234  		index := NewTruncIndex([]string{})
   235  		for _, id := range testSet {
   236  			if err := index.Add(id); err != nil {
   237  				b.Fatal(err)
   238  			}
   239  		}
   240  		b.StartTimer()
   241  		for _, id := range testSet {
   242  			if err := index.Delete(id); err != nil {
   243  				b.Fatal(err)
   244  			}
   245  		}
   246  	}
   247  }
   248  
   249  func BenchmarkTruncIndexDelete250(b *testing.B) {
   250  	var testSet []string
   251  	for i := 0; i < 250; i++ {
   252  		testSet = append(testSet, utils.GenerateRandomID())
   253  	}
   254  	b.ResetTimer()
   255  	for i := 0; i < b.N; i++ {
   256  		b.StopTimer()
   257  		index := NewTruncIndex([]string{})
   258  		for _, id := range testSet {
   259  			if err := index.Add(id); err != nil {
   260  				b.Fatal(err)
   261  			}
   262  		}
   263  		b.StartTimer()
   264  		for _, id := range testSet {
   265  			if err := index.Delete(id); err != nil {
   266  				b.Fatal(err)
   267  			}
   268  		}
   269  	}
   270  }
   271  
   272  func BenchmarkTruncIndexDelete500(b *testing.B) {
   273  	var testSet []string
   274  	for i := 0; i < 500; i++ {
   275  		testSet = append(testSet, utils.GenerateRandomID())
   276  	}
   277  	b.ResetTimer()
   278  	for i := 0; i < b.N; i++ {
   279  		b.StopTimer()
   280  		index := NewTruncIndex([]string{})
   281  		for _, id := range testSet {
   282  			if err := index.Add(id); err != nil {
   283  				b.Fatal(err)
   284  			}
   285  		}
   286  		b.StartTimer()
   287  		for _, id := range testSet {
   288  			if err := index.Delete(id); err != nil {
   289  				b.Fatal(err)
   290  			}
   291  		}
   292  	}
   293  }
   294  
   295  func BenchmarkTruncIndexNew100(b *testing.B) {
   296  	var testSet []string
   297  	for i := 0; i < 100; i++ {
   298  		testSet = append(testSet, utils.GenerateRandomID())
   299  	}
   300  	b.ResetTimer()
   301  	for i := 0; i < b.N; i++ {
   302  		NewTruncIndex(testSet)
   303  	}
   304  }
   305  
   306  func BenchmarkTruncIndexNew250(b *testing.B) {
   307  	var testSet []string
   308  	for i := 0; i < 250; i++ {
   309  		testSet = append(testSet, utils.GenerateRandomID())
   310  	}
   311  	b.ResetTimer()
   312  	for i := 0; i < b.N; i++ {
   313  		NewTruncIndex(testSet)
   314  	}
   315  }
   316  
   317  func BenchmarkTruncIndexNew500(b *testing.B) {
   318  	var testSet []string
   319  	for i := 0; i < 500; i++ {
   320  		testSet = append(testSet, utils.GenerateRandomID())
   321  	}
   322  	b.ResetTimer()
   323  	for i := 0; i < b.N; i++ {
   324  		NewTruncIndex(testSet)
   325  	}
   326  }
   327  
   328  func BenchmarkTruncIndexAddGet100(b *testing.B) {
   329  	var testSet []string
   330  	var testKeys []string
   331  	for i := 0; i < 500; i++ {
   332  		id := utils.GenerateRandomID()
   333  		testSet = append(testSet, id)
   334  		l := rand.Intn(12) + 12
   335  		testKeys = append(testKeys, id[:l])
   336  	}
   337  	b.ResetTimer()
   338  	for i := 0; i < b.N; i++ {
   339  		index := NewTruncIndex([]string{})
   340  		for _, id := range testSet {
   341  			if err := index.Add(id); err != nil {
   342  				b.Fatal(err)
   343  			}
   344  		}
   345  		for _, id := range testKeys {
   346  			if res, err := index.Get(id); err != nil {
   347  				b.Fatal(res, err)
   348  			}
   349  		}
   350  	}
   351  }
   352  
   353  func BenchmarkTruncIndexAddGet250(b *testing.B) {
   354  	var testSet []string
   355  	var testKeys []string
   356  	for i := 0; i < 500; i++ {
   357  		id := utils.GenerateRandomID()
   358  		testSet = append(testSet, id)
   359  		l := rand.Intn(12) + 12
   360  		testKeys = append(testKeys, id[:l])
   361  	}
   362  	b.ResetTimer()
   363  	for i := 0; i < b.N; i++ {
   364  		index := NewTruncIndex([]string{})
   365  		for _, id := range testSet {
   366  			if err := index.Add(id); err != nil {
   367  				b.Fatal(err)
   368  			}
   369  		}
   370  		for _, id := range testKeys {
   371  			if res, err := index.Get(id); err != nil {
   372  				b.Fatal(res, err)
   373  			}
   374  		}
   375  	}
   376  }
   377  
   378  func BenchmarkTruncIndexAddGet500(b *testing.B) {
   379  	var testSet []string
   380  	var testKeys []string
   381  	for i := 0; i < 500; i++ {
   382  		id := utils.GenerateRandomID()
   383  		testSet = append(testSet, id)
   384  		l := rand.Intn(12) + 12
   385  		testKeys = append(testKeys, id[:l])
   386  	}
   387  	b.ResetTimer()
   388  	for i := 0; i < b.N; i++ {
   389  		index := NewTruncIndex([]string{})
   390  		for _, id := range testSet {
   391  			if err := index.Add(id); err != nil {
   392  				b.Fatal(err)
   393  			}
   394  		}
   395  		for _, id := range testKeys {
   396  			if res, err := index.Get(id); err != nil {
   397  				b.Fatal(res, err)
   398  			}
   399  		}
   400  	}
   401  }