1package deb 2 3import ( 4 "fmt" 5 "os" 6 "testing" 7 8 "github.com/aptly-dev/aptly/database" 9) 10 11func BenchmarkSnapshotCollectionForEach(b *testing.B) { 12 const count = 1024 13 14 tmpDir := os.TempDir() 15 defer os.RemoveAll(tmpDir) 16 17 db, _ := database.NewOpenDB(tmpDir) 18 defer db.Close() 19 20 collection := NewSnapshotCollection(db) 21 22 for i := 0; i < count; i++ { 23 snapshot := NewSnapshotFromRefList(fmt.Sprintf("snapshot%d", i), nil, NewPackageRefList(), fmt.Sprintf("Snapshot number %d", i)) 24 if collection.Add(snapshot) != nil { 25 b.FailNow() 26 } 27 } 28 29 b.ResetTimer() 30 31 for i := 0; i < b.N; i++ { 32 collection = NewSnapshotCollection(db) 33 34 collection.ForEach(func(s *Snapshot) error { 35 return nil 36 }) 37 } 38} 39 40func BenchmarkSnapshotCollectionByUUID(b *testing.B) { 41 const count = 1024 42 43 tmpDir := os.TempDir() 44 defer os.RemoveAll(tmpDir) 45 46 db, _ := database.NewOpenDB(tmpDir) 47 defer db.Close() 48 49 collection := NewSnapshotCollection(db) 50 51 uuids := []string{} 52 for i := 0; i < count; i++ { 53 snapshot := NewSnapshotFromRefList(fmt.Sprintf("snapshot%d", i), nil, NewPackageRefList(), fmt.Sprintf("Snapshot number %d", i)) 54 if collection.Add(snapshot) != nil { 55 b.FailNow() 56 } 57 uuids = append(uuids, snapshot.UUID) 58 } 59 60 b.ResetTimer() 61 62 for i := 0; i < b.N; i++ { 63 collection = NewSnapshotCollection(db) 64 65 if _, err := collection.ByUUID(uuids[i%len(uuids)]); err != nil { 66 b.FailNow() 67 } 68 } 69} 70 71func BenchmarkSnapshotCollectionByName(b *testing.B) { 72 const count = 1024 73 74 tmpDir := os.TempDir() 75 defer os.RemoveAll(tmpDir) 76 77 db, _ := database.NewOpenDB(tmpDir) 78 defer db.Close() 79 80 collection := NewSnapshotCollection(db) 81 82 for i := 0; i < count; i++ { 83 snapshot := NewSnapshotFromRefList(fmt.Sprintf("snapshot%d", i), nil, NewPackageRefList(), fmt.Sprintf("Snapshot number %d", i)) 84 if collection.Add(snapshot) != nil { 85 b.FailNow() 86 } 87 } 88 89 b.ResetTimer() 90 91 for i := 0; i < b.N; i++ { 92 collection = NewSnapshotCollection(db) 93 94 if _, err := collection.ByName(fmt.Sprintf("snapshot%d", i%count)); err != nil { 95 b.FailNow() 96 } 97 } 98} 99