transfer-bench_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build integration,benchmark
  16. package integration
  17. import (
  18. "log"
  19. "testing"
  20. "time"
  21. )
  22. func TestBenchmarkTransfer(t *testing.T) {
  23. log.Println("Cleaning...")
  24. err := removeAll("s1", "s2", "h1/index", "h2/index")
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. log.Println("Generating files...")
  29. err = generateFiles("s1", 10000, 22, "../LICENSE")
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. expected := directoryContents("s1")
  34. log.Println("Starting sender...")
  35. sender := syncthingProcess{ // id1
  36. log: "1.out",
  37. argv: []string{"-home", "h1"},
  38. port: 8081,
  39. apiKey: apiKey,
  40. }
  41. err = sender.start()
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. // Make sure the sender has the full index before they connect
  46. sender.post("/rest/scan?folder=default", nil)
  47. log.Println("Starting receiver...")
  48. receiver := syncthingProcess{ // id2
  49. log: "2.out",
  50. argv: []string{"-home", "h2"},
  51. port: 8082,
  52. apiKey: apiKey,
  53. }
  54. err = receiver.start()
  55. if err != nil {
  56. sender.stop()
  57. t.Fatal(err)
  58. }
  59. var t0, t1 time.Time
  60. loop:
  61. for {
  62. evs, err := receiver.events()
  63. if err != nil {
  64. if isTimeout(err) {
  65. continue
  66. }
  67. sender.stop()
  68. receiver.stop()
  69. t.Fatal(err)
  70. }
  71. for _, ev := range evs {
  72. if ev.Type == "StateChanged" {
  73. data := ev.Data.(map[string]interface{})
  74. if data["folder"].(string) != "default" {
  75. continue
  76. }
  77. log.Println(ev)
  78. if data["to"].(string) == "syncing" {
  79. t0 = ev.Time
  80. continue
  81. }
  82. if !t0.IsZero() && data["to"].(string) == "idle" {
  83. t1 = ev.Time
  84. break loop
  85. }
  86. }
  87. }
  88. time.Sleep(250 * time.Millisecond)
  89. }
  90. sender.stop()
  91. receiver.stop()
  92. log.Println("Verifying...")
  93. actual := directoryContents("s2")
  94. err = compareDirectoryContents(actual, expected)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. log.Println("Sync took", t1.Sub(t0))
  99. }