syncthing/test/transfer-bench_test.go

134 lines
2.6 KiB
Go
Raw Normal View History

2014-11-16 20:13:20 +00:00
// Copyright (C) 2014 The Syncthing Authors.
2014-09-29 19:43:32 +00:00
//
2015-03-07 20:36:35 +00:00
// 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 http://mozilla.org/MPL/2.0/.
2014-09-27 18:09:36 +00:00
// +build integration,benchmark
2014-09-27 18:09:36 +00:00
package integration
2014-09-27 18:09:36 +00:00
import (
"log"
"testing"
"time"
)
2015-04-05 13:34:15 +00:00
func TestBenchmarkTransferManyFiles(t *testing.T) {
benchmarkTransfer(t, 50000, 15)
}
func TestBenchmarkTransferLargeFiles(t *testing.T) {
benchmarkTransfer(t, 200, 24)
}
func benchmarkTransfer(t *testing.T, files, sizeExp int) {
2014-09-27 18:09:36 +00:00
log.Println("Cleaning...")
err := removeAll("s1", "s2", "h1/index*", "h2/index*")
2014-09-27 18:09:36 +00:00
if err != nil {
t.Fatal(err)
}
log.Println("Generating files...")
2015-04-05 13:34:15 +00:00
err = generateFiles("s1", files, sizeExp, "../LICENSE")
2014-09-27 18:09:36 +00:00
if err != nil {
t.Fatal(err)
}
expected, err := directoryContents("s1")
if err != nil {
t.Fatal(err)
}
2014-09-27 18:09:36 +00:00
log.Println("Starting sender...")
2014-09-27 18:09:36 +00:00
sender := syncthingProcess{ // id1
instance: "1",
argv: []string{"-home", "h1"},
port: 8081,
apiKey: apiKey,
2014-09-27 18:09:36 +00:00
}
2014-10-06 10:03:49 +00:00
err = sender.start()
2014-09-27 18:09:36 +00:00
if err != nil {
t.Fatal(err)
}
// Wait for one scan to succeed, or up to 20 seconds... This is to let
// startup, UPnP etc complete and make sure the sender has the full index
// before they connect.
for i := 0; i < 20; i++ {
resp, err := sender.post("/rest/scan?folder=default", nil)
if err != nil {
time.Sleep(time.Second)
continue
}
if resp.StatusCode != 200 {
resp.Body.Close()
time.Sleep(time.Second)
continue
}
break
}
log.Println("Starting receiver...")
2014-09-27 18:09:36 +00:00
receiver := syncthingProcess{ // id2
instance: "2",
argv: []string{"-home", "h2"},
port: 8082,
apiKey: apiKey,
2014-09-27 18:09:36 +00:00
}
2014-10-06 10:03:49 +00:00
err = receiver.start()
2014-09-27 18:09:36 +00:00
if err != nil {
sender.stop()
t.Fatal(err)
}
var t0, t1 time.Time
2014-09-27 18:09:36 +00:00
loop:
for {
evs, err := receiver.events()
if err != nil {
if isTimeout(err) {
2014-09-27 18:09:36 +00:00
continue
}
sender.stop()
receiver.stop()
t.Fatal(err)
}
for _, ev := range evs {
if ev.Type == "StateChanged" {
data := ev.Data.(map[string]interface{})
if data["folder"].(string) != "default" {
2014-09-27 18:09:36 +00:00
continue
}
log.Println(ev)
if data["to"].(string) == "syncing" {
t0 = ev.Time
continue
}
if !t0.IsZero() && data["to"].(string) == "idle" {
t1 = ev.Time
2014-09-27 18:09:36 +00:00
break loop
}
}
}
time.Sleep(250 * time.Millisecond)
2014-09-27 18:09:36 +00:00
}
sender.stop()
receiver.stop()
log.Println("Verifying...")
actual, err := directoryContents("s2")
if err != nil {
t.Fatal(err)
}
err = compareDirectoryContents(actual, expected)
if err != nil {
t.Fatal(err)
}
log.Println("Sync took", t1.Sub(t0))
2014-09-27 18:09:36 +00:00
}