Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSON format output #3

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
Loading