2015-10-21 15:00:09 +00:00
|
|
|
// 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-10-21 15:00:09 +00:00
|
|
|
|
|
|
|
package db_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2018-01-28 11:26:01 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-10-21 15:00:09 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
var files, oneFile, firstHalf, secondHalf []protocol.FileInfo
|
2018-05-17 07:26:40 +00:00
|
|
|
var benchS *db.FileSet
|
|
|
|
|
|
|
|
func lazyInitBenchFileSet() {
|
|
|
|
if benchS != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 15:00:09 +00:00
|
|
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
files = append(files, protocol.FileInfo{
|
|
|
|
Name: fmt.Sprintf("file%d", i),
|
2018-01-28 11:26:01 +00:00
|
|
|
Version: protocol.Vector{[]protocol.Counter{{ID: myID, Value: 1000}}},
|
2015-10-21 15:00:09 +00:00
|
|
|
Blocks: genBlocks(i),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
middle := len(files) / 2
|
|
|
|
firstHalf = files[:middle]
|
|
|
|
secondHalf = files[middle:]
|
|
|
|
oneFile = firstHalf[middle-1 : middle]
|
|
|
|
|
|
|
|
ldb, _ := tempDB()
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS = db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
|
|
|
|
replace(benchS, remoteDevice0, files)
|
|
|
|
replace(benchS, protocol.LocalDeviceID, firstHalf)
|
2015-10-21 15:00:09 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 17:42:35 +00:00
|
|
|
func tempDB() (*db.Instance, string) {
|
2015-10-21 15:00:09 +00:00
|
|
|
dir, err := ioutil.TempDir("", "syncthing")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-11-05 17:42:35 +00:00
|
|
|
dbi, err := db.Open(filepath.Join(dir, "db"))
|
2015-10-21 15:00:09 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-11-05 17:42:35 +00:00
|
|
|
return dbi, dir
|
2015-10-21 15:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkReplaceAll(b *testing.B) {
|
|
|
|
ldb, dir := tempDB()
|
|
|
|
defer func() {
|
|
|
|
ldb.Close()
|
|
|
|
os.RemoveAll(dir)
|
|
|
|
}()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2018-01-28 11:26:01 +00:00
|
|
|
m := db.NewFileSet("test)", fs.NewFilesystem(fs.FilesystemTypeBasic, "."), ldb)
|
|
|
|
replace(m, protocol.LocalDeviceID, files)
|
2015-10-21 15:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUpdateOneChanged(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
2015-10-21 15:00:09 +00:00
|
|
|
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))
|
|
|
|
|
2018-05-17 07:26:40 +00:00
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if i%1 == 0 {
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.Update(protocol.LocalDeviceID, changed)
|
2015-10-21 15:00:09 +00:00
|
|
|
} else {
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.Update(protocol.LocalDeviceID, oneFile)
|
2015-10-21 15:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUpdateOneUnchanged(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.Update(protocol.LocalDeviceID, oneFile)
|
2015-10-21 15:00:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNeedHalf(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
count := 0
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.WithNeed(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
|
2015-10-21 15:00:09 +00:00
|
|
|
count++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if count != len(secondHalf) {
|
|
|
|
b.Errorf("wrong length %d != %d", count, len(secondHalf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHave(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
count := 0
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.WithHave(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
|
2015-10-21 15:00:09 +00:00
|
|
|
count++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if count != len(firstHalf) {
|
|
|
|
b.Errorf("wrong length %d != %d", count, len(firstHalf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGlobal(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
count := 0
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.WithGlobal(func(fi db.FileIntf) bool {
|
2015-10-21 15:00:09 +00:00
|
|
|
count++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if count != len(files) {
|
|
|
|
b.Errorf("wrong length %d != %d", count, len(files))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkNeedHalfTruncated(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
count := 0
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.WithNeedTruncated(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
|
2015-10-21 15:00:09 +00:00
|
|
|
count++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if count != len(secondHalf) {
|
|
|
|
b.Errorf("wrong length %d != %d", count, len(secondHalf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkHaveTruncated(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
count := 0
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.WithHaveTruncated(protocol.LocalDeviceID, func(fi db.FileIntf) bool {
|
2015-10-21 15:00:09 +00:00
|
|
|
count++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if count != len(firstHalf) {
|
|
|
|
b.Errorf("wrong length %d != %d", count, len(firstHalf))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkGlobalTruncated(b *testing.B) {
|
2018-05-17 07:26:40 +00:00
|
|
|
lazyInitBenchFileSet()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2015-10-21 15:00:09 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
count := 0
|
2018-05-17 07:26:40 +00:00
|
|
|
benchS.WithGlobalTruncated(func(fi db.FileIntf) bool {
|
2015-10-21 15:00:09 +00:00
|
|
|
count++
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if count != len(files) {
|
|
|
|
b.Errorf("wrong length %d != %d", count, len(files))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
}
|