xor_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package xor
  2. import (
  3. "bytes"
  4. "math/rand"
  5. "testing"
  6. )
  7. func TestVerifyBytesNoSIMD(t *testing.T) {
  8. for i := 1; i <= unitSize+16+2; i++ {
  9. if !verifyBytesNoSIMD(i) {
  10. t.Fatal("xor fault ", "size:", i)
  11. }
  12. }
  13. }
  14. func verifyBytesNoSIMD(size int) bool {
  15. dst := make([]byte, size)
  16. src0 := make([]byte, size)
  17. src1 := make([]byte, size)
  18. expect := make([]byte, size)
  19. rand.Seed(7)
  20. fillRandom(src0)
  21. rand.Seed(8)
  22. fillRandom(src1)
  23. for i := 0; i < size; i++ {
  24. expect[i] = src0[i] ^ src1[i]
  25. }
  26. xorBytes(dst, src0, src1, size)
  27. return bytes.Equal(expect, dst)
  28. }
  29. func TestVerifyBytes(t *testing.T) {
  30. for i := 1; i <= unitSize+16+2; i++ {
  31. if !verifyBytes(i) {
  32. t.Fatal("xor fault ", "size:", i)
  33. }
  34. }
  35. }
  36. func verifyBytes(size int) bool {
  37. dst := make([]byte, size)
  38. src0 := make([]byte, size)
  39. src1 := make([]byte, size)
  40. expect := make([]byte, size)
  41. rand.Seed(7)
  42. fillRandom(src0)
  43. rand.Seed(8)
  44. fillRandom(src1)
  45. for i := 0; i < size; i++ {
  46. expect[i] = src0[i] ^ src1[i]
  47. }
  48. xorBytes(dst, src0, src1, size)
  49. return bytes.Equal(expect, dst)
  50. }
  51. func TestVerifyBytesSrc1(t *testing.T) {
  52. for i := 1; i <= unitSize+16+2; i++ {
  53. if !verifyBytesSrc1(i) {
  54. t.Fatal("xor fault ", "size:", i)
  55. }
  56. }
  57. }
  58. func verifyBytesSrc1(size int) bool {
  59. dst := make([]byte, size)
  60. src0 := make([]byte, size)
  61. src1 := make([]byte, size)
  62. expect := make([]byte, size)
  63. rand.Seed(7)
  64. fillRandom(src0)
  65. rand.Seed(8)
  66. fillRandom(src1)
  67. for i := 0; i < size; i++ {
  68. expect[i] = src0[i] ^ src1[i]
  69. }
  70. xorSrc0(dst, src0, src1)
  71. return bytes.Equal(expect, dst)
  72. }
  73. func TestVerifyMatrixNoSIMD(t *testing.T) {
  74. for i := 1; i <= unitSize+16+2; i++ {
  75. if !verifyMatrixNoSIMD(i) {
  76. t.Fatal("xor fault ", "size:", i)
  77. }
  78. }
  79. }
  80. func verifyMatrixNoSIMD(size int) bool {
  81. numSRC := 3
  82. dst := make([]byte, size)
  83. expect := make([]byte, size)
  84. src := make([][]byte, numSRC)
  85. for i := 0; i < numSRC; i++ {
  86. src[i] = make([]byte, size)
  87. rand.Seed(int64(i))
  88. fillRandom(src[i])
  89. }
  90. for i := 0; i < size; i++ {
  91. expect[i] = src[0][i] ^ src[1][i]
  92. }
  93. for i := 2; i < numSRC; i++ {
  94. for j := 0; j < size; j++ {
  95. expect[j] ^= src[i][j]
  96. }
  97. }
  98. matrixNoSIMD(dst, src)
  99. return bytes.Equal(expect, dst)
  100. }
  101. func TestVerifyMatrix(t *testing.T) {
  102. for i := 1; i <= unitSize+16+2; i++ {
  103. if !verifyMatrix(i) {
  104. t.Fatal("xor fault ", "size:", i)
  105. }
  106. }
  107. }
  108. func verifyMatrix(size int) bool {
  109. numSRC := 3
  110. dst := make([]byte, size)
  111. expect := make([]byte, size)
  112. src := make([][]byte, numSRC)
  113. for i := 0; i < numSRC; i++ {
  114. src[i] = make([]byte, size)
  115. rand.Seed(int64(i))
  116. fillRandom(src[i])
  117. }
  118. for i := 0; i < size; i++ {
  119. expect[i] = src[0][i] ^ src[1][i]
  120. }
  121. for i := 2; i < numSRC; i++ {
  122. for j := 0; j < size; j++ {
  123. expect[j] ^= src[i][j]
  124. }
  125. }
  126. xorMatrix(dst, src)
  127. return bytes.Equal(expect, dst)
  128. }
  129. func BenchmarkBytesNoSIMDx12B(b *testing.B) {
  130. benchmarkBytesNoSIMD(b, 12)
  131. }
  132. func BenchmarkBytes12B(b *testing.B) {
  133. benchmarkBytesMini(b, 12)
  134. }
  135. func BenchmarkBytesNoSIMD16B(b *testing.B) {
  136. benchmarkBytesNoSIMD(b, 16)
  137. }
  138. func BenchmarkBytes16B(b *testing.B) {
  139. benchmarkBytesMini(b, 16)
  140. }
  141. func BenchmarkBytesNoSIMD24B(b *testing.B) {
  142. benchmarkBytesNoSIMD(b, 24)
  143. }
  144. func BenchmarkBytes24B(b *testing.B) {
  145. benchmarkBytesMini(b, 24)
  146. }
  147. func BenchmarkBytesNoSIMD32B(b *testing.B) {
  148. benchmarkBytesNoSIMD(b, 32)
  149. }
  150. func BenchmarkBytes32B(b *testing.B) {
  151. benchmarkBytesMini(b, 32)
  152. }
  153. func benchmarkBytesMini(b *testing.B, size int) {
  154. src0 := make([]byte, size)
  155. src1 := make([]byte, size)
  156. dst := make([]byte, size)
  157. rand.Seed(int64(0))
  158. fillRandom(src0)
  159. rand.Seed(int64(1))
  160. fillRandom(src1)
  161. BytesSrc1(dst, src0, src1)
  162. b.SetBytes(int64(size) * 2)
  163. b.ResetTimer()
  164. for i := 0; i < b.N; i++ {
  165. BytesSrc1(dst, src0, src1)
  166. }
  167. }
  168. func BenchmarkBytesNoSIMD1K(b *testing.B) {
  169. benchmarkBytesNoSIMD(b, 1024)
  170. }
  171. func BenchmarkBytesNoSIMD16K(b *testing.B) {
  172. benchmarkBytesNoSIMD(b, 16*1024)
  173. }
  174. func BenchmarkBytesNoSIMD16M(b *testing.B) {
  175. benchmarkBytesNoSIMD(b, 16*1024*1024)
  176. }
  177. func benchmarkBytesNoSIMD(b *testing.B, size int) {
  178. src1 := make([]byte, size)
  179. src2 := make([]byte, size)
  180. dst := make([]byte, size)
  181. rand.Seed(int64(0))
  182. fillRandom(src1)
  183. rand.Seed(int64(1))
  184. fillRandom(src2)
  185. bytesNoSIMD(dst, src1, src2, size)
  186. b.SetBytes(int64(size) * 2)
  187. b.ResetTimer()
  188. for i := 0; i < b.N; i++ {
  189. bytesNoSIMD(dst, src1, src2, size)
  190. }
  191. }
  192. func BenchmarkBytes1K(b *testing.B) {
  193. benchmarkBytes(b, 1024)
  194. }
  195. func BenchmarkBytes16K(b *testing.B) {
  196. benchmarkBytes(b, 16*1024)
  197. }
  198. func BenchmarkBytes16M(b *testing.B) {
  199. benchmarkBytes(b, 16*1024*1024)
  200. }
  201. // compare with bytes
  202. func BenchmarkMatrix2x1K(b *testing.B) {
  203. benchmarkMatrix(b, 2, 1024)
  204. }
  205. func BenchmarkMatrix2x16K(b *testing.B) {
  206. benchmarkMatrix(b, 2, 16*1024)
  207. }
  208. func BenchmarkMatrix2x16M(b *testing.B) {
  209. benchmarkMatrix(b, 2, 16*1024*1024)
  210. }
  211. func benchmarkBytes(b *testing.B, size int) {
  212. src1 := make([]byte, size)
  213. src2 := make([]byte, size)
  214. dst := make([]byte, size)
  215. rand.Seed(int64(0))
  216. fillRandom(src1)
  217. rand.Seed(int64(1))
  218. fillRandom(src2)
  219. xorBytes(dst, src1, src2, size)
  220. b.SetBytes(int64(size) * 2)
  221. b.ResetTimer()
  222. for i := 0; i < b.N; i++ {
  223. xorBytes(dst, src1, src2, size)
  224. }
  225. }
  226. func BenchmarkMatrixNoSIMD5x1K(b *testing.B) {
  227. benchmarkMatrixNoSIMD(b, 5, 1024)
  228. }
  229. func BenchmarkMatrixNoSIMD5x16K(b *testing.B) {
  230. benchmarkMatrixNoSIMD(b, 5, 16*1024)
  231. }
  232. func BenchmarkMatrixNoSIMD5x16M(b *testing.B) {
  233. benchmarkMatrixNoSIMD(b, 5, 16*1024*1024)
  234. }
  235. func benchmarkMatrixNoSIMD(b *testing.B, numSRC, size int) {
  236. src := make([][]byte, numSRC)
  237. dst := make([]byte, size)
  238. for i := 0; i < numSRC; i++ {
  239. rand.Seed(int64(i))
  240. src[i] = make([]byte, size)
  241. fillRandom(src[i])
  242. }
  243. matrixNoSIMD(dst, src)
  244. b.SetBytes(int64(size * numSRC))
  245. b.ResetTimer()
  246. for i := 0; i < b.N; i++ {
  247. matrixNoSIMD(dst, src)
  248. }
  249. }
  250. func BenchmarkMatrix5x1K(b *testing.B) {
  251. benchmarkMatrix(b, 5, 1024)
  252. }
  253. func BenchmarkMatrix5x16K(b *testing.B) {
  254. benchmarkMatrix(b, 5, 16*1024)
  255. }
  256. func BenchmarkMatrix5x16M(b *testing.B) {
  257. benchmarkMatrix(b, 5, 16*1024*1024)
  258. }
  259. func benchmarkMatrix(b *testing.B, numSRC, size int) {
  260. src := make([][]byte, numSRC)
  261. dst := make([]byte, size)
  262. for i := 0; i < numSRC; i++ {
  263. rand.Seed(int64(i))
  264. src[i] = make([]byte, size)
  265. fillRandom(src[i])
  266. }
  267. xorMatrix(dst, src)
  268. b.SetBytes(int64(size * numSRC))
  269. b.ResetTimer()
  270. for i := 0; i < b.N; i++ {
  271. xorMatrix(dst, src)
  272. }
  273. }
  274. func fillRandom(p []byte) {
  275. for i := 0; i < len(p); i += 7 {
  276. val := rand.Int63()
  277. for j := 0; i+j < len(p) && j < 7; j++ {
  278. p[i+j] = byte(val)
  279. val >>= 8
  280. }
  281. }
  282. }