queue_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. package model
  16. import (
  17. "fmt"
  18. "testing"
  19. "github.com/syncthing/syncthing/internal/protocol"
  20. )
  21. var (
  22. f1 = &protocol.FileInfo{Name: "f1"}
  23. f2 = &protocol.FileInfo{Name: "f2"}
  24. f3 = &protocol.FileInfo{Name: "f3"}
  25. f4 = &protocol.FileInfo{Name: "f4"}
  26. f5 = &protocol.FileInfo{Name: "f5"}
  27. )
  28. func TestJobQueue(t *testing.T) {
  29. // Some random actions
  30. q := NewJobQueue()
  31. q.Push(f1)
  32. q.Push(f2)
  33. q.Push(f3)
  34. q.Push(f4)
  35. progress, queued := q.Jobs()
  36. if len(progress) != 0 || len(queued) != 4 {
  37. t.Fatal("Wrong length")
  38. }
  39. for i := 1; i < 5; i++ {
  40. n := q.Pop()
  41. if n == nil || n.Name != fmt.Sprintf("f%d", i) {
  42. t.Fatal("Wrong element")
  43. }
  44. progress, queued = q.Jobs()
  45. if len(progress) != 1 || len(queued) != 3 {
  46. t.Fatal("Wrong length")
  47. }
  48. q.Done(n)
  49. progress, queued = q.Jobs()
  50. if len(progress) != 0 || len(queued) != 3 {
  51. t.Fatal("Wrong length", len(progress), len(queued))
  52. }
  53. q.Push(n)
  54. progress, queued = q.Jobs()
  55. if len(progress) != 0 || len(queued) != 4 {
  56. t.Fatal("Wrong length")
  57. }
  58. q.Done(f5) // Does not exist
  59. progress, queued = q.Jobs()
  60. if len(progress) != 0 || len(queued) != 4 {
  61. t.Fatal("Wrong length")
  62. }
  63. }
  64. if len(q.progress) > 0 || len(q.queued) != 4 {
  65. t.Fatal("Wrong length")
  66. }
  67. for i := 4; i > 0; i-- {
  68. progress, queued = q.Jobs()
  69. if len(progress) != 4-i || len(queued) != i {
  70. t.Fatal("Wrong length")
  71. }
  72. s := fmt.Sprintf("f%d", i)
  73. q.Bump(s)
  74. progress, queued = q.Jobs()
  75. if len(progress) != 4-i || len(queued) != i {
  76. t.Fatal("Wrong length")
  77. }
  78. n := q.Pop()
  79. if n == nil || n.Name != s {
  80. t.Fatal("Wrong element")
  81. }
  82. progress, queued = q.Jobs()
  83. if len(progress) != 5-i || len(queued) != i-1 {
  84. t.Fatal("Wrong length")
  85. }
  86. q.Done(f5) // Does not exist
  87. progress, queued = q.Jobs()
  88. if len(progress) != 5-i || len(queued) != i-1 {
  89. t.Fatal("Wrong length")
  90. }
  91. }
  92. if len(q.progress) != 4 || q.Pop() != nil {
  93. t.Fatal("Wrong length")
  94. }
  95. q.Done(f1)
  96. q.Done(f2)
  97. q.Done(f3)
  98. q.Done(f4)
  99. q.Done(f5) // Does not exist
  100. if len(q.progress) != 0 || q.Pop() != nil {
  101. t.Fatal("Wrong length")
  102. }
  103. progress, queued = q.Jobs()
  104. if len(progress) != 0 || len(queued) != 0 {
  105. t.Fatal("Wrong length")
  106. }
  107. q.Bump("")
  108. q.Done(f5) // Does not exist
  109. progress, queued = q.Jobs()
  110. if len(progress) != 0 || len(queued) != 0 {
  111. t.Fatal("Wrong length")
  112. }
  113. }
  114. /*
  115. func BenchmarkJobQueuePush(b *testing.B) {
  116. files := genFiles(b.N)
  117. q := NewJobQueue()
  118. b.ResetTimer()
  119. for i := 0; i < b.N; i++ {
  120. q.Push(&files[i])
  121. }
  122. }
  123. func BenchmarkJobQueuePop(b *testing.B) {
  124. files := genFiles(b.N)
  125. q := NewJobQueue()
  126. for j := range files {
  127. q.Push(&files[j])
  128. }
  129. b.ResetTimer()
  130. for i := 0; i < b.N; i++ {
  131. q.Pop()
  132. }
  133. }
  134. func BenchmarkJobQueuePopDone(b *testing.B) {
  135. files := genFiles(b.N)
  136. q := NewJobQueue()
  137. for j := range files {
  138. q.Push(&files[j])
  139. }
  140. b.ResetTimer()
  141. for i := 0; i < b.N; i++ {
  142. n := q.Pop()
  143. q.Done(n)
  144. }
  145. }
  146. */
  147. func BenchmarkJobQueueBump(b *testing.B) {
  148. files := genFiles(b.N)
  149. q := NewJobQueue()
  150. for j := range files {
  151. q.Push(&files[j])
  152. }
  153. b.ResetTimer()
  154. for i := 0; i < b.N; i++ {
  155. q.Bump(files[i].Name)
  156. }
  157. }
  158. func BenchmarkJobQueuePushPopDone10k(b *testing.B) {
  159. files := genFiles(10000)
  160. b.ResetTimer()
  161. for i := 0; i < b.N; i++ {
  162. q := NewJobQueue()
  163. for j := range files {
  164. q.Push(&files[j])
  165. }
  166. for range files {
  167. n := q.Pop()
  168. q.Done(n)
  169. }
  170. }
  171. }