<reponame>dendisuhubdy/Xpedite<filename>test/pytest/test_xpedite/test_profiler/profile.py<gh_stars>1-10
"""
This module contains various functions used in testing Xpedite profiling and comparing
results.

Author:  <NAME>, <NAME>
"""

import os
import copy
from test_xpedite.test_profiler.app         import TargetLauncher
from test_xpedite                           import (
                                              TXN_COUNT, XPEDITE_APP_INFO_PATH,
                                              XPEDITE_APP_INFO_PARAMETER_PATH,
                                              PARAMETERS_DATA_DIR, DIR_PATH,
                                              SRC_DIR_PATH, PROFILER_PATH, LOCALHOST,
                                            )
from test_xpedite.test_profiler.comparator  import findDiff
from xpedite.profiler.probeAdmin            import ProbeAdmin


REPORT_NAME = 'XpediteTest'
SAMPLE_FILE_PATH = '{{dataDir}}/{parameterDir}/xpedite-*-{{runId}}-[0-9]*.data'.format(
  parameterDir=PARAMETERS_DATA_DIR
)

def generateProfiles(app, scenario):
  """
  Start Xpedite profiling and return profiles generated by profiling
  """
  from xpedite.profiler import Profiler
  return Profiler.profile(
    app, scenario.profileInfo, REPORT_NAME, None, app.dryRun, interactive=False, heartbeatInterval=1
  )

def runXpediteReport(runId, context, scenario, sampleFilePath=None, cpuInfoOverride=False):
  """
  Run xpedite report
  """
  with scenario.makeXpediteDormantApp(runId, context.workspace, sampleFilePath) as xpediteApp:
    if cpuInfoOverride:
      xpediteApp.env.proxy.fullCpuInfo = scenario.fullCpuInfo
    xpediteApp.appInfoPath = os.path.join(scenario.dataDir, XPEDITE_APP_INFO_PARAMETER_PATH)
    return generateProfiles(xpediteApp, scenario)

def runXpediteRecord(context, scenario):
  """
  Run xpedite record against a live target application process
  """
  with TargetLauncher(context, scenario) as app:
    report = generateProfiles(app.xpediteApp, scenario)
    fullCpuInfo = copy.deepcopy(app.xpediteApp.env.proxy.fullCpuInfo)
    dataFiles = app.xpediteApp.gatherFiles(app.xpediteApp.sampleFilePattern())
  return report, fullCpuInfo, dataFiles

def loadProbes(context, scenario):
  """
  Generate a probe map to test the state of application probes
  """
  with TargetLauncher(context, scenario) as app:
    return ProbeAdmin.loadProbes(app.xpediteApp)

def compareVsBaseline(context, scenario):
  """
  Compare profiles with benchmarks and profiles without benchmarks against existing profiles
  """
  runId = scenario.discoverRunId()
  sampleFilePath = SAMPLE_FILE_PATH.format(dataDir=scenario.dataDir, runId=runId)
  report = runXpediteReport(
    runId, context, scenario, sampleFilePath=sampleFilePath, cpuInfoOverride=True
  )
  reportProfiles = report.profiles
  reportProfiles.transactionRepo = None
  reportProfiles.cpuInfo.cpuId = scenario.cpuId
  if scenario.benchmarkPaths:
    benchmarkCount = len(scenario.benchmarkPaths)
    validateBenchmarks(reportProfiles, benchmarkCount)
  findDiff(reportProfiles.__dict__, scenario.baselineProfiles.__dict__)
  assert reportProfiles == scenario.baselineProfiles

def validateBenchmarks(profiles, benchmarkCount):
  """
  Validate the number of benchmark and number of timelines per benchmark
  """
  for profile in profiles:
    assert len(profile.benchmarks) == benchmarkCount
    for benchmark in profile.benchmarks.keys():
      assert len(profile.benchmarks[benchmark].timelineCollection) == TXN_COUNT

def generateProfileInfoFile(xpediteApp, probes):
  """
  Generate profile information for a specific app to be compared to an expected result
  """
  from xpedite.profiler.profileInfoGenerator import ProfileInfoGenerator
  profiler = os.path.join(DIR_PATH, SRC_DIR_PATH, PROFILER_PATH)
  generator = ProfileInfoGenerator(
    xpediteApp.executableName, LOCALHOST, XPEDITE_APP_INFO_PATH,
    probes, profiler
  )
  generator.generate()
  return generator.filePath

def buildNotebook(context, scenario):
  """
  Test to confirm a Jupyter notebook can be creating from profile information and results
  generated by xpedite record
  """
  import xpedite.jupyter.driver
  from xpedite.jupyter            import DATA_DIR, DATA_FILE_EXT, NOTEBOOK_EXT
  from xpedite.benchmark          import makeBenchmark
  from xpedite.jupyter.result     import Result
  from test_xpedite               import mkdtemp
  report, fullCpuInfo, dataFiles = runXpediteRecord(context, scenario)
  notebookDir = mkdtemp()
  tempDataDir = os.path.join(notebookDir, DATA_DIR)
  os.mkdir(tempDataDir)
  notebookPath = os.path.join(notebookDir, '{}{}'.format(REPORT_NAME, NOTEBOOK_EXT))
  dataFilePath = os.path.join(notebookDir, DATA_DIR, '{}{}'.format(REPORT_NAME, DATA_FILE_EXT))
  result = Result(report)
  notebook = xpedite.jupyter.driver.buildNotebook(
    scenario.appName, result, notebookPath, dataFilePath, report.runId
  )
  return notebook, dataFilePath, report, fullCpuInfo, dataFiles

"""
This module contains various functions used in testing Xpedite profiling and comparing
results.

Author:  <NAME>, <NAME>
"""

import os
import copy
from test_xpedite.test_profiler.app         import TargetLauncher
from test_xpedite                           import (
                                              TXN_COUNT, XPEDITE_APP_INFO_PATH,
                                              XPEDITE_APP_INFO_PARAMETER_PATH,
                                              PARAMETERS_DATA_DIR, DIR_PATH,
                                              SRC_DIR_PATH, PROFILER_PATH, LOCALHOST,
                                            )
from test_xpedite.test_profiler.comparator  import findDiff
from xpedite.profiler.probeAdmin            import ProbeAdmin


REPORT_NAME = 'XpediteTest'
SAMPLE_FILE_PATH = '{{dataDir}}/{parameterDir}/xpedite-*-{{runId}}-[0-9]*.data'.format(
  parameterDir=PARAMETERS_DATA_DIR
)

def generateProfiles(app, scenario):
  """
  Start Xpedite profiling and return profiles generated by profiling
  """
  from xpedite.profiler import Profiler
  return Profiler.profile(
    app, scenario.profileInfo, REPORT_NAME, None, app.dryRun, interactive=False, heartbeatInterval=1
  )

def runXpediteReport(runId, context, scenario, sampleFilePath=None, cpuInfoOverride=False):
  """
  Run xpedite report
  """
  with scenario.makeXpediteDormantApp(runId, context.workspace, sampleFilePath) as xpediteApp:
    if cpuInfoOverride:
      xpediteApp.env.proxy.fullCpuInfo = scenario.fullCpuInfo
    xpediteApp.appInfoPath = os.path.join(scenario.dataDir, XPEDITE_APP_INFO_PARAMETER_PATH)
    return generateProfiles(xpediteApp, scenario)

def runXpediteRecord(context, scenario):
  """
  Run xpedite record against a live target application process
  """
  with TargetLauncher(context, scenario) as app:
    report = generateProfiles(app.xpediteApp, scenario)
    fullCpuInfo = copy.deepcopy(app.xpediteApp.env.proxy.fullCpuInfo)
    dataFiles = app.xpediteApp.gatherFiles(app.xpediteApp.sampleFilePattern())
  return report, fullCpuInfo, dataFiles

def loadProbes(context, scenario):
  """
  Generate a probe map to test the state of application probes
  """
  with TargetLauncher(context, scenario) as app:
    return ProbeAdmin.loadProbes(app.xpediteApp)

def compareVsBaseline(context, scenario):
  """
  Compare profiles with benchmarks and profiles without benchmarks against existing profiles
  """
  runId = scenario.discoverRunId()
  sampleFilePath = SAMPLE_FILE_PATH.format(dataDir=scenario.dataDir, runId=runId)
  report = runXpediteReport(
    runId, context, scenario, sampleFilePath=sampleFilePath, cpuInfoOverride=True
  )
  reportProfiles = report.profiles
  reportProfiles.transactionRepo = None
  reportProfiles.cpuInfo.cpuId = scenario.cpuId
  if scenario.benchmarkPaths:
    benchmarkCount = len(scenario.benchmarkPaths)
    validateBenchmarks(reportProfiles, benchmarkCount)
  findDiff(reportProfiles.__dict__, scenario.baselineProfiles.__dict__)
  assert reportProfiles == scenario.baselineProfiles

def validateBenchmarks(profiles, benchmarkCount):
  """
  Validate the number of benchmark and number of timelines per benchmark
  """
  for profile in profiles:
    assert len(profile.benchmarks) == benchmarkCount
    for benchmark in profile.benchmarks.keys():
      assert len(profile.benchmarks[benchmark].timelineCollection) == TXN_COUNT

def generateProfileInfoFile(xpediteApp, probes):
  """
  Generate profile information for a specific app to be compared to an expected result
  """
  from xpedite.profiler.profileInfoGenerator import ProfileInfoGenerator
  profiler = os.path.join(DIR_PATH, SRC_DIR_PATH, PROFILER_PATH)
  generator = ProfileInfoGenerator(
    xpediteApp.executableName, LOCALHOST, XPEDITE_APP_INFO_PATH,
    probes, profiler
  )
  generator.generate()
  return generator.filePath

def buildNotebook(context, scenario):
  """
  Test to confirm a Jupyter notebook can be creating from profile information and results
  generated by xpedite record
  """
  import xpedite.jupyter.driver
  from xpedite.jupyter            import DATA_DIR, DATA_FILE_EXT, NOTEBOOK_EXT
  from xpedite.benchmark          import makeBenchmark
  from xpedite.jupyter.result     import Result
  from test_xpedite               import mkdtemp
  report, fullCpuInfo, dataFiles = runXpediteRecord(context, scenario)
  notebookDir = mkdtemp()
  tempDataDir = os.path.join(notebookDir, DATA_DIR)
  os.mkdir(tempDataDir)
  notebookPath = os.path.join(notebookDir, '{}{}'.format(REPORT_NAME, NOTEBOOK_EXT))
  dataFilePath = os.path.join(notebookDir, DATA_DIR, '{}{}'.format(REPORT_NAME, DATA_FILE_EXT))
  result = Result(report)
  notebook = xpedite.jupyter.driver.buildNotebook(
    scenario.appName, result, notebookPath, dataFilePath, report.runId
  )
  return notebook, dataFilePath, report, fullCpuInfo, dataFiles
This module contains various functions used in testing Xpedite profiling and comparing results.  Author:  <NAME>, <NAME> Start Xpedite profiling and return profiles generated by profiling Run xpedite report Run xpedite record against a live target application process Generate a probe map to test the state of application probes Compare profiles with benchmarks and profiles without benchmarks against existing profiles Validate the number of benchmark and number of timelines per benchmark Generate profile information for a specific app to be compared to an expected result Test to confirm a Jupyter notebook can be creating from profile information and results   generated by xpedite record