func Poller(lock *sync.Mutex, ss *spacesaving.Rate, pc *pcap.Pcap) { w := bufio.NewWriter(os.Stdout) for _ = range time.Tick(3 * time.Second) { stat, _ := pc.Getstats() lock.Lock() fmt.Fprintf(w, "\033c") elements := ss.GetAll(time.Now()) for i, e := range elements { fmt.Fprintf(w, "%60s\t%f\t%f\n", e.Key, e.LoRate, e.HiRate) if i > 40 { break } } fmt.Fprintf(w, "\n") fmt.Fprintf(w, "received:%v dropped:%v/%v (software/interface)\n", stat.PacketsReceived, stat.PacketsDropped, stat.PacketsIfDropped) w.Flush() lock.Unlock() } }
func main() { ss := spacesaving.Rate{} slots, err := strconv.ParseInt(os.Args[1], 10, 64) if err != nil { panic(err) } ss.Init(uint32(slots), halfLife) var lastTime time.Time in := bufio.NewReader(os.Stdin) for lineno := 1; true; lineno += 1 { line, err := in.ReadString('\n') if err == io.EOF { break } if err != nil { fmt.Fprintf(os.Stderr, "%s\n", err) os.Exit(1) } line = strings.TrimSpace(line) parts := strings.SplitN(line, ",", 2) ts, err := time.Parse(TimeFormatString, parts[0]) if err != nil { fmt.Fprintf(os.Stderr, "Ignoring line %d: %v\n", lineno, err) continue } key := strings.TrimSpace(parts[1]) ss.Touch(key, ts) lastTime = ts } elements := ss.GetAll(lastTime) for _, e := range elements { fmt.Printf("%s, %f, %f\n", e.Key, e.LoRate, e.HiRate) } }