Skip to content

Commit

Permalink
Support JSON format output
Browse files Browse the repository at this point in the history
Signed-off-by: gdmmx <[email protected]>
  • Loading branch information
gdmmx authored and yilunzhang committed Apr 2, 2024
1 parent fdcd45d commit 2e5df28
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,14 @@ func main() {
var (
rpcAddr string
m, n int
jsFmt bool
version bool
)

flag.StringVar(&rpcAddr, "rpc", "http://seed.nkn.org:30003", "Initial RPC address in the form ip:port")
flag.IntVar(&m, "m", 8, "Number of concurrent goroutines")
flag.IntVar(&n, "n", 8, "Number of steps to repeat")
flag.BoolVar(&jsFmt, "json", false, "Print version")
flag.BoolVar(&version, "version", false, "Print version")
flag.Parse()

Expand Down Expand Up @@ -290,9 +292,24 @@ func main() {
uncertainty := new(big.Int).Div(new(big.Int).Mul(big.NewInt(int64(math.Sqrt(float64(totalNodesVisited)))), totalSpace), totalArea).Int64()
estimatedRelayPerSecond := float64(totalRelay) / float64(totalUptime) * float64(estimatedTotalNodes) / (math.Log2(float64(estimatedTotalNodes)) / 2)

log.Printf("Total nodes visited: %d\n", totalNodesVisited)
log.Printf("Total area covered: %.2f%%\n", 100*float64(totalNodesVisited)/float64(estimatedTotalNodes))
log.Printf("Estimated total number of nodes in the network: %d +- %d\n", estimatedTotalNodes, uncertainty)
log.Printf("Estimated network relay per second: %.0f\n", estimatedRelayPerSecond)
log.Printf("Time used: %v\n", time.Since(timeStart))
if jsFmt {
jsStr, err := json.Marshal(map[string]float64{
"visited": float64(totalNodesVisited),
"covered": 100 * float64(totalNodesVisited) / float64(estimatedTotalNodes),
"Estimated": float64(estimatedTotalNodes),
"uncertainty": float64(uncertainty),
"relayPS": estimatedRelayPerSecond,
"time": float64(time.Since(timeStart)),
})
if err != nil {
log.Fatalln("json formatter error")
}
fmt.Println(string(jsStr))
} else {
log.Printf("Total nodes visited: %d\n", totalNodesVisited)
log.Printf("Total area covered: %.2f%%\n", 100*float64(totalNodesVisited)/float64(estimatedTotalNodes))
log.Printf("Estimated total number of nodes in the network: %d +- %d\n", estimatedTotalNodes, uncertainty)
log.Printf("Estimated network relay per second: %.0f\n", estimatedRelayPerSecond)
log.Printf("Time used: %v\n", time.Since(timeStart))
}
}

0 comments on commit 2e5df28

Please sign in to comment.