This repository has been archived by the owner on Nov 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sim.rb
executable file
·521 lines (457 loc) · 17.3 KB
/
sim.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env ruby
# vim:et:sw=2:ts=2
require 'fileutils'
debug = false
basedir = "."
datadir = if debug then "#{basedir}/data2" else "#{basedir}/data" end
graphdir = "#{basedir}/graphs"
tmpdir = '/tmp'
neighborBroadcastPeriod = 30
valid_cmds = [
'run-nofailures',
'run-failures',
'agg-nofailures',
'agg-failures-bandwidth',
'agg-failures-reachability',
'plot-nofailures-bandwidth',
'plot-failures-bandwidth',
'plot-failures-reachability',
]
if ARGV.size != 1 or not valid_cmds.index ARGV[0]
puts "./sim.rb {#{valid_cmds.join '|'}}"
exit 1
end
cmd = ARGV[0].split('-')
mode, subject = cmd[0], cmd[1]
case subject
when 'failures'
failure_rate_range = [5, 25, 50, 75, 90] # [50,90]
num_nodes_range = [50]
num_runs = 2
schemes = ["simple", "sqrt"]
if debug
failure_rate_range = [50,90]
num_nodes_range = [50]
num_runs = 1
schemes = ["sqrt"]
end
when 'nofailures'
failure_rate_range = [0]
#num_nodes_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]
num_nodes_range = [160, 170, 180, 190, 200]
num_runs = 1
schemes = ["simple", "sqrt"]
scheme_labels = ["RON", "RON with new routing algorithm"]
#schemes = ["simple"]
#schemes = ["sqrt"]
end
def sys(cmd)
if not system(cmd)
raise "command failed: #{cmd}"
end
end
def remkdir(path)
begin FileUtils.rm_r(path) ; rescue ; end
FileUtils.mkdir_p(path)
end
get_subdir = '"#{datadir}/#{scheme}/n#{num_nodes}/f#{failure_rate}/r#{run}"'
#sys('make')
def is_nonzero_int_and_file(x)
begin
Integer( File.basename(x) ) != 0 and File.file? x
rescue
false
end
end
$num_runs = num_runs
$get_subdir = get_subdir
$datadir = datadir
$neighborBroadcastPeriod = neighborBroadcastPeriod
def agg_runs(scheme, num_nodes, failure_rate, xs)
datadir = $datadir
nbp = $neighborBroadcastPeriod
if scheme.eql?("sqrt")
nbp /= 2
end
puts nbp
for run in 1..$num_runs
subdir = eval $get_subdir
puts subdir
for path in Dir["#{subdir}/*"].delete_if{|x| not is_nonzero_int_and_file(x)}
File.open(path) do |f|
begin
start_time = Integer(f.grep(/server started/)[0].split(' ', 2)[0])
f.seek(0)
ping_bytes, bytes, end_time = 0, 0, 0
rounds = 0
f.grep(/sent (measurements|recs)/) do |line|
fields = line.split(' ')
end_time = Integer(fields[0])
fields = line.split(', ')
bytes += fields[1].split(' ')[0].to_i
rounds = rounds + 1
end
if scheme.eql?("sqrt")
rounds = rounds / 2
end
if rounds == 0
rounds = 1
end
f.seek(0)
f.grep(/sent ping_pongs/) do |line|
fields = line.split(' ')
end_time = Integer(fields[0])
fields = line.split(', ')
#puts #{fields[1]}
ping_bytes += fields[1].split(' ')[0].to_i
end
#puts ping_bytes
#xs << (bytes.to_f * 8/1000) / ((end_time - start_time) / 1000.0)
#puts (rounds * nbp)
#bytes += ping_bytes
xs << (bytes.to_f * 8/1000) / (rounds * nbp)
rescue
end
end
end
end # run
end
# aggregate xs and append to out file
def append_agg(xs, out, param)
avg_size = xs.inject{|x,y| x+y}.to_f / xs.size.to_f
out.puts "#{param} #{avg_size}"
end
case mode
when 'run'
for scheme in schemes
nbp = $neighborBroadcastPeriod
if scheme.eql?("sqrt")
nbp /= 2
end
puts nbp
for num_nodes in num_nodes_range
for failure_rate in failure_rate_range
for run in 1..num_runs
subdir = eval get_subdir
puts subdir
remkdir(subdir)
# if failures, first generate failure data, and prepare the extra
# arg
if failure_rate > 0
total_time = 300
sys("./rand.py #{failure_rate} #{num_nodes} #{total_time} > #{subdir}/failures.dat")
extra_args = "-DsimData=#{subdir}/failures.dat"
else
extra_args = ''
total_time = 300
end
# run for all configs with the same failure data set.
sys("./run.bash -DenableSubpings=false -DtotalTime=#{total_time} -DconsoleLogFilter=all" +
" -DnumNodes=#{num_nodes} -Dscheme=#{scheme}" +
" -DprobePeriod=30 -DneighborBroadcastPeriod=#{nbp}" +
" #{extra_args}" +
" -DlogFileBase=#{subdir}/ > /dev/null")
end
end
end
end
when 'agg'
case subject
when 'nofailures'
for scheme in schemes
File.open("#{datadir}/bandwidth-#{scheme}-nofailures.dat", 'a') do |out|
for num_nodes in num_nodes_range
xs = []
agg_runs(scheme, num_nodes, 0, xs)
append_agg(xs,out,num_nodes)
end
end
end
# same as above, but one file per num_nodes, and now the x-axis is not
# num_nodes but rather the failure_rate
when 'failures'
case cmd[2]
when 'bandwidth'
#########################################################################
#
# bandwidth
#
#for scheme in schemes
# for num_nodes in num_nodes_range
# File.open("#{datadir}/bandwidth-#{scheme}-failures-n#{num_nodes}.dat", 'a') do |out|
# for failure_rate in failure_rate_range
# xs = []
# agg_runs(scheme, num_nodes, failure_rate, xs)
# append_agg(xs,out,failure_rate)
# end
# end
# end
#end
for num_nodes in num_nodes_range
for scheme in schemes
File.open("#{datadir}/bw_#{num_nodes}_#{scheme}_all.dat", "w") do |out|
for failure_rate in failure_rate_range
xs = []
for run in 1..num_runs
subdir = eval get_subdir
puts "#{subdir}"
# one file per subdir (one line per node) -> "bw in Kbps"
File.open("#{subdir}/bw.dat", "w") do |bw_dat|
for path in Dir["#{subdir}/*"].delete_if{|x| not is_nonzero_int_and_file(x)}
File.open(path) do |f|
begin
start_time = Integer(f.grep(/server started/)[0].split(' ', 2)[0])
bytes, end_time = 0, 0
# count outgoing messages
f.seek(0)
f.grep(/sent (measurements|recs)/) do |line|
fields = line.split(' ')
end_time = [end_time, Integer(fields[0])].max()
fields = line.split(', ')
bytes += fields[1].split(' ')[0].to_i
end
# count incoming messages
f.seek(0)
regex = Regexp.new('(\d+) .*recv.(?:Measurements|RoutingRecs).*len (\d+)')
f.each do |line|
match = regex.match(line)
if match
end_time = [end_time, Integer(match[1])].max()
bytes += match[2].to_i
end
end
node_bw = (bytes.to_f * 8/1000) / ((end_time - start_time) / 1000.0)
bw_dat.puts "#{node_bw}"
rescue
end
end
end
end
end #runs
# find (min, max, average) across runs, of the averages in each run
f_dir = "#{datadir}/#{scheme}/n#{num_nodes}/f#{failure_rate}"
# "min, max, mean" of averages, across runs
# has one line per run
File.open("#{f_dir}/avg_bw.dat", "w") do |avg_bw_dat|
for run in 1..num_runs
stats = `cat #{f_dir}/r#{run}/bw.dat | ../tools/UnixStat/bin/stats min max mean`
avg_bw_dat.puts "#{stats}"
end
end
min_bw_across_runs = `cat #{f_dir}/avg_bw.dat | awk '{print $1}' | ../tools/UnixStat/bin/stats mean`
max_bw_across_runs = `cat #{f_dir}/avg_bw.dat | awk '{print $2}' | ../tools/UnixStat/bin/stats mean`
avg_bw_across_runs = `cat #{f_dir}/avg_bw.dat | awk '{print $3}' | ../tools/UnixStat/bin/stats mean`
outline = "#{failure_rate} #{avg_bw_across_runs.chomp} #{min_bw_across_runs.chomp} #{max_bw_across_runs.chomp}"
puts outline
out.puts outline
end
end
end
end
when 'reachability'
#########################################################################
#
# reachability
#
for num_nodes in num_nodes_range
for scheme in schemes
File.open("#{datadir}/reachable_#{num_nodes}_#{scheme}.dat", "w") do |out|
for failure_rate in failure_rate_range
for run in 1..num_runs
subdir = eval get_subdir
puts "#{subdir}"
# one file per subdir (one line per node) -> "min, max, mean"
reachable_dat = File.new("#{subdir}/reachable.dat", "w")
available_dat = File.new("#{subdir}/available_hops.dat", "w")
for path in Dir["#{subdir}/*"].delete_if{|x| not is_nonzero_int_and_file(x)}
File.open(path) do |f|
begin
num_avg = 0
# one tmp file per node for stats -> "#{live_nodes} #{num_paths} #{num_nodes}"
File.open("#{tmpdir}/stats_n#{num_nodes}_tmp.dat", "w") do |tmp_file|
start_time = Integer(f.grep(/server started/)[0].split(' ', 2)[0])
f.seek(0)
live_nodes, avg_paths, count, end_time = 0, 0, 0, 0
f.grep(/ live nodes, /) do |line|
fields = line.split(': ')
end_time = Integer(fields[0].split(' ')[0])
fields = fields[1].split(', ')
live_nodes = fields[0].split(' ')[0].to_i
num_paths = fields[1].split(' ')[0].to_i
#direct_paths = fields[2].split(' ')[0].to_i
count += 1
tmp_file.puts "#{live_nodes} #{num_paths} #{num_nodes}"
end
end
stats = `cat #{tmpdir}/stats_n#{num_nodes}_tmp.dat | awk '{print $1}' | ..//tools/UnixStat/bin/stats min max mean`
stats2 = `cat #{tmpdir}/stats_n#{num_nodes}_tmp.dat | awk '{print $2}' | ..//tools/UnixStat/bin/stats min max mean`
#stats3 = `cat #{tmpdir}/stats_n#{num_nodes}_tmp.dat | awk '{print $3}' | ..//tools/UnixStat/bin/stats min max mean`
# puts "reachable => #{stats}"
# puts "available hops => #{stats2}"
reachable_dat.puts "#{stats}"
available_dat.puts "#{stats2}"
rescue
end
end
end
reachable_dat.close
available_dat.close
end #runs
# find (min, max, average) across runs, of the averages in each run
f_dir = File.dirname subdir # "#{datadir}/#{scheme}/n#{num_nodes}/f#{failure_rate}"
# "min, max, mean" of averages, across runs
# has one line per run
avg_reachable_dat = File.new("#{f_dir}/avg_reachable.dat", "w")
avg_available_hops_dat = File.new("#{f_dir}/avg_available_hops.dat", "w")
for run in 1..num_runs
# we are interested in the mean value in each of these files
stats = `cat #{f_dir}/r#{run}/reachable.dat | awk '{print $3}' | ..//tools/UnixStat/bin/stats min max mean`
avg_reachable_dat.puts "#{stats}"
stats2 = `cat #{f_dir}/r#{run}/available_hops.dat | awk '{print $3}' | ..//tools/UnixStat/bin/stats min max mean`
avg_available_hops_dat.puts "#{stats2}"
end
avg_reachable_dat.close
avg_available_hops_dat.close
avg_reachability_across_runs = `cat #{f_dir}/avg_reachable.dat | awk '{print $3}' | ..//tools/UnixStat/bin/stats mean`
avg_available_hops_across_runs = `cat #{f_dir}/avg_available_hops.dat | awk '{print $3}' | ..//tools/UnixStat/bin/stats mean`
puts "#{failure_rate} #{avg_reachability_across_runs.chomp} #{avg_available_hops_across_runs.chomp}"
out.puts "#{failure_rate} #{avg_reachability_across_runs.chomp} #{avg_available_hops_across_runs.chomp}"
end
end
end
end
# for scheme in schemes
# for num_nodes in num_nodes_range
# File.open("#{datadir}/availability-#{scheme}-failures-n#{num_nodes}.dat", 'a') do |out|
# for failure_rate in failure_rate_range
# xs = []
#
# for run in 1..num_runs
# subdir = eval get_subdir
# puts subdir
#
# for path in Dir["#{subdir}/*"].delete_if{|x| not is_nonzero_int_and_file(x)}
# File.open(path) do |f|
# begin
# start_time = Integer(f.grep(/server started/)[0].split(' ', 2)[0])
# f.seek(0)
# live_nodes, avg_paths, count, end_time = 0, 0, 0, 0
# f.grep(/ live nodes, /) do |line|
# fields = line.split(': ')
# end_time = Integer(fields[0].split(' ')[0])
# fields = fields[1].split(', ')
# live_nodes += fields[0].split(' ')[0].to_i
# avg_paths += fields[1].split(' ')[0].to_i
# count += 1
# end
# xs << (bytes.to_f * 8/1000) / ((end_time - start_time) / 1000.0)
# rescue
# end
# end
# end
#
# end # run
#
# append_agg(xs,out,failure_rate)
# end
# end
# end
# end
end # case cmd[2]
end # case subject
#
# plotting
#
when 'plot'
case subject
when 'nofailures'
remkdir(graphdir)
for gtype in ["monochrome", "color"]
plots = []
i = 0
for scheme in schemes
plots << "'#{datadir}/bandwidth-#{scheme}-nofailures.dat' using 1:2 with linespoints title '#{scheme_labels[i]}'"
i += 1
end
plots = plots.join(', ')
File.open("bandwidth.gnuplot", "w") do |out|
out.puts %Q{
set terminal postscript eps #{gtype}
set size 0.65
set output '#{graphdir}/bandwidth_#{gtype}.eps'
set title "Comparison of bandwidth overhead"
set xlabel "number of nodes"
set ylabel "bandwidth overhead (Kbps)"
set key left top
plot #{plots}
}
end
sys("cat bandwidth.gnuplot | gnuplot -")
sys("rm bandwidth.gnuplot")
end
when 'failures'
case cmd[2]
when 'bandwidth'
for num_nodes in [50]
for gtype in ["monochrome", "color"]
plots = []
i = 0
for scheme in schemes
puts "'#{datadir}/bw_#{num_nodes}_#{scheme}_all.dat' with yerrorbars title '#{scheme_labels[i]}'"
plots << "'#{datadir}/bw_#{num_nodes}_#{scheme}_all.dat' with yerrorbars title '#{scheme_labels[i]}'"
i += 1
end
plots = plots.join(', ')
# set output '#{graphdir}/bandwidth_#{gtype}.eps'
File.open("bandwidth.gnuplot", "w") do |out|
out.puts %Q{
set terminal postscript eps #{gtype}
set size 0.65
set output '#{graphdir}/bandwidth_vs_failures_#{num_nodes}nodes_#{gtype}.eps'
set title "Comparison of routing bandwidth overhead (min, mean, max)"
set xlabel "failure percentage"
set ylabel "routing bandwidth overhead (Kbps)"
set xrange [0:100]
set yrange [0:]
set key left bottom
plot #{plots}
}
end
sys("cat bandwidth.gnuplot | gnuplot -")
sys("rm bandwidth.gnuplot")
end
end
when 'reachability'
for num_nodes in num_nodes_range
for gtype in ["monochrome", "color"]
plots = []
for scheme in schemes
for metric, pos in [['reachable nodes',2],['mean available hops',3]]
plots << "'#{datadir}/reachable_#{num_nodes}_#{scheme}.dat' using 1:#{pos} with linespoints title '#{scheme} - #{metric}'"
end
end
plots = plots.join(', ')
# set output '#{graphdir}/bandwidth_#{gtype}.eps'
File.open("bandwidth.gnuplot", "w") do |out|
out.puts %Q{
set terminal postscript eps #{gtype}
set size 0.65
set output '#{graphdir}/reachability_vs_failures_#{num_nodes}nodes_#{gtype}.eps'
set title "Reachability"
set xlabel "failure percentage"
set ylabel "average # of reachable nodes per node"
set xrange [0:100]
set yrange [0:]
set key left bottom
plot #{plots}
}
end
sys("cat bandwidth.gnuplot | gnuplot -")
sys("rm bandwidth.gnuplot")
end
end
#sys("rm #{datadir}/*.dat")
#sys("rm #{tmpdir}/*.dat")
end
end
end