Skip to content

Commit

Permalink
Rewrite to be more automated.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhigh committed Jan 6, 2016
1 parent d56a87f commit 0dffd90
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 122 deletions.
10 changes: 10 additions & 0 deletions data/home_cern/ipinfo.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"ip","country_code","country_name","region_code","region_name","city","zip_code","time_zone","latitude","longitude","metro_code","mean_rtt"
"128.95.230.102","US","United States","WA","Washington","Seattle","98105","America/Los_Angeles",47.6606,-122.2919,"819",0
"209.124.181.134","US","United States","WA","Washington","Seattle","98105","America/Los_Angeles",47.6606,-122.2919,"819",0
"64.57.28.53","US","United States","MI","Michigan","Ann Arbor","48104","America/Detroit",42.2734,-83.7133,"505",0
"198.71.45.24","US","United States","MI","Michigan","Ann Arbor","48104","America/Detroit",42.2734,-83.7133,"505",18.886
"198.71.45.18","US","United States","MI","Michigan","Ann Arbor","48104","America/Detroit",42.2734,-83.7133,"505",34.893
"198.71.45.14","US","United States","MI","Michigan","Ann Arbor","48104","America/Detroit",42.2734,-83.7133,"505",46.863
"192.91.246.126","CH","Switzerland","GE","Geneva","Geneva","1205","Europe/Zurich",46.1956,6.1481,"0",182.871
"192.65.196.38","CH","Switzerland","GE","Geneva","Geneva","1205","Europe/Zurich",46.1956,6.1481,"0",166.646333333333
"188.184.37.205","CH","Switzerland","GE","Geneva","Meyrin","1217","Europe/Zurich",46.2324,6.0502,"0",164.322
12 changes: 12 additions & 0 deletions data/home_cern/route.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"addr","mean_rtt","rtt.1","rtt.2","rtt.3"
"128.95.230.102",0,0,0,0
"10.132.1.73",0,0,0,0
"10.132.1.75",0,0,0,0
"209.124.181.134",0,0,0,0
"64.57.28.53",0,0,0,0
"198.71.45.24",18.886,18.886,18.886,18.886
"198.71.45.18",34.893,34.893,34.893,34.893
"198.71.45.14",46.863,46.863,46.863,46.863
"192.91.246.126",182.871,182.871,182.871,182.871
"192.65.196.38",166.646333333333,163.985,167.977,167.977
"188.184.37.205",164.322,163.993,163.993,164.98
16 changes: 16 additions & 0 deletions data/home_cern/route.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
traceroute to home.cern (188.184.37.205), 30 hops max, 60 byte packets
1 128.95.230.102 0.000 ms 0.000 ms 0.000 ms
2 10.132.1.73 0.000 ms 0.000 ms 0.000 ms
3 10.132.1.75 0.000 ms 0.000 ms 0.000 ms
4 209.124.181.134 0.000 ms 0.000 ms 0.000 ms
5 64.57.28.53 0.000 ms 0.000 ms 0.000 ms
6 198.71.45.24 18.886 ms 18.886 ms 18.886 ms
7 198.71.45.18 34.893 ms 34.893 ms 34.893 ms
8 198.71.45.14 46.863 ms 46.863 ms 46.863 ms
9 192.91.246.126 182.871 ms 182.871 ms 182.871 ms
10 * * *
11 * * *
12 192.65.196.38 163.985 ms 167.977 ms 167.977 ms
13 * * *
14 * * *
15 188.184.37.205 163.993 ms 163.993 ms 164.980 ms
4 changes: 0 additions & 4 deletions data/udrive_uw_edu/ipinfo.csv

This file was deleted.

5 changes: 0 additions & 5 deletions data/udrive_uw_edu/route.csv

This file was deleted.

4 changes: 0 additions & 4 deletions data/udrive_uw_edu/route_4225roo_linux.txt

This file was deleted.

10 changes: 0 additions & 10 deletions data/udrive_uw_edu/route_4225roo_windows.txt

This file was deleted.

4 changes: 0 additions & 4 deletions data/udrive_uw_edu/route_mhsf_osx.txt

This file was deleted.

160 changes: 65 additions & 95 deletions plot_route.R
Original file line number Diff line number Diff line change
@@ -1,96 +1,66 @@
# Plot network hops to reach hosts by round-trip time (rtt) with error bars.

# Data files were created with these commands from the Terminal (console):
#
# DOS> tracert -d example.com > "route.txt"
#
# bash-linux$ sudo traceroute -I \
# -n example.com 2>&1 | sed 's/\*/* ms/g' | tr -s " " "|" | \
# tail -n +2 > "route.txt"
#
# bash-osx$ traceroute -I \
# -n example.com 2>&1 | sed 's/\*/* ms/g' | tr -s " " "|" | \
# tail -n +3 > "route.txt"

# Function to install packages as needed then load them into R.
load_packages <- function(pkgs) {
# Install packages (if necessary) and load them into memory.

for (pkg in pkgs) {
if (! suppressWarnings(suppressPackageStartupMessages(require(
pkg, character.only=TRUE, quietly=TRUE))) ) {
install.packages(pkg, repos="http://cran.fhcrc.org",
dependencies=TRUE)
if (! suppressWarnings(suppressPackageStartupMessages(require(
pkg, character.only=TRUE, quietly=TRUE))) ) {
stop(paste0(c("Can't load package: ", pkg, "!"),
collapse = ""))
}
}
}
}

# Function to import route hop data from text files.
import.route <- function(fname, troute.type, host) {
# We have two different formats, so parse differently for each type.
if (troute.type == "tracert") {
route <- read.fwf(fname,
widths=c(3, 6, 3, 6, 3, 6, 3, 16),
header=F, skip=4, strip.white=TRUE,
stringsAsFactors=F)
route <- route[,c(1, 2, 4, 6, 8)]
route <- route[complete.cases(route),]
names(route) <- c("hop", "query.1", "query.2", "query.3", "ip.addr")
route[,2:4] <- as.numeric(gsub("[<*]", "", as.matrix(route[2:4])))
} else {
route <- read.table(fname, sep="|", header=F, stringsAsFactors=F)
route <- route[,c(2, 3, 4, 6, 8)]
route <- route[complete.cases(route),]
names(route) <- c("hop", "ip.addr", "query.1", "query.2", "query.3")
route[,3:5] <- as.numeric(gsub("<", "", as.matrix(route[3:5])))
route <- route[, c(1, 3:5, 2)]
}

route$hop <- sprintf("%02d", as.numeric(route$hop))
route$hop <- as.factor(route$hop)
route.long <- melt(route, id=c("hop","ip.addr"))
names(route.long) <- c("hop", "ip.addr", "query", "rtt")
route.long$host <- rep(host, nrow(route.long))
route.long$query <- as.factor(gsub("query.", "", route.long$query,
fixed = TRUE))
return(na.omit(as.data.table(route.long)))
}

# Load packages needed for this script.
load_packages(c("reshape", "Rmisc", "ggplot2", "data.table"))

# Define information about the data files to be used as route data.
files <- c("data/www_gov_za/route.txt",
"data/www_gov_bb/route.txt",
"data/www_gov_ro/route.txt",
"data/www_cubagob_cu/route.txt")
types <- c("tracert", "tracert", "tracert", "tracert")
hosts <- c("www.gov.za", "www.gov.bb", "www.gov.ro", "www.cubagob.cu")
route.info <- cbind(file=files, type=types, host=hosts)

# Process the routes using the route info.
routes <- rbindlist(lapply(1:nrow(route.info),
function(n) import.route(files[n], types[n], hosts[n])))

# Combine all route data into a singe data table.
route.summary <- summarySE(routes, measurevar="rtt", groupvars=c("host", "hop"))


# Make a basic line and point graph.

# Plotting code (below) based on this example:
# http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)

# Use 95% confidence interval for error bars.
g <- ggplot(route.summary, aes(x=hop, y=rtt, colour=host)) +
geom_errorbar(aes(ymin=rtt-ci, ymax=rtt+ci), width=.1) +
geom_line(aes(group=host)) +
geom_point() + theme_bw() +
xlab("network hop") + ylab("round-trip time (ms)") +
ggtitle("Round-trip time (ms) per network hop")
# Plot network hops to reach hosts by round-trip time (rtt) with error bars.

# Function to install packages as needed then load them into R.
load_packages <- function(pkgs) {
# Install packages (if necessary) and load them into memory.

for (pkg in pkgs) {
if (! suppressWarnings(suppressPackageStartupMessages(require(
pkg, character.only=TRUE, quietly=TRUE))) ) {
install.packages(pkg, repos="http://cran.fhcrc.org",
dependencies=TRUE)
if (! suppressWarnings(suppressPackageStartupMessages(require(
pkg, character.only=TRUE, quietly=TRUE))) ) {
stop(paste0(c("Can't load package: ", pkg, "!"),
collapse = ""))
}
}
}
}

# Function to import and tidy route hop data from CSV file.
import.route <- function(fname, host) {
if (file.exists(fname) == TRUE) {
route <- read.table(fname, sep=",", header=T, stringsAsFactors=F)
route$hop <- row.names(route)
route$mean_rtt <- NULL
route$hop <- sprintf("%02d", as.numeric(route$hop))
route$hop <- as.factor(route$hop)
route.long <- melt(route, id=c("hop","addr"))
names(route.long) <- c("hop", "addr", "query", "rtt")
route.long$host <- rep(host, nrow(route.long))
route.long$query <- as.factor(gsub("rtt.", "", route.long$query,
fixed = TRUE))
return(na.omit(as.data.table(route.long)))
} else {
return(as.data.table(NULL))
}
}

# Load packages needed for this script.
load_packages(c("reshape", "Rmisc", "ggplot2", "data.table"))

# Search the "data" folder for "route.csv" files.
hosts <- gsub("_", ".", list.dirs('data', recursive=FALSE, full.names=FALSE))
files <- gsub("$", "/route.csv", list.dirs('data', recursive=FALSE))

# Process the routes using the route info.
routes <- rbindlist(lapply(1:length(files),
function(n) import.route(files[n], hosts[n])))

# Combine all route data into a singe data table.
route.summary <- summarySE(routes, measurevar="rtt", groupvars=c("host", "hop"))

# Make a basic line and point graph with error bars.

# Plotting code (below) based on this example:
# http://www.cookbook-r.com/Graphs/Plotting_means_and_error_bars_(ggplot2)

# Use 95% confidence interval for error bars.
g <- ggplot(route.summary, aes(x=hop, y=rtt, colour=host)) +
geom_errorbar(aes(ymin=rtt-ci, ymax=rtt+ci), width=.1, size=2) +
geom_line(aes(group=host), size=2) +
theme_bw() +
xlab("network hop") + ylab("round-trip time (ms)") +
ggtitle("Round-trip time (ms) per network hop")
plot(g)

0 comments on commit 0dffd90

Please sign in to comment.