github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/perf/hash-perf-rig/main.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package main
    23  
    24  import (
    25  	"crypto/sha1"
    26  	"crypto/sha256"
    27  	"crypto/sha512"
    28  	"fmt"
    29  	"hash"
    30  	"io"
    31  	"os"
    32  	"time"
    33  
    34  	"github.com/codahale/blake2"
    35  	humanize "github.com/dustin/go-humanize"
    36  	flag "github.com/juju/gnuflag"
    37  	"github.com/kch42/buzhash"
    38  )
    39  
    40  func main() {
    41  	useSHA := flag.String("use-sha", "", "<default>=no hashing, 1=sha1, 256=sha256, 512=sha512, blake=blake2b")
    42  	useBH := flag.Bool("use-bh", false, "whether we buzhash the bytes")
    43  	flag.Parse(true)
    44  
    45  	flag.Usage = func() {
    46  		fmt.Printf("%s <big-file>\n", os.Args[0])
    47  		flag.PrintDefaults()
    48  	}
    49  
    50  	if len(flag.Args()) < 1 {
    51  		flag.Usage()
    52  		return
    53  	}
    54  
    55  	p := flag.Args()[0]
    56  	bh := buzhash.NewBuzHash(64 * 8)
    57  	f, _ := os.Open(p)
    58  	defer f.Close()
    59  	t0 := time.Now()
    60  	buf := make([]byte, 4*1024)
    61  	l := uint64(0)
    62  
    63  	var h hash.Hash
    64  	if *useSHA == "1" {
    65  		h = sha1.New()
    66  	} else if *useSHA == "256" {
    67  		h = sha256.New()
    68  	} else if *useSHA == "512" {
    69  		h = sha512.New()
    70  	} else if *useSHA == "blake" {
    71  		h = blake2.NewBlake2B()
    72  	}
    73  
    74  	for {
    75  		n, err := f.Read(buf)
    76  		l += uint64(n)
    77  		if err == io.EOF {
    78  			break
    79  		}
    80  		s := buf[:n]
    81  		if h != nil {
    82  			h.Write(s)
    83  		}
    84  		if *useBH {
    85  			bh.Write(s)
    86  		}
    87  	}
    88  
    89  	t1 := time.Now()
    90  	d := t1.Sub(t0)
    91  	fmt.Printf("Read  %s in %s (%s/s)\n", humanize.Bytes(l), d, humanize.Bytes(uint64(float64(l)/d.Seconds())))
    92  	digest := []byte{}
    93  	if h != nil {
    94  		fmt.Printf("%x\n", h.Sum(digest))
    95  	}
    96  }