Skip to content

Instantly share code, notes, and snippets.

@qrwteyrutiyoup
Created November 3, 2016 15:52
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 qrwteyrutiyoup/97adfd0a26f155317df7354351a35475 to your computer and use it in GitHub Desktop.
Save qrwteyrutiyoup/97adfd0a26f155317df7354351a35475 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
)
type Stats struct {
sent uint32
received uint32
pdr float64
delay float64
throughput float64
overhead float64
}
func print_stats(s Stats) {
fmt.Printf("sent: %d#received: %d#delivery ratio: %f#average end-to-end-delay: %f milliseconds#throughput: %f Mbps#routing overhead: %f#\n", s.sent, s.received, s.pdr, s.delay, s.throughput, s.overhead)
}
func parse_trace(infile string) (bool, Stats) {
stats := Stats{sent: 0, received: 0, pdr: 0.0, delay: 0.0, throughput: 0.0, overhead: 0.0}
file, err := os.Open(infile)
if err != nil {
fmt.Printf("Error opening file '%s': %v\n", infile, err)
return false, stats
}
defer file.Close()
rxRegex := `TraceDelay: RX (\d+) bytes from .*? Sequence Number: \d+ Uid: \d+ TXtime: \+(\d+).0ns RXtime: \+(\d+).0ns Delay: \+(\d+).0ns`
rx := regexp.MustCompile(rxRegex)
protoRegex := `Protocol: (\d+)`
proto := regexp.MustCompile(protoRegex)
var first_tx_time, last_rx_time float64
first_tx_time = 0.0
last_rx_time = 0.0
var converted float64
scanner := bufio.NewScanner(file)
var line string
for scanner.Scan() {
line = scanner.Text()
switch {
case strings.HasPrefix(line, "TraceDelay: RX "):
if matches := rx.FindStringSubmatch(line); matches != nil {
stats.received++
converted, _ = strconv.ParseFloat(matches[1], 64)
stats.throughput += converted
converted, _ = strconv.ParseFloat(matches[2], 64)
if first_tx_time == 0.0 || converted < first_tx_time {
first_tx_time = converted
}
converted, _ = strconv.ParseFloat(matches[3], 64)
if last_rx_time == 0.0 || converted > last_rx_time {
last_rx_time = converted
}
converted, _ = strconv.ParseFloat(matches[4], 64)
stats.delay += converted
}
case strings.HasPrefix(line, "TraceDelay TX "):
stats.sent++
case strings.HasPrefix(line, "Protocol: "):
if proto.MatchString(line) {
if matches := proto.FindStringSubmatch(line); matches != nil {
converted, _ = strconv.ParseFloat(matches[1], 64)
stats.overhead += converted
}
}
}
}
stats.pdr = float64(stats.received) / float64(stats.sent)
// In nanoseconds.
stats.delay /= float64(stats.received)
// In milliseconds.
stats.delay /= 1e6
// In seconds.
sending_time := (last_rx_time - first_tx_time) / 1e9
// In Bps.
stats.throughput /= sending_time
// In MBps.
stats.throughput /= (1024.0 * 1024.0)
// Now Mbps.
stats.throughput *= 8.0
stats.overhead /= float64(stats.received)
return true, stats
}
func main() {
if len(os.Args) == 1 {
fmt.Println("Usage: %s <trace to extract metrics>", os.Args[0])
} else {
infile := os.Args[1]
_, stats := parse_trace(infile)
print_stats(stats)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment