123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- // 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"
- "testing"
- "github.com/syncthing/syncthing/lib/db"
- "github.com/syncthing/syncthing/lib/protocol"
- )
- var files, oneFile, firstHalf, secondHalf, changed100, unchanged100 []protocol.FileInfo
- func lazyInitBenchFiles() {
- if files != nil {
- return
- }
- files = make([]protocol.FileInfo, 0, 1000)
- for i := 0; i < 1000; i++ {
- files = append(files, protocol.FileInfo{
- Name: fmt.Sprintf("file%d", i),
- Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1000}}},
- Blocks: genBlocks(i),
- })
- }
- middle := len(files) / 2
- firstHalf = files[:middle]
- secondHalf = files[middle:]
- oneFile = firstHalf[middle-1 : middle]
- unchanged100 := files[100:200]
- changed100 := append([]protocol.FileInfo{}, unchanged100...)
- for i := range changed100 {
- changed100[i].Version = changed100[i].Version.Copy().Update(myID)
- }
- }
- func getBenchFileSet(b testing.TB) (*db.Lowlevel, *db.FileSet) {
- lazyInitBenchFiles()
- ldb := newLowlevelMemory(b)
- benchS := newFileSet(b, "test)", ldb)
- replace(benchS, remoteDevice0, files)
- replace(benchS, protocol.LocalDeviceID, firstHalf)
- return ldb, benchS
- }
- func BenchmarkReplaceAll(b *testing.B) {
- ldb := newLowlevelMemory(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- m := newFileSet(b, "test)", ldb)
- replace(m, protocol.LocalDeviceID, files)
- }
- b.ReportAllocs()
- }
- func BenchmarkUpdateOneChanged(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- changed := make([]protocol.FileInfo, 1)
- changed[0] = oneFile[0]
- changed[0].Version = changed[0].Version.Copy().Update(myID)
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- if i%2 == 0 {
- benchS.Update(protocol.LocalDeviceID, changed)
- } else {
- benchS.Update(protocol.LocalDeviceID, oneFile)
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkUpdate100Changed(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- if i%2 == 0 {
- benchS.Update(protocol.LocalDeviceID, changed100)
- } else {
- benchS.Update(protocol.LocalDeviceID, unchanged100)
- }
- }
- b.ReportAllocs()
- }
- func setup10Remotes(benchS *db.FileSet) {
- idBase := remoteDevice1.String()[1:]
- first := 'J'
- for i := 0; i < 10; i++ {
- id, _ := protocol.DeviceIDFromString(fmt.Sprintf("%v%s", first+rune(i), idBase))
- if i%2 == 0 {
- benchS.Update(id, changed100)
- } else {
- benchS.Update(id, unchanged100)
- }
- }
- }
- func BenchmarkUpdate100Changed10Remotes(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- setup10Remotes(benchS)
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- if i%2 == 0 {
- benchS.Update(protocol.LocalDeviceID, changed100)
- } else {
- benchS.Update(protocol.LocalDeviceID, unchanged100)
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkUpdate100ChangedRemote(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- if i%2 == 0 {
- benchS.Update(remoteDevice0, changed100)
- } else {
- benchS.Update(remoteDevice0, unchanged100)
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkUpdate100ChangedRemote10Remotes(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- if i%2 == 0 {
- benchS.Update(remoteDevice0, changed100)
- } else {
- benchS.Update(remoteDevice0, unchanged100)
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkUpdateOneUnchanged(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- benchS.Update(protocol.LocalDeviceID, oneFile)
- }
- b.ReportAllocs()
- }
- func BenchmarkNeedHalf(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- count := 0
- snap := snapshot(b, benchS)
- snap.WithNeed(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
- count++
- return true
- })
- snap.Release()
- if count != len(secondHalf) {
- b.Errorf("wrong length %d != %d", count, len(secondHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkNeedHalfRemote(b *testing.B) {
- ldb := newLowlevelMemory(b)
- defer ldb.Close()
- fset := newFileSet(b, "test)", ldb)
- replace(fset, remoteDevice0, firstHalf)
- replace(fset, protocol.LocalDeviceID, files)
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- count := 0
- snap := snapshot(b, fset)
- snap.WithNeed(remoteDevice0, func(fi protocol.FileIntf) bool {
- count++
- return true
- })
- snap.Release()
- if count != len(secondHalf) {
- b.Errorf("wrong length %d != %d", count, len(secondHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkHave(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- count := 0
- snap := snapshot(b, benchS)
- snap.WithHave(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
- count++
- return true
- })
- snap.Release()
- if count != len(firstHalf) {
- b.Errorf("wrong length %d != %d", count, len(firstHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkGlobal(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- count := 0
- snap := snapshot(b, benchS)
- snap.WithGlobal(func(fi protocol.FileIntf) bool {
- count++
- return true
- })
- snap.Release()
- if count != len(files) {
- b.Errorf("wrong length %d != %d", count, len(files))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkNeedHalfTruncated(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- count := 0
- snap := snapshot(b, benchS)
- snap.WithNeedTruncated(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
- count++
- return true
- })
- snap.Release()
- if count != len(secondHalf) {
- b.Errorf("wrong length %d != %d", count, len(secondHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkHaveTruncated(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- count := 0
- snap := snapshot(b, benchS)
- snap.WithHaveTruncated(protocol.LocalDeviceID, func(fi protocol.FileIntf) bool {
- count++
- return true
- })
- snap.Release()
- if count != len(firstHalf) {
- b.Errorf("wrong length %d != %d", count, len(firstHalf))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkGlobalTruncated(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- count := 0
- snap := snapshot(b, benchS)
- snap.WithGlobalTruncated(func(fi protocol.FileIntf) bool {
- count++
- return true
- })
- snap.Release()
- if count != len(files) {
- b.Errorf("wrong length %d != %d", count, len(files))
- }
- }
- b.ReportAllocs()
- }
- func BenchmarkNeedCount(b *testing.B) {
- ldb, benchS := getBenchFileSet(b)
- defer ldb.Close()
- benchS.Update(protocol.LocalDeviceID, changed100)
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- snap := snapshot(b, benchS)
- _ = snap.NeedSize(protocol.LocalDeviceID)
- snap.Release()
- }
- b.ReportAllocs()
- }
|