-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·70 lines (60 loc) · 2.18 KB
/
main.py
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
#! /usr/bin/env python
import networkx as nx
from datetime import datetime as dati
import datetime
import dateutil.relativedelta
import argparse
from collections import defaultdict
import statistics
from multiprocessing import Pool, Manager
import random
import sys
def parse_args():
p = argparse.ArgumentParser()
p.add_argument("-s", help="starting number of nodes", default=100,
type=int)
p.add_argument("-e", help="ending number of nodes", default=1000, type=int)
p.add_argument("-d", help="delta from one test to the other", default=100,
type=int)
p.add_argument("-r", help="runs per size", default=10, type=int)
p.add_argument("-p", help="processes", default=1, type=int)
p.add_argument("-S", help="save the generated graph", default='',
type=str, nargs='?', const='/tmp')
args = p.parse_args()
return args
def gen_graph(x):
start_time = dati.now()
g = nx.internet_as_graph(x)
end_time = dati.now()
return x, g, (end_time-start_time).total_seconds()
def print_res(times, runs):
padding = 20
if not times:
print("size".ljust(padding), "average time (s)".ljust(padding),
"std dev (s)".ljust(padding), "human readable".ljust(padding))
return
for x in times:
if len(times[x]) == runs and x not in printed_res:
human_r = str(datetime.timedelta(seconds=sum(times[x])/len(times[x])))
print(str(x).ljust(padding),
str(round(statistics.mean(times[x]), 5)).ljust(padding),
str(round(statistics.stdev(times[x]), 5)).ljust(padding),
human_r.ljust(padding))
printed_res.add(x)
sys.stdout.flush()
args = parse_args()
times = defaultdict(list)
graphs = defaultdict(list)
printed_res = set()
arg_list = []
for x in range(args.s, args.e+1, args.d):
arg_list += [x]*args.r
pool = Pool(args.p)
print_res(times, args.r)
for x, g, t in pool.imap_unordered(gen_graph, arg_list):
times[x].append(t)
graphs[x].append(g)
print_res(times, args.r)
if args.S:
nx.write_graphml(g, "%s/internet-AS-graph-%d-%d.graphml" % (
args.S, x, len(graphs[x])))