#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging

import devtools_test_base
import pyauto_functional  # Must be imported before pyauto
import pyauto
import pyauto_utils


class DevToolsNativeMemorySnapshotTest(devtools_test_base.DevToolsTestBase):
  """Test for tracking unknown share in the DevTools native memory snapshots.

  This test navigates the browser to a test page, then takes native memory
  snapshot over remote debugging protocol and prints render process private
  memory size and unknown size extracted from the snapshot. It is used to
  track size of the memory that is not counted by DevTools memory
  instrumentation.

  The test uses Web Page Replay server as a proxy that allows to replay
  the same state of the test pages and avoid heavy network traffic on the
  real web sites. See webpagereplay.ReplayServer documentation to learn how
  to record new page archives.
  """
  def testNytimes(self):
    self.RunTestWithUrl('http://www.nytimes.com/')

  def testCnn(self):
    self.RunTestWithUrl('http://www.cnn.com/')

  def testGoogle(self):
    self.RunTestWithUrl('http://www.google.com/')

  def PrintTestResult(self, hostname, snapshot):
    total = snapshot.GetProcessPrivateMemorySize()
    unknown = snapshot.GetUnknownSize()
    logging.info('Got data for: %s, total size = %d, unknown size = %d' %
        (hostname, total, unknown))
    graph_name = 'DevTools Native Snapshot - ' + hostname
    pyauto_utils.PrintPerfResult(graph_name, 'Total', total, 'bytes')
    pyauto_utils.PrintPerfResult(graph_name, 'Unknown', unknown, 'bytes')


if __name__ == '__main__':
  pyauto_functional.Main()

@@ -1516,16 +1516,17 @@
 known))%0A
+%0A
     grap
@@ -1539,47 +1539,36 @@
  = '
-DevTools Native Snapshot - ' + hostname
+Native Memory Unknown Bytes'
 %0A   
@@ -1613,10 +1613,23 @@
 me, 
-'T
+hostname + ': t
 otal
@@ -1637,16 +1637,24 @@
 , total,
+%0A       
  'bytes'
@@ -1704,10 +1704,23 @@
 me, 
-'U
+hostname + ': u
 nkno
@@ -1732,23 +1732,260 @@
 unknown,
- 'bytes
+%0A        'bytes')%0A%0A    if total == 0:%0A      logging.info('No total process memory size')%0A      return%0A    unknown_percent = float(unknown) / total%0A    pyauto_utils.PrintPerfResult('Native Memory Unknown %25', hostname,%0A        unknown_percent, '%25
 ')%0A%0A%0Aif 

add a graph with unknown memory percent