github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/go/perf/hash-perf-rig/main.go (about) 1 // Copyright 2016 Attic Labs, Inc. All rights reserved. 2 // Licensed under the Apache License, version 2.0: 3 // http://www.apache.org/licenses/LICENSE-2.0 4 5 package main 6 7 import ( 8 "crypto/sha1" 9 "crypto/sha256" 10 "crypto/sha512" 11 "fmt" 12 "hash" 13 "io" 14 "os" 15 "time" 16 17 "github.com/attic-labs/kingpin" 18 "github.com/codahale/blake2" 19 humanize "github.com/dustin/go-humanize" 20 "github.com/kch42/buzhash" 21 ) 22 23 func main() { 24 useSHA := kingpin.Flag("use-sha", "<default>=no hashing, 1=sha1, 256=sha256, 512=sha512, blake=blake2b").String() 25 useBH := kingpin.Flag("use-bh", "whether we buzhash the bytes").Bool() 26 bigFile := kingpin.Arg("bigfile", "input file to chunk").Required().String() 27 28 kingpin.Parse() 29 30 bh := buzhash.NewBuzHash(64 * 8) 31 f, _ := os.Open(*bigFile) 32 defer f.Close() 33 t0 := time.Now() 34 buf := make([]byte, 4*1024) 35 l := uint64(0) 36 37 var h hash.Hash 38 if *useSHA == "1" { 39 h = sha1.New() 40 } else if *useSHA == "256" { 41 h = sha256.New() 42 } else if *useSHA == "512" { 43 h = sha512.New() 44 } else if *useSHA == "blake" { 45 h = blake2.NewBlake2B() 46 } 47 48 for { 49 n, err := f.Read(buf) 50 l += uint64(n) 51 if err == io.EOF { 52 break 53 } 54 s := buf[:n] 55 if h != nil { 56 h.Write(s) 57 } 58 if *useBH { 59 bh.Write(s) 60 } 61 } 62 63 t1 := time.Now() 64 d := t1.Sub(t0) 65 fmt.Printf("Read %s in %s (%s/s)\n", humanize.Bytes(l), d, humanize.Bytes(uint64(float64(l)/d.Seconds()))) 66 digest := []byte{} 67 if h != nil { 68 fmt.Printf("%x\n", h.Sum(digest)) 69 } 70 }