1 # Copyright 2015 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
10 from telemetry
.internal
.backends
.chrome_inspector
import inspector_websocket
11 from telemetry
.internal
.backends
.chrome_inspector
import websocket
14 class MemoryTimeoutException(Exception):
18 class MemoryUnrecoverableException(Exception):
22 class MemoryUnexpectedResponseException(Exception):
26 class MemoryBackend(object):
28 def __init__(self
, inspector_socket
):
29 self
._inspector
_websocket
= inspector_socket
31 def SetMemoryPressureNotificationsSuppressed(self
, suppressed
, timeout
=30):
32 """Enable/disable suppressing memory pressure notifications.
35 suppressed: If true, memory pressure notifications will be suppressed.
36 timeout: The timeout in seconds.
39 MemoryTimeoutException: If more than |timeout| seconds has passed
40 since the last time any data is received.
41 MemoryUnrecoverableException: If there is a websocket error.
42 MemoryUnexpectedResponseException: If the response contains an error
43 or does not contain the expected result.
45 self
._SendMemoryRequest
('setPressureNotificationsSuppressed',
46 {'suppressed': suppressed
}, timeout
)
48 def SimulateMemoryPressureNotification(self
, pressure_level
, timeout
=30):
49 """Simulate a memory pressure notification.
52 pressure level: The memory pressure level of the notification ('moderate'
54 timeout: The timeout in seconds.
57 MemoryTimeoutException: If more than |timeout| seconds has passed
58 since the last time any data is received.
59 MemoryUnrecoverableException: If there is a websocket error.
60 MemoryUnexpectedResponseException: If the response contains an error
61 or does not contain the expected result.
63 self
._SendMemoryRequest
('simulatePressureNotification',
64 {'level': pressure_level
}, timeout
)
66 def _SendMemoryRequest(self
, command
, params
, timeout
):
67 method
= 'Memory.%s' % command
73 response
= self
._inspector
_websocket
.SyncRequest(request
, timeout
)
74 except websocket
.WebSocketTimeoutException
:
75 raise MemoryTimeoutException(
76 'Exception raised while sending a %s request:\n%s' %
77 (method
, traceback
.format_exc()))
78 except (socket
.error
, websocket
.WebSocketException
,
79 inspector_websocket
.WebSocketDisconnected
):
80 raise MemoryUnrecoverableException(
81 'Exception raised while sending a %s request:\n%s' %
82 (method
, traceback
.format_exc()))
84 if 'error' in response
:
85 code
= response
['error']['code']
86 if code
== inspector_websocket
.InspectorWebsocket
.METHOD_NOT_FOUND_CODE
:
88 '%s DevTools method not supported by the browser' % method
)
90 raise MemoryUnexpectedResponseException(
91 'Inspector returned unexpected response for %s:\n%s' %
92 (method
, json
.dumps(response
, indent
=2)))
95 self
._inspector
_websocket
= None