TestSpecs/gen: resolved bugs in generation scripts
[p2p-testing-infrastructure.git] / TestSpecs / gen / rgen
1 #!/bin/bash
2
3 #
4 # Generate R script for plot processing from campaign and scenario
5 # configuration file
6 #
7 # Sample run:
8 #   ./rgen performance-experiments-3.01.cfg > out.R
9 #
10
11 if test $# -ne 1; then
12         echo "Usage: $0 scenario-config-file" 1>&2
13         exit 1
14 fi
15
16 scenario_file=$1
17
18 scenario_name=$(basename ${scenario_file} .cfg)
19
20 declare num_seeders
21 declare num_leechers
22 declare description
23 declare dl_max_limit
24 declare ul_max_limit
25
26 # parse comment zone in scenario file (description, leechers, seeders etc.)
27 parse_comment()
28 {
29         num_leechers=$(grep "^[ \t]*#[ \t]*Number of leechers:" ${scenario_file} | awk -F ":" '{print $2;}')
30         num_seeders=$(grep "^[ \t]*#[ \t]*Number of seeders:" ${scenario_file} | awk -F ":" '{print $2;}')
31         dl_max_limit=$(grep "^[ \t]*#[ \t]*Maximum download:" ${scenario_file} | awk -F ":" '{print $2;}')
32         dl_max_upload=$(grep "^[ \t]*#[ \t]*Maximum upload:" ${scenario_file} | awk -F ":" '{print $2;}')
33         description=$(grep "^[ \t]*#[ \t]*Description:" ${scenario_file} | awk -F ":" '{print $2;}')
34 }
35
36 # test client description for "leecher" string
37 test_is_leecher()
38 {
39         client_type=$1
40
41         grep "leecher" <<<"${client_type}" &> /dev/null
42 }
43
44 # test client description for "seeder" string
45 test_is_seeder()
46 {
47         client_type=$1
48
49         grep "seeder" <<<"${client_type}" &> /dev/null
50 }
51
52 parse_comment
53
54 cat <<END
55 # import ggplot2
56 library(ggplot2)
57
58 # Read transmitted command line arguments
59 args <- commandArgs(trailingOnly = TRUE)
60
61 # The data files are located in the target folder. Also, the graph will be saved in the target folder.
62 target_folder <- args[2]
63 campaign_name <- args[3]
64 rm(args)
65
66 # read data from the data file
67 END
68
69 index=1
70 while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
71         new_port=${port%22}
72         if test -z "${new_port}"; then
73                 base_log_name=${server%%.*}
74         else
75                 base_log_name=${server%%.*}-${new_port}
76         fi
77         if test_is_leecher ${client_type}; then
78                 base_client_name="leecher"
79         elif test_is_seeder ${client_type}; then
80                 base_client_name="seeder"
81         else
82                 base_client_name="tracker"
83         fi
84         if [ ${base_client_name} != "tracker" ]; then
85                 cat <<END
86 ${base_client_name}${index}=read.table(paste(target_folder, "${base_log_name}.log.data", sep="/"), header=T, sep=" ")
87 END
88                 ((index++))
89         fi
90 done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
91
92 cat <<END
93
94 # transform KB/s to Mbit/s
95 END
96
97 index=1
98 while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
99         if ! test_is_leecher ${client_type}; then
100                 ((index++))
101                 continue
102         fi
103         cat <<END
104 leecher${index}\$dlspeed <- leecher${index}\$dlspeed*8/1000
105 END
106         ((index++))
107 done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
108
109 cat <<END
110
111 # plot dlspeed-percent data
112 p <- ggplot() +
113 END
114
115 index=1
116 while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
117         if ! test_is_leecher ${client_type}; then
118                 ((index++))
119                 continue
120         fi
121         cat <<END
122 geom_point(aes(x=leecher${index}\$percent, y=leecher${index}\$dlspeed, label="Leecher ${index}"), size=1) +
123 END
124         ((index++))
125 done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
126
127 cat <<END
128 theme_bw() +
129 scale_x_continuous("Percent", limits=c(0, 100), breaks=seq(0, 100, 10)) +
130 scale_y_continuous("Download speed (Mbit/s)", limits=c(0, ${dl_max_limit}), breaks=seq(0, ${dl_max_limit}, 0.5)) +
131 coord_cartesian() +
132 scale_colour_manual("Legend") +
133 opts(title=paste(campaign_name, "${scenario_name}: a test swarm (${num_seeders} Seeders, ${num_leechers} Leechers), ${description}", sep="\n"))
134
135 # plot data as an eps file
136 postscript(paste(target_folder, "${scenario_name}-dlspeed-percent.eps", sep="/"))
137 print(p)
138 dev.off()
139
140 # plot data as an png file
141 png(paste(target_folder, "${scenario_name}-dlspeed-percent.png", sep="/"), width = 1280, height = 800)
142 print(p)
143 dev.off()
144
145
146 # plot dlspeed-time data
147 p <- ggplot() +
148 END
149
150 index=1
151 while read server port user remote_path remote_if dl_bw dl_burst ul_bw ul_burst num_conn pre_run_script post_run_script client_type torrent_file periods; do
152         if ! test_is_leecher ${client_type}; then
153                 ((index++))
154                 continue
155         fi
156         cat <<END
157 geom_point(aes(x=leecher${index}\$time, y=leecher${index}\$dlspeed, label="Leecher ${index}"), size=1) +
158 END
159         ((index++))
160 done < <(grep -v '^#\|^[ \t]*$' ${scenario_file})
161
162 cat <<END
163 theme_bw() +
164 scale_x_continuous("Time(s)") +
165 scale_y_continuous("Download speed (Mbit/s)", limits=c(0, ${dl_max_limit}), breaks=seq(0, ${dl_max_limit}, 0.5)) +
166 coord_cartesian() +
167 scale_colour_manual("Legend") +
168 opts(title=paste(campaign_name, "${scenario_name}: a test swarm (${num_seeders} Seeders, ${num_leechers} Leechers), ${description}", sep="\n"))
169
170 # plot data as an eps file
171 postscript(paste(target_folder, "${scenario_name}-dlspeed-time.eps", sep="/"))
172 print(p)
173 dev.off()
174
175 # plot data as an png file
176 png(paste(target_folder, "${scenario_name}-dlspeed-time.png", sep="/"), width = 1280, height = 800)
177 print(p)
178 dev.off()
179 END