def testRocketFuel3967(): filepath = '../../../graphs/rocketfuel/3967/weights.intra' g, lli = utils.textToG(filepath, useInt=False) weighted = True testCases = ( ('317', '431', '010011100011001010001010001100101001001000110000100101001000',), ) failureSrcDstPairs = [] for s,d, expectedString in testCases: expectedEncodingBs = bitstring.BitString('0b' + expectedString) pp, dp = approach2.getDg(g, s, d, weighted) ## dag = approach2.getDagWithVnodes( ## pp, dp, returnDetourPathsWithVNodes=False) encodingBs = encode( pp, dp, lli, s, d, returnActualEncoding=True, roundUpToMultipleBits=1) if encodingBs != expectedEncodingBs: failureSrcDstPairs.append((s,d)) pass pass func_name = inspect.getframeinfo(inspect.currentframe())[2] print 'Test', func_name, 'result:' if len(failureSrcDstPairs) == 0: print ' passed' pass else: print ' failed' print ' The failed src-dst pairs:' for s,d in (failureSrcDstPairs): print 's,d=%s,%s' %(repr(s),repr(d)) pass pass return
def evalOnePair(s, d, hdrLenCounts, pairsWithLargeEncodings): pp, dp = approach2.getDg(g, s, d, weighted) if (not pp): print 'no path: s,d="%s","%s"' % (s,d) return if dp == None: dp = {} pass hdrLen4 = codec4.encode( pp, dp, lli, s, d, roundUpToMultipleBits=8) if hdrLen4 > headerLengthThreshold: pairsWithLargeEncodings.append((s,d)) pass addHdrLen(hdrLenCounts, hdrLen4) return
pass pass ########################### # now that we have the pairs we want to eval, eval them outputFile = open(outputFilePath, 'w') outputFile.write('\n\xff %s, codec3, argv: %s\n' % (Revision, str(sys.argv))) for i, (s, d) in enumerate(srcDstPairs): #### use approach2 # comment must start with 2 bytes '\n\xff' outputFile.write('\n\xff pair num %u s,d=%s,%s...' % (i,s,d)) pp, dp = approach2.getDg(g, s, d, weighted) if (not pp): print 'no path: s,d="%s","%s"' % (s,d) outputFile.write(' no path.\n') continue if dp == None: dp = {} pass # end the comment, must end with 1 byte '\n' outputFile.write('\n') bs = codec3.encode(pp,dp,lli,s,d,roundUpToMultipleBits=1,returnActualEncoding=True) asBytes = bs.tobytes()
def evalOneFile(filename, numberOfPairsToTry, pairIsOrdered=False, weighted=False): startdateSecs = int(time.time()) print ''' _______________________________________________ filename: [%s] start date: [%s] ''' % (filename, time.ctime(startdateSecs)) g, lli = utils.textToG(filename, useInt=False) allNodes = g.nodes() numNodes = len(allNodes) i = 0 srcDstPairResults3 = {} if numberOfPairsToTry > 0: while i < numberOfPairsToTry: # this is crypto random integer idx1 = (struct.unpack('I', os.urandom(4))[0]) % numNodes idx2 = (struct.unpack('I', os.urandom(4))[0]) % numNodes while idx2 == idx1: idx2 = (struct.unpack('I', os.urandom(4))[0]) % numNodes pass s,d = allNodes[idx1], allNodes[idx2] if (s,d) in srcDstPairResults3: # definitely skip print 'pair (%s,%s) already encountered -> skip' % (s,d) continue elif (d,s) in srcDstPairResults3: # not seen (s,d) yet but seen (d,s), should skip or not? if not pairIsOrdered: print 'pairs are not ordered, and (%s,%s) already encountered -> skip' % (d,s) continue pass # do this so we know we have seen this (s, d) pair srcDstPairResults3[(s, d)] = None # init to None (which will # mean disconnected) print 's,d="%s","%s"' % (s,d) i += 1 pass # end while i < numberOfPairsToTry pass # end if numberOfPairsToTry > 0 else: assert numberOfPairsToTry > 0 pass ########################### # now that we have the pairs we want to eval, eval them for s, d in srcDstPairResults3.keys(): #### use approach2 pp, dp = approach2.getDg(g, s, d, weighted) if (not pp) or (not dp): print 'no path: s,d="%s","%s"' % (s,d) continue numPNodes = len(pp) - 1 # don't want to count the destination fractionWithDetour = len(dp) / float(numPNodes) srcDstPairResults3[(s, d)] = SrcDstPairResult3( fractionWithDetour) pass # end while loop enddateSecs = int(time.time()) fileResult3 = FileResult3(filename, startdateSecs, enddateSecs, srcDstPairResults3) return fileResult3
def evalOneFile(filename, numberOfPairsToTry, pairIsOrdered=False, weighted=False): startdateSecs = int(time.time()) print ''' _______________________________________________ filename: [%s] start date: [%s] ''' % (filename, time.ctime(startdateSecs)) g, lli = utils.textToG(filename, useInt=False, ignoreWeights=(not weighted)) allNodes = g.nodes() numNodes = len(allNodes) i = 0 srcDstPairResults3 = {} if numberOfPairsToTry > 0: while i < numberOfPairsToTry: # this is crypto random integer idx1 = (struct.unpack('I', os.urandom(4))[0]) % numNodes idx2 = (struct.unpack('I', os.urandom(4))[0]) % numNodes while idx2 == idx1: idx2 = (struct.unpack('I', os.urandom(4))[0]) % numNodes pass s,d = allNodes[idx1], allNodes[idx2] if (s,d) in srcDstPairResults3: # definitely skip print 'pair (%s,%s) already encountered -> skip' % (s,d) continue elif (d,s) in srcDstPairResults3: # not seen (s,d) yet but seen (d,s), should skip or not? if not pairIsOrdered: print 'pairs are not ordered, and (%s,%s) already encountered -> skip' % (d,s) continue pass # do this so we know we have seen this (s, d) pair srcDstPairResults3[(s, d)] = None # init to None (which will # mean disconnected) print 's,d="%s","%s"' % (s,d) i += 1 pass # end while i < numberOfPairsToTry pass # end if numberOfPairsToTry > 0 else: # numberOfPairsToTry is <= 0, so we do all (un-ordered) # pairs. the graph'd better be not too big. for i in range(numNodes - 1): for j in range(i + 1, numNodes): s = allNodes[i] d = allNodes[j] print 's,d="%s","%s"' % (s,d) srcDstPairResults3[(s, d)] = None # init to None (which will # mean disconnected) pass pass pass ########################### # now that we have the pairs we want to eval, eval them for s, d in srcDstPairResults3.keys(): #### use approach2 hdrLens2Normal = {} dagsizeNormal = [] hdrLens2Smaller = {} dagsizeSmaller = [] pp, dp = approach2.getDg(g, s, d, weighted=weighted, everyOtherNodeUsePredecessor=False) if (not pp) or (not dp): print 'no path: s,d="%s","%s"' % (s,d) continue for hdrlenDict, dagsizeList, onlyLaterTwoThirds \ in ((hdrLens2Normal, dagsizeNormal, False), (hdrLens2Smaller, dagsizeSmaller, True)): # if onlyLaterTwoThirds if onlyLaterTwoThirds: # remove from "dp" entries of the first 1/3 of pnodes pplen = len(pp) for pnode in pp[:int(float(pplen)/3)]: if pnode in dp: del dp[pnode] pass pass pass dag, virtualDetourPaths = approach2.getDagWithVnodes( pp, dp, returnDetourPathsWithVNodes=True) dagsizeList.append(dag.number_of_edges()) for offsetPtrAlignment in offsetPtrAlignments: hdrlenDict[offsetPtrAlignment], \ _ = approach2.getHeader2( dag, pp, virtualDetourPaths, lli, False, s, d, roundUpToMultipleBits=offsetPtrAlignment) pass pass if len(hdrLens2Smaller) == 0: assert len(hdrLens2Normal) == len(dagsizeSmaller) == len(dagsizeNormal) == 0 pass else: srcDstPairResults3[(s, d)] = SrcDstPairResult3( hdrLens2Normal, dagsizeNormal[0], hdrLens2Smaller, dagsizeSmaller[0]) pass pass # end while loop enddateSecs = int(time.time()) fileResult3 = FileResult3(filename, startdateSecs, enddateSecs, srcDstPairResults3) return fileResult3
def getDgWithStretch(g, s, d, weighted, stretch): # "weighted": whether the GRAPH is intended to be weighted return approach2.getDg(g, s, d, weighted=weighted)
def evalOneFile(filename, numberOfPairsToTry, pairIsOrdered=False, weighted=False): startdateSecs = int(time.time()) print ''' _______________________________________________ filename: [%s] start date: [%s] ''' % (filename, time.ctime(startdateSecs)) g, lli = utils.textToG(filename, useInt=False) allNodes = g.nodes() numNodes = len(allNodes) i = 0 srcDstPairResults3 = {} if numberOfPairsToTry > 0: while i < numberOfPairsToTry: # this is crypto random integer idx1 = (struct.unpack('I', os.urandom(4))[0]) % numNodes idx2 = (struct.unpack('I', os.urandom(4))[0]) % numNodes while idx2 == idx1: idx2 = (struct.unpack('I', os.urandom(4))[0]) % numNodes pass s,d = allNodes[idx1], allNodes[idx2] if (s,d) in srcDstPairResults3: # definitely skip print 'pair (%s,%s) already encountered -> skip' % (s,d) continue elif (d,s) in srcDstPairResults3: # not seen (s,d) yet but seen (d,s), should skip or not? if not pairIsOrdered: print 'pairs are not ordered, and (%s,%s) already encountered -> skip' % (d,s) continue pass # do this so we know we have seen this (s, d) pair srcDstPairResults3[(s, d)] = None # init to None (which will # mean disconnected) print 's,d="%s","%s"' % (s,d) i += 1 pass # end while i < numberOfPairsToTry pass # end if numberOfPairsToTry > 0 else: # numberOfPairsToTry is <= 0, so we do all (un-ordered) # pairs. the graph'd better be not too big. for i in range(numNodes - 1): for j in range(i + 1, numNodes): s = allNodes[i] d = allNodes[j] print 's,d="%s","%s"' % (s,d) srcDstPairResults3[(s, d)] = None # init to None (which will # mean disconnected) pass pass pass ########################### # now that we have the pairs we want to eval, eval them for s, d in srcDstPairResults3.keys(): #### use approach2 pp, dp = approach2.getDg(g, s, d, weighted) if (not pp) or (not dp): print 'no path: s,d="%s","%s"' % (s,d) continue # how many nodes in the undirected graph? subnodes = set(pp) for path in dp.values(): subnodes.update(path) pass dag = approach2.getDagWithVnodes( pp, dp, returnDetourPathsWithVNodes=False) diff = dag.number_of_nodes() - len(subnodes) percent = (float(diff) / len(subnodes)) * 100 srcDstPairResults3[(s, d)] = SrcDstPairResult3( diff, percent) pass # end while loop enddateSecs = int(time.time()) fileResult3 = FileResult3(filename, startdateSecs, enddateSecs, srcDstPairResults3) return fileResult3