Commit 339f3e1c authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 75a9a26b
// zsha1 - compute sha1 of whole latest objects stream in a ZODB database
// zhash - compute hash of whole latest objects stream in a ZODB database
// +build ignore
package main
......@@ -6,6 +6,8 @@ package main
import (
"context"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"flag"
"fmt"
"hash"
......@@ -25,21 +27,55 @@ import (
"github.com/pkg/profile"
)
// hasher is hash.Hash which also knows its name
type hasher struct {
hash.Hash
name string
}
func main() {
defer log.Flush()
fadler32 := flag.Bool("adler32", false, "compute Adler32 checksum")
fcrc32 := flag.Bool("crc32", false, "compute CRC32 checksum")
fsha1 := flag.Bool("sha1", false, "compute SHA1 cryptographic hash")
fsha256 := flag.Bool("sha256", false, "compute SHA256 cryptographic hash")
fsha512 := flag.Bool("sha512", false, "compute SHA512 cryptographic hash")
useprefetch := flag.Bool("useprefetch", false, "prefetch loaded objects")
flag.Parse()
url := flag.Args()[0] // XXX dirty
ctx := context.Background()
err := zsha1(ctx, url, *useprefetch)
var h hasher
inith := func(name string, ctor func() hash.Hash) {
if h.name != "" {
log.Fatalf(ctx, "duplicate hashes: %s and %s specified", h.name, name)
}
h.name = name
h.Hash = ctor()
}
switch {
case *fadler32: inith("adler32", func() hash.Hash { return adler32.New() })
case *fcrc32: inith("crc32", func() hash.Hash { return crc32.NewIEEE() })
case *fsha1: inith("sha1", sha1.New)
case *fsha256: inith("sha256", sha256.New)
case *fsha512: inith("sha512", sha512.New)
}
if h.Hash == nil {
log.Fatal(ctx, "no hash function specified")
}
err := zhash(ctx, url, h, *useprefetch)
if err != nil {
log.Fatal(ctx, err)
}
}
func zsha1(ctx context.Context, url string, useprefetch bool) (err error) {
defer task.Running(&ctx, "zsha1")(&err)
func zhash(ctx context.Context, url string, h hasher, useprefetch bool) (err error) {
defer task.Running(&ctx, "zhash")(&err)
stor, err := zodb.OpenStorageURL(ctx, url)
if err != nil {
......@@ -111,18 +147,7 @@ func zsha1(ctx context.Context, url string, useprefetch bool) (err error) {
for qqq := 0; qqq < 10; qqq++ {
tstart := time.Now()
var m hash.Hash
hashName := "crc32"
switch hashName {
case "sha1":
m = sha1.New()
case "crc32":
m = crc32.NewIEEE()
case "adler32":
m = adler32.New()
default:
panic(0) // XXX
}
h.Reset() // XXX temp
oid := zodb.Oid(0)
nread := 0
......@@ -143,9 +168,9 @@ loop:
return err
}
//m.Write(data)
h.Write(data)
//fmt.Fprintf(os.Stderr, "%d @%s\tsha1: %x\n", uint(oid), serial, m.Sum(nil))
//fmt.Fprintf(os.Stderr, "%d @%s\tsha1: %x\n", uint(oid), serial, h.Sum(nil))
//fmt.Fprintf(os.Stderr, "\tdata: %x\n", data)
nread += len(data)
......@@ -160,7 +185,7 @@ loop:
x += " +prefetch"
}
fmt.Printf("%s:%x ; oid=0..%d nread=%d t=%s (%s / object) x=%s\n",
hashName,m.Sum(nil), oid-1, nread, δt, δt / time.Duration(oid), x) // XXX /oid cast ?
h.name, h.Sum(nil), oid-1, nread, δt, δt / time.Duration(oid), x) // XXX /oid cast ?
}
return nil
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""zsha1 - compute sha1 of whole latest objects stream in a ZODB database"""
"""zhash - compute hash of whole latest objects stream in a ZODB database"""
from __future__ import print_function
......@@ -12,10 +12,23 @@ import hashlib
from zlib import crc32, adler32
import sys
from time import time
from getopt import getopt, GetoptError
# adler32 in hashlib interface
class Adler32Hasher:
name = "adler32"
def __init__(self):
self.h = adler32('')
def update(self, data):
self.h = adler32(data, self.h)
def hexdigest(self):
return '%x' % (self.h & 0xffffffff)
# crc32 in hashlib interface
class CRC32Hasher:
name = "crc32"
def __init__(self):
......@@ -27,34 +40,64 @@ class CRC32Hasher:
def hexdigest(self):
return '%x' % (self.h & 0xffffffff)
# adler32 in hashlib interface
class Adler32Hasher:
name = "adler32"
# {} name -> hasher
hashRegistry = {
"adler32": Adler32Hasher,
"crc32": CRC32Hasher,
"sha1": hashlib.sha1,
"sha256": hashlib.sha256,
"sha512": hashlib.sha512,
}
def __init__(self):
self.h = adler32('')
def usage(w):
print(\
"""Usage: zhash [options] url
def update(self, data):
self.h = adler32(data, self.h)
options:
def hexdigest(self):
return '%x' % (self.h & 0xffffffff)
--adler32 compute Adler32 checksum
--crc32 compute CRC32 checksum
--sha1 compute SHA1 cryptographic hash
--sha256 compute SHA256 cryptographic hash
--sha512 compute SHA512 cryptographic hash
""", file=w)
def main():
url = sys.argv[1]
try:
optv, argv = getopt(sys.argv[1:], "h", ["help"] + hashRegistry.keys())
except GetoptError as e:
print("E: %s" % e, file=sys.stderr)
usage(sys.stderr)
exit(1)
for opt, _ in optv:
if opt in ("-h", "--help"):
usage(sys.stdout)
sys.exit()
opt = opt.lstrip("-")
hctor = hashRegistry[opt]
h = hctor()
if len(argv) != 1:
usage(sys.stderr)
sys.exit(1)
url = argv[0]
stor = zodbtools.util.storageFromURL(url, read_only=True)
last_tid = stor.lastTransaction()
before = p64(u64(last_tid) + 1)
for zzz in range(10):
#m = hashlib.sha1()
m = CRC32Hasher()
#m = Adler32Hasher()
tstart = time()
# vvv h.reset() XXX temp
try:
h = h.__class__()
except:
h = hashlib.new(h.name)
oid = 0
nread = 0
while 1:
......@@ -63,9 +106,9 @@ def main():
except POSKeyError:
break
m.update(data)
h.update(data)
#print('%s @%s\tsha1: %s' % (oid, u64(serial), m.hexdigest()), file=sys.stderr)
#print('%s @%s\tsha1: %s' % (oid, u64(serial), h.hexdigest()), file=sys.stderr)
#print('\tdata: %s' % (data.encode('hex'),), file=sys.stderr)
nread += len(data)
......@@ -75,7 +118,7 @@ def main():
dt = tend - tstart
print('%s:%s ; oid=0..%d nread=%d t=%.3fs (%.1fμs / object) x=zsha1.py' % \
(m.name, m.hexdigest(), oid-1, nread, dt, dt * 1E6 / oid))
(h.name, h.hexdigest(), oid-1, nread, dt, dt * 1E6 / oid))
if __name__ == '__main__':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment