123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- // Copyright (C) 2015 The Syncthing Authors.
- //
- // This Source Code Form is subject to the terms of the Mozilla Public
- // License, v. 2.0. If a copy of the MPL was not distributed with this file,
- // You can obtain one at https://mozilla.org/MPL/2.0/.
- package db_test
- import (
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "testing"
- "github.com/syncthing/syncthing/lib/db"
- "github.com/syncthing/syncthing/lib/fs"
- "github.com/syncthing/syncthing/lib/protocol"
- )
- var files, oneFile, firstHalf, secondHalf []protocol.FileInfo
- var s *db.FileSet
- func init() {
- for i := 0; i < 1000; i++ {
- files = append(files, protocol.FileInfo{
- Name: fmt.Sprintf("file%d", i),
- Version: protocol.Vector{[]protocol.Counter{{ID: myID, Value: 1000}}},
- Blocks: genBlocks(i),
- })
- }
- middle := len(files) / 2
- firstHalf = files[:middle]
- secondHalf = files[middle:]
- oneFile = firstHalf[middle-1 : middle]
- ldb, _ := tempDB()
- s = db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
- replace(s, remoteDevice0, files)
- replace(s, protocol.LocalDeviceID, firstHalf)
- }
- func tempDB() (*db.Instance, string) {
- dir, err := ioutil.TempDir("", "syncthing")
- if err != nil {
- panic(err)
- }
- dbi, err := db.Open(filepath.Join(dir, "db"))
- if err != nil {
- panic(err)
- }
- return dbi, dir
- }
- func BenchmarkReplaceAll(b *testing.B) {
- ldb, dir := tempDB()
- defer func() {
- ldb.Close()
- os.RemoveAll(dir)
- }()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
- replace(m, protocol.LocalDeviceID, files)
- }
- b.ReportAllocs()
- }
- func BenchmarkUpdateOneChanged(b *testing.B) {
- changed := make([]protocol.FileInfo, 1)
- changed[0] = oneFile[0]
- changed[0].Version = changed[0].Version.Update(myID)
- changed[0].Blocks = genBlocks(len(changed[0].Blocks))
- for i := 0; i < b.N; i++ {
- if i%1 == 0 {
- s.Update(protocol.LocalDeviceID, changed)
- } else {
- s.Update(protocol.LocalDeviceID, oneFile)
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkUpdateOneUnchanged(b *testing.B) {
- for i := 0; i < b.N; i++ {
- s.Update(protocol.LocalDeviceID, oneFile)
- }
- b.ReportAllocs()
- }
- func BenchmarkNeedHalf(b *testing.B) {
- for i := 0; i < b.N; i++ {
- count := 0
- s.WithNeed(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
- count++
- return true
- })
- if count != len(secondHalf) {
- b.Errorf("wrong length %d != %d", count, len(secondHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkHave(b *testing.B) {
- for i := 0; i < b.N; i++ {
- count := 0
- s.WithHave(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
- count++
- return true
- })
- if count != len(firstHalf) {
- b.Errorf("wrong length %d != %d", count, len(firstHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkGlobal(b *testing.B) {
- for i := 0; i < b.N; i++ {
- count := 0
- s.WithGlobal(func(fi db.FileIntf) bool {
- count++
- return true
- })
- if count != len(files) {
- b.Errorf("wrong length %d != %d", count, len(files))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkNeedHalfTruncated(b *testing.B) {
- for i := 0; i < b.N; i++ {
- count := 0
- s.WithNeedTruncated(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
- count++
- return true
- })
- if count != len(secondHalf) {
- b.Errorf("wrong length %d != %d", count, len(secondHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkHaveTruncated(b *testing.B) {
- for i := 0; i < b.N; i++ {
- count := 0
- s.WithHaveTruncated(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
- count++
- return true
- })
- if count != len(firstHalf) {
- b.Errorf("wrong length %d != %d", count, len(firstHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkGlobalTruncated(b *testing.B) {
- for i := 0; i < b.N; i++ {
- count := 0
- s.WithGlobalTruncated(func(fi db.FileIntf) bool {
- count++
- return true
- })
- if count != len(files) {
- b.Errorf("wrong length %d != %d", count, len(files))
- }
- }
- b.ReportAllocs()
- }
|