1import glob 2import sys 3inputDirPath = sys.argv[1] 4 5inputFilePaths = glob.glob(inputDirPath+"/*") 6inputFilePaths.sort() 7 8merged = dict() 9 10stretches = [] 11 12total = 0 13for inputFilePath in inputFilePaths: 14 print "Processing file {}".format(inputFilePath) 15 with open(inputFilePath, 'r') as f: 16 inData = f.readlines() 17 pathsChecked = 0. 18 avgStretch = 0. 19 for line in inData: 20 dat = line.rstrip('\n').split(' ') 21 eHops = int(dat[0]) 22 nHops = int(dat[1]) 23 count = int(dat[2]) 24 if eHops not in merged: merged[eHops] = dict() 25 if nHops not in merged[eHops]: merged[eHops][nHops] = 0 26 merged[eHops][nHops] += count 27 total += count 28 pathsChecked += count 29 stretch = float(nHops)/eHops 30 avgStretch += stretch*count 31 finStretch = avgStretch / max(1, pathsChecked) 32 stretches.append(str(finStretch)) 33 34hopsUsed = 0. 35hopsNeeded = 0. 36avgStretch = 0. 37results = [] 38for eHops in sorted(merged.keys()): 39 for nHops in sorted(merged[eHops].keys()): 40 count = merged[eHops][nHops] 41 result = "{} {} {}".format(eHops, nHops, count) 42 results.append(result) 43 hopsUsed += nHops*count 44 hopsNeeded += eHops*count 45 stretch = float(nHops)/eHops 46 avgStretch += stretch*count 47 print result 48bandwidthUsage = hopsUsed/max(1, hopsNeeded) 49avgStretch /= max(1, total) 50 51with open("results.txt", "w") as f: 52 f.write('\n'.join(results)) 53 54with open("stretches.txt", "w") as f: 55 f.write('\n'.join(stretches)) 56 57print "Total files processed: {}".format(len(inputFilePaths)) 58print "Total paths found: {}".format(total) 59print "Bandwidth usage: {}".format(bandwidthUsage) 60print "Average stretch: {}".format(avgStretch) 61 62 63