def generate(self, workingDir, oFile, strBuffer): init, end = SimTimeRange.getTimeRange(self.__timeRange, self.__finishTime, self.__discardTime) strBuffer.writeln('') strBuffer.writeln('************* %s ****************' % self.__behaviorName) strBuffer.writeln('* Frequency: %.2f searches/s' % self.__searchFreq) strBuffer.writeln('* Generated: %d sets of searches' % self.getSearches()) strBuffer.writeln('* Time range: [%s, %s] s' % (init, end)) strBuffer.writeln("* Simultaneous searches: %.2f" % (self.getSimultaneous())) strBuffer.writeln("* Total searches: %d" % int(self.getSimultaneous() * self.getSearches())) strBuffer.writeln("* Search duration: %d s" % self.__searchDuration) strBuffer.writeln("* Invalid searches ratio: %.2f" % self.__invalidSearches) strBuffer.writeln("* Generating %d invalid searches" % int(self.getInvalidSearches())) self.printInfo(strBuffer) startTime = time() if self.__searchPeriod > 0.0: #Create search events each specified period of time during specified time range currentTime = init + DELTA_TIME while (currentTime < end + DELTA_TIME): self.perform(currentTime, oFile) #Increment time currentTime += self.__searchPeriod if self.__searchDuration > 0: self.finishSearches(self.__searchDuration, oFile) strBuffer.writeln('* Behavior generation time: %s ' % TimeFormatter.formatTime(time() - startTime)) strBuffer.writeln('**********************************************************')
def generate(self, workingDir, file, repeat, strBuffer): command = 'tools/setdest' parameters = '-v 2 -n %s -s 1 -m %s -M %s -t %.1f -P 2 -p %s -x %s -y %s' % (self.__nNodes, self.__minSpeed, self.__maxSpeed, self.__finishTime, self.__pauseTime, self.__maxX, self.__maxY) cmd = command + ' ' + parameters strBuffer.writeln('') strBuffer.writeln('************* Random waypoint ****************') strBuffer.writeln('* Nodes: %s' % self.__nNodes) strBuffer.writeln('* Minimum speed: %s m/s' % self.__minSpeed) strBuffer.writeln('* Maximum speed: %s m/s' % self.__maxSpeed) strBuffer.writeln('* Simulation time: %s s' % self.__finishTime) strBuffer.writeln('* Pause time: %s s' % self.__pauseTime) strBuffer.writeln('* Max X: %s m' % self.__maxX) strBuffer.writeln('* Max Y: %s m' % self.__maxY) strBuffer.writeln('* Generated using external command: %s' % cmd) paramList = [command] for e in parameters.split('-'): paramList.append('-' + e) startTime = time() oFilePath = os.path.join(workingDir, file) outputFile = open(oFilePath, 'w') p = subprocess.Popen(paramList, stdout=outputFile) result = p.wait() outputFile.close() if result != 0: strBuffer.writeln('ERROR: There was a problem during mobility model generation') print strBuffer.getvalue() sys.exit() strBuffer.writeln('* Model generation time: %s' % TimeFormatter.formatTime(time() - startTime)) strBuffer.writeln('**********************************************') return file
def __execute(self): strBuffer = StrBuffer.StrBuffer() strBuffer.writeln('* Running repeat %d of %d' % (self.__repeat + 1, self.__repeatNumber)) #Create temporal directory and prepare configuration files if os.path.isdir(self.__tempDir): shutil.rmtree(self.__tempDir) os.mkdir(self.__tempDir) self.__prepareSimulationFiles(self.__tempDir, self.__config, self.__configDir) #Create script using ScriptGenerator if self.__configGenerator.getDiscardTime() >= self.__configGenerator.getSimulationTime(): strBuffer.writeln('* ERROR: Discard time %.2f should be smaller than simulation time %.2f ' % (self.__configGenerator.getDiscardTime(), self.__configGenerator.getSimulationTime())) print strBuffer.getvalue() sys.exit() scriptGenerator = ScriptGenerator(self.__config, strBuffer) scriptGenerator.generate('Script.tcl', self.__tempDir, self.__repeat, strBuffer) #Run script simStartTime = time.time() out = open(os.path.join(self.__tempDir, 'out'), 'w') err = open(os.path.join(self.__tempDir, 'err'), 'w') strBuffer.writeln('* Simulation script on directory %s try %d' % (self.__tempDir, self.__try)) self.__p = subprocess.Popen(['ns', 'Script.tcl'], stdout=out, stderr=err, cwd=self.__tempDir) self.__timer = JVMCheckingTimer(JVM_DUMP_CHECK_TIME, self.__p, self.__tempDir) self.__timer.start() print strBuffer.getvalue() sys.stdout.flush() success = self.__p.wait() == 0 self.__timer.cancel() out.close() err.close() strBuffer = StrBuffer.StrBuffer() if not success: strBuffer.writeln('* ERROR: There was a problem during simulation execution on directory %s' % self.__tempDir) else: strBuffer.writeln('* Finished NS-2 simulation on directory %s. Running time: %s' % (self.__tempDir, TimeFormatter.formatTime(time.time() - simStartTime))) self.__compressOutputLog(strBuffer) print strBuffer.getvalue() sys.stdout.flush() return success
def __run(self): resolvedFileBuffer = self.__resolveImports(self.__inputFile) configGenerator = ConfigGenerator(resolvedFileBuffer) configurationCounter = 0 self.__saveTimestamp(self.__workingDir) while configGenerator.hasNext(): configurationDir = os.path.join(self.__workingDir, 'configuration-' + str(configurationCounter)) os.mkdir(configurationDir) startTime = time.time() repeatNumber = configGenerator.getRepeat() print '' print "* Running experiment configuration %d with %d repetition(s)" % (configurationCounter + 1, repeatNumber) print '' sys.stdout.flush() #Get current execution configuration config, generatedExpConfig = configGenerator.next() measures = Measures(config, resolvedFileBuffer) measures.startConfiguration(configurationCounter, configGenerator.getTag(), configGenerator.getType(), configGenerator.getSimulationTime(), configGenerator.getDiscardTime()) #save current configuration generatedConfigFile = open(os.path.join(configurationDir, 'GeneratedConfig.xml') , 'w') generatedConfigFile.write(generatedExpConfig) generatedConfigFile.close() data = [] for counter in range(repeatNumber): repeatDir = os.path.join(configurationDir, 'repeat-' + str(counter)) data.append((repeatDir, config, self.__configDir, resolvedFileBuffer, configGenerator, counter, repeatNumber)) pool = multiprocessing.Pool(processes=self.__processes) results = pool.map(runRepeat, data) success = all([success for success, outputLog, tempDir in results]) if not success: print '* Errors during execution of configuration %d' % configurationCounter else: output = [(outputLog, tempDir) for success, outputLog, tempDir in results] self.__processRepeats(measures, output) result = measures.endConfiguration() if success: outputFilePath = os.path.join(self.__outputDir, self.__inputFileName + '-resultConfig' + str(configurationCounter) + '.xml') print '* Writing configuration result XML to file %s ' % outputFilePath outputFile = open(outputFilePath, 'w') outputFile.write(result) outputFile.close() measures.savePartialResults(os.path.join(configurationDir, 'partialResults.txt')) print '* Finished configuration %s. Running time: %s' % (configurationCounter, TimeFormatter.formatTime(time.time() - startTime)) print '' sys.stdout.flush() configurationCounter = configurationCounter + 1 print '' sys.stdout.flush() #Removing running directory if not must be maintained if not self.__debug: shutil.rmtree(self.__workingDir)
def __compressOutputLog(self, strBuffer): startTime = time.time() strBuffer.writeln('* Compressing output log file %s' % self.__getPlainLog()) p = subprocess.Popen(['gzip', self.__getPlainLog()]) success = p.wait() == 0 if not success: strBuffer.writeln('ERROR: There was a problem compressing file %s' % self.__getPlainLog()) else: strBuffer.writeln('* Compressed log obtained in %s. Running time: %s' % (self.getOutputLog(), TimeFormatter.formatTime(time.time() - startTime))) return success
def process(data): measures, tempDir, outputLog, simulationTime, discardTime = data currentMeasures = [] for clazz, period, units, discardable in measures: measure = createMeasure(clazz, period, simulationTime) currentMeasures.append(measure) measure.start(tempDir) repeatFinished = False sys.stdout.flush() logProcessStartTime = timeUtil.time() outputFile = gzip.open(outputLog, 'r') line = outputFile.readline() while (line != '' and not repeatFinished): line.replace( '\n', '' ) repeatFinished = checkFinished(line) time = getLogLineTime(line) for measure in currentMeasures: if time >= discardTime or not measure.isDiscardable(): measure.parseLine(line) line = outputFile.readline() outputFile.close() print '* Parsed output log %s (%s). Simulation finished: %s. Running time: %s.' % (outputLog, sizeof_fmt(os.path.getsize(outputLog)), repeatFinished, TimeFormatter.formatTime(timeUtil.time() - logProcessStartTime)) sys.stdout.flush() results = [] for measure in currentMeasures: measure.finish() results.append((measure.getType(), (measure.getValues(), measure.getTotalValue()))) return repeatFinished, results