Fix underlay testing issue on video power measurement

It fails on certain video sites, for example:
http://crosvideo.appspot.com

Also, this CL adds an option --bypass-ipg so that we can
debug testing harness where Intel Power Gadget is not
supported.

BUG=868561
TEST=manual, GPU bots
R=magchen@chromium.org

Change-Id: I0e8a2882dab4a1fcf6d5989e65dc36d57cad165a
Reviewed-on: https://chromium-review.googlesource.com/c/1471295
Commit-Queue: Zhenyao Mo <zmo@chromium.org>
Reviewed-by: Sunny Sachanandani <sunnyps@chromium.org>
Reviewed-by: Maggie Chen <magchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#632466}
diff --git a/content/test/gpu/gpu_tests/ipg_utils.py b/content/test/gpu/gpu_tests/ipg_utils.py
index 35c8919..8402ec0 100644
--- a/content/test/gpu/gpu_tests/ipg_utils.py
+++ b/content/test/gpu/gpu_tests/ipg_utils.py
@@ -19,23 +19,20 @@
    5 seconds, call AnalyzeIPGLogFile(skip_in_sec=5).
 """
 
+import datetime
 import json
 import logging
 import os
 import subprocess
-import datetime
 
 def LocateIPG():
   ipg_dir = os.getenv('IPG_Dir')
   if not ipg_dir:
-    logging.warning("No env IPG_Dir")
-    return None
+    raise Exception("No env IPG_Dir")
   gadget_path = os.path.join(ipg_dir, "PowerLog3.0.exe")
-  logging.debug("Try to locale Intel Power Gadget at " + gadget_path)
-  if os.path.isfile(gadget_path):
-    logging.debug("Intel Power Gadget Found")
-    return gadget_path
-  return None
+  if not os.path.isfile(gadget_path):
+    raise Exception("Can't locate Intel Power Gadget at " + gadget_path)
+  return gadget_path
 
 def GenerateIPGLogFilename(log_prefix='PowerLog', log_dir=None, current_run=1,
                            total_runs=1, timestamp=False):
@@ -51,9 +48,6 @@
 
 def RunIPG(duration_in_s=60, resolution_in_ms=100, logfile=None):
   intel_power_gadget_path = LocateIPG()
-  if not intel_power_gadget_path:
-    logging.warning("Can't locate Intel Power Gadget")
-    return
   command = ('"%s" -duration %d -resolution %d' %
              (intel_power_gadget_path, duration_in_s, resolution_in_ms))
   if not logfile:
@@ -61,19 +55,15 @@
     logfile = GenerateIPGLogFilename()
   command = command + (' -file %s' %logfile)
   logging.debug("Running: " + command)
-  try:
-    output = subprocess.check_output(command)
-    logging.debug("Running: DONE")
-    logging.debug(output)
-  except subprocess.CalledProcessError as err:
-    logging.warning(err)
+  output = subprocess.check_output(command)
+  logging.debug("Running: DONE")
+  logging.debug(output)
 
 def AnalyzeIPGLogFile(logfile=None, skip_in_sec=0):
   if not logfile:
     logfile = GenerateIPGLogFilename()
   if not os.path.isfile(logfile):
-    logging.warning("Can't locate logfile at " + logfile)
-    return {}
+    raise Exception("Can't locate logfile at " + logfile)
   first_line = True
   samples = 0
   cols = 0
diff --git a/content/test/gpu/gpu_tests/power_measurement_integration_test.py b/content/test/gpu/gpu_tests/power_measurement_integration_test.py
index 8b52a23d..4a9b484b 100644
--- a/content/test/gpu/gpu_tests/power_measurement_integration_test.py
+++ b/content/test/gpu/gpu_tests/power_measurement_integration_test.py
@@ -27,8 +27,10 @@
 from gpu_tests import ipg_utils
 from gpu_tests.gpu_test_expectations import GpuTestExpectations
 
+import logging
 import os
 import sys
+import time
 
 # Waits for [x] seconds after browser launch before measuring power to
 # avoid startup tasks affecting results.
@@ -83,6 +85,8 @@
         var left = vid_rect.left - parent_rect.left;
         layer.style.top = top.toString() + "px";
         layer.style.left = left.toString() + "px";
+        // The following might mess with the layout of some sites.
+        video.parentNode.style.position = "relative";
         video.parentNode.appendChild(layer);
       }
       return video.currentTime > 0;
@@ -167,10 +171,13 @@
                       help="if a test is repeated multiples and outliers is "
                       "set to N, then N smallest results and N largest results "
                       "are discarded before computing mean and stdev.")
+    parser.add_option("--bypass-ipg", action="store_true", default=False,
+                      help="Do not launch Intel Power Gadget. This is for "
+                      "testing convenience on machines where Intel Power "
+                      "Gadget does not work.")
 
   @classmethod
   def GenerateGpuTests(cls, options):
-    yield ('Basic', '-', {'test_func': 'Basic'})
     if options.url is not None:
       # This is for local testing convenience only and is not to be added to
       # any bots.
@@ -183,7 +190,12 @@
               'logdir': options.logdir,
               'duration': options.duration,
               'delay': options.delay,
-              'resolution': options.resolution})
+              'resolution': options.resolution,
+              'bypass_ipg': options.bypass_ipg})
+    else:
+      yield ('Basic', '-',
+             {'test_func': 'Basic',
+              'bypass_ipg': options.bypass_ipg})
 
   @classmethod
   def SetUpProcess(cls):
@@ -194,9 +206,6 @@
     cls.StartBrowser()
 
   def RunActualGpuTest(self, test_path, *args):
-    ipg_path = ipg_utils.LocateIPG()
-    if not ipg_path:
-      self.fail("Fail to locate Intel Power Gadget")
     test_params = args[0]
     assert test_params is not None and 'test_func' in test_params
     prefixed_test_func_name = '_RunTest_%s' % test_params['test_func']
@@ -210,14 +219,18 @@
   # Actual test functions
 
   def _RunTest_Basic(self, test_path, params):
+    bypass_ipg = params['bypass_ipg']
+    total_time = _POWER_MEASUREMENT_DURATION + _POWER_MEASUREMENT_DELAY
+    if bypass_ipg:
+      logging.info("Bypassing Intel Power Gadget")
+      time.sleep(total_time)
+      return
     logfile = None # Use the default path
-    ipg_utils.RunIPG(_POWER_MEASUREMENT_DURATION + _POWER_MEASUREMENT_DELAY,
-                     _POWER_MEASUREMENT_RESOLUTION,
-                     logfile)
+    ipg_utils.RunIPG(total_time, _POWER_MEASUREMENT_RESOLUTION, logfile)
     results = ipg_utils.AnalyzeIPGLogFile(logfile, _POWER_MEASUREMENT_DELAY)
     # TODO(zmo): output in a way that the results can be tracked at
     # chromeperf.appspot.com.
-    print "Results: ", results
+    logging.info("Results: %s", str(results))
 
 
   def _RunTest_URL(self, test_path, params):
@@ -229,13 +242,14 @@
     ipg_duration = params['duration']
     ipg_delay = params['delay']
     ipg_resolution = params['resolution']
+    bypass_ipg = params['bypass_ipg']
 
-    print ""
-    print "Total iterations: ", repeat
+    if repeat > 1:
+      logging.info("Total iterations: %d", repeat)
     logfiles = []
     for iteration in range(repeat):
-      run_label = "Iteration_%d" % iteration
-      print run_label
+      if repeat > 1:
+        logging.info("Iteration %d", iteration)
       if test_path:
         self.tab.action_runner.Navigate(test_path, _FULLSCREEN_SCRIPT)
         self.tab.WaitForDocumentReadyStateToBeComplete()
@@ -253,22 +267,29 @@
         self.tab.action_runner.ClickElement(element_function=(
             'locateFullscreenButton()'))
 
-      logfile = None
-      if ipg_logdir:
-        if not os.path.isdir(ipg_logdir):
-          self.fail("Folder " + ipg_logdir + " doesn't exist")
-        logfile = ipg_utils.GenerateIPGLogFilename(log_dir=ipg_logdir,
-                                                   timestamp=True)
-      ipg_utils.RunIPG(ipg_duration + ipg_delay, ipg_resolution, logfile)
-      logfiles.append(logfile)
+      if bypass_ipg:
+        logging.info("Bypassing Intel Power Gadget")
+        time.sleep(ipg_duration + ipg_delay)
+      else:
+        logfile = None
+        if ipg_logdir:
+          if not os.path.isdir(ipg_logdir):
+            self.fail("Folder " + ipg_logdir + " doesn't exist")
+          logfile = ipg_utils.GenerateIPGLogFilename(log_dir=ipg_logdir,
+                                                     timestamp=True)
+        ipg_utils.RunIPG(ipg_duration + ipg_delay, ipg_resolution, logfile)
+        logfiles.append(logfile)
 
       if repeat > 1 and iteration < repeat - 1:
         self.StopBrowser()
         self.StartBrowser()
 
+    if bypass_ipg:
+      return
+
     if repeat == 1:
       results = ipg_utils.AnalyzeIPGLogFile(logfiles[0], ipg_delay)
-      print "Results: ", results
+      logging.info("Results: %s", str(results))
     else:
       json_path = None
       if ipg_logdir:
@@ -277,7 +298,7 @@
 
       summary = ipg_utils.ProcessResultsFromMultipleIPGRuns(
         logfiles, ipg_delay, outliers, json_path)
-      print 'Summary: ', summary
+      logging.info("Summary: %s", str(summary))
 
 
 def load_tests(loader, tests, pattern):