benchmark_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package weakhash
  7. import (
  8. "os"
  9. "testing"
  10. "github.com/chmduquesne/rollinghash/adler32"
  11. )
  12. const testFile = "../model/testdata/~syncthing~file.tmp"
  13. const size = 128 << 10
  14. func BenchmarkFind1MFile(b *testing.B) {
  15. b.ReportAllocs()
  16. b.SetBytes(1 << 20)
  17. for i := 0; i < b.N; i++ {
  18. fd, err := os.Open(testFile)
  19. if err != nil {
  20. b.Fatal(err)
  21. }
  22. _, err = Find(fd, []uint32{0, 1, 2}, size)
  23. if err != nil {
  24. b.Fatal(err)
  25. }
  26. fd.Close()
  27. }
  28. }
  29. func BenchmarkWeakHashAdler32(b *testing.B) {
  30. data := make([]byte, size)
  31. hf := adler32.New()
  32. for i := 0; i < b.N; i++ {
  33. hf.Write(data)
  34. }
  35. _ = hf.Sum32()
  36. b.SetBytes(size)
  37. }
  38. func BenchmarkWeakHashAdler32Roll(b *testing.B) {
  39. data := make([]byte, size)
  40. hf := adler32.New()
  41. hf.Write(data)
  42. b.ResetTimer()
  43. for i := 0; i < b.N; i++ {
  44. for i := 0; i <= size; i++ {
  45. hf.Roll('a')
  46. }
  47. }
  48. b.SetBytes(size)
  49. }