Skip to content

Instantly share code, notes, and snippets.

@zelig
Last active April 7, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zelig/4321b564f4e63d993bc175b19ad427a2 to your computer and use it in GitHub Desktop.
Save zelig/4321b564f4e63d993bc175b19ad427a2 to your computer and use it in GitHub Desktop.
db stats script
package main
import (
"bufio"
"fmt"
"log"
"os"
// "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/pot"
)
type bytes struct {
b []byte
}
func (b *bytes) String() string {
return fmt.Sprintf("%x", b.b)
}
func NewBytes(s string) *bytes {
return &bytes{common.Hex2Bytes(s)}
}
func (self *bytes) PO(val pot.PotVal, pos int) (int, bool) {
b := val.(*bytes)
one := b.b
other := self.b
for i := pos / 8; i < len(one); i++ {
if one[i] == other[i] {
continue
}
oxo := one[i] ^ other[i]
start := 0
if i == pos/8 {
start = pos % 8
}
for j := start; j < 8; j++ {
if (uint8(oxo)>>uint8(7-j))&0x01 != 0 {
return i*8 + j, false
}
}
}
return len(one) * 8, true
}
func main() {
if len(os.Args) < 3 {
log.Fatal("usage: <allchunksfile> (<address> <chunksfile>)+")
}
allchunks := os.Args[1]
chunks := pot.NewPot(nil, 0)
file, err := os.Open(allchunks)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
chunks.Add(NewBytes(scanner.Text()))
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
log.Printf("read a total of %v hashes", chunks.Size())
for i := 2; i < len(os.Args); i += 2 {
addr := NewBytes(os.Args[i])
chunksfile := os.Args[i+1]
file, err := os.Open(chunksfile)
if err != nil {
log.Fatal(err)
}
nodechunks := make(map[string]bool)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
nodechunks[scanner.Text()] = true
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
file.Close()
var prevpo, pocnt, has, total, nodetotal int
prevpo = 256
chunks.EachNeighbour(addr, func(val pot.PotVal, po int) bool {
if prevpo > po {
if nodetotal > 0 {
printstats(prevpo, chunks.Size(), len(nodechunks), total, nodetotal, pocnt, has)
}
prevpo = po
pocnt = 0
has = 0
}
pocnt++
total++
chunk := val.(*bytes)
if nodechunks[chunk.String()] {
nodetotal++
has++
}
return true
})
printstats(prevpo, chunks.Size(), len(nodechunks), total, nodetotal, pocnt, has)
}
}
func printstats(prevpo, chunkscnt, nodecnt, total, nodetotal, pocnt, has int) {
if chunkscnt > 0 && nodecnt > 0 && pocnt > 0 {
log.Printf("PO: %v, Cum Total: %v (%v%%), Node Has: %v/%v (%v%%), Cum Total: %v/%v (%v%%), Cum Coverage: %v/%v (%v%%)\n", prevpo, total, 100*total/chunkscnt, has, pocnt, 100*has/pocnt, nodetotal, nodecnt, 100*nodetotal/nodecnt, nodetotal, total, 100*nodetotal/total)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment