1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"time"
"git.torproject.org/user/phw/zoossh.git"
)
var processedCons int64 = 0
var processedDescs int64 = 0
var totalExits int64 = 0
var totalRelays int64 = 0
var totalBw uint64 = 0
func Min(a uint64, b uint64, c uint64) uint64 {
min := a
if b < min {
min = b
}
if c < min {
min = c
}
return min
}
func ProcessDescriptors(path string, info os.FileInfo, err error) error {
if _, err := os.Stat(path); err != nil {
return err
}
if info.IsDir() {
return nil
}
consensus, err := zoossh.ParseDescriptorFile(path)
if err != nil {
return err
}
if (processedDescs % 100) == 0 {
fmt.Printf(".")
}
for _, getDesc := range consensus.RouterDescriptors {
desc := getDesc()
totalBw += Min(desc.BandwidthAvg, desc.BandwidthBurst, desc.BandwidthObs)
processedDescs++
}
return nil
}
func ProcessConsensus(path string, info os.FileInfo, err error) error {
if _, err := os.Stat(path); err != nil {
return err
}
if info.IsDir() {
return nil
}
consensus, err := zoossh.ParseConsensusFile(path)
if err != nil {
return err
}
fmt.Printf(".")
for _, getStatus := range consensus.RouterStatuses {
status := getStatus()
totalRelays++
if status.Flags.Exit == true {
totalExits++
}
}
processedCons++
return nil
}
func main() {
if len(os.Args) != 3 {
log.Fatalf("Usage: %s CONSENSUS_ARCHIVE DESCRIPTOR_ARCHIVE", os.Args[0])
}
before := time.Now()
filepath.Walk(os.Args[1], ProcessConsensus)
fmt.Println()
after := time.Now()
duration := after.Sub(before)
fmt.Println("Total time for consensuses:", duration)
fmt.Printf("Time per consensus: %dms\n",
duration.Nanoseconds()/processedCons/int64(1000000))
fmt.Printf("Processed %d consensuses with %d router status entries.\n",
processedCons, totalRelays)
fmt.Printf("Total exits: %d\n", totalExits)
before = time.Now()
filepath.Walk(os.Args[2], ProcessDescriptors)
fmt.Println()
after = time.Now()
duration = after.Sub(before)
fmt.Println("Total time for descriptors:", duration)
fmt.Printf("Time per descriptor: %dns\n",
duration.Nanoseconds()/processedDescs)
fmt.Printf("Processed %d descriptors.\n", processedDescs)
fmt.Printf("Average advertised bandwidth: %d\n", totalBw/uint64(processedDescs))
}
|