Ticket #13192: plot.R

File plot.R, 1.8 KB (added by karsten, 5 years ago)

R script to plot stats of hidden service usage in the Tor network

Line 
1# Plot stats of hidden service usage in the Tor network (#13192)
2#
3# Howto:
4#  - Create in/ and out/ subdirectories.
5#  - Copy tor logs containing hidden-service heartbeats into separate
6#    files in in/ subdirectory.
7#  - Convert logs in in/ to space-separated value files in out/, and
8#    exclude first line which may not contain full 30-minute interval:
9#    for i in $(ls in); do \
10#      grep "Heartbeat.*hidden service.*handled\.$" in/$i | \
11#      cut -d" " -f6,9,13,17 | tail -n+2 | sed 's/|/ /g' > out/$i; done
12#  - Install R and ggplot2, e.g.:
13#    apt-get install r-base r-cran-ggplot2
14#  - Run this script:
15#    R --slave -f plot.R
16
17require(ggplot2)
18require(reshape)
19require(scales)
20require(RColorBrewer)
21
22for (file in list.files("out")) {
23  if (!exists("d")) {
24    d <- data.frame(read.table(paste("out", file, sep = "/")),
25                    relay = file)
26  } else {
27    d <- rbind(d, data.frame(read.table(paste("out", file, sep = "/")),
28                             relay = file))
29  }
30}
31d <- aggregate(list(exit = d$V2, middle = d$V6, hs = d$V4, other = d$V8),
32               by = list(relay = d$relay), FUN = sum)
33
34ggplot(melt(d, id = "relay"),
35       aes(x = relay, y = value, fill = variable)) +
36geom_bar(stat = "identity", position = "dodge") +
37scale_x_discrete("") +
38scale_y_continuous("") +
39scale_fill_brewer("", palette = "Set1") +
40ggtitle("Cells handled by circuit position/type") +
41theme(legend.position = "top", legend.direction='horizontal')
42ggsave("cells-types.png", width = 8, height = 5, dpi = 100)
43
44hs_color = brewer.pal(4, name = "Set1")[3]
45ggplot(d, aes(x = relay, y = hs / (exit + middle + hs + other))) +
46geom_bar(stat = "identity", fill = hs_color) +
47scale_x_discrete("") +
48scale_y_continuous("", labels = percent) +
49ggtitle("Fraction of cells seen on hidden-service circuits\n")
50ggsave("cells-hs.png", width = 8, height = 5, dpi = 100)
51