aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/tensorboard/plugins/trace/trace_test.py
blob: d44f46b5bb2d7c679afa8948acdb0d13e0ec4a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for tensorflow.contrib.tensorboard.plugins.trace package."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tempfile

from google.protobuf import json_format

from tensorflow.contrib.tensorboard.plugins import trace
from tensorflow.python.framework import constant_op
from tensorflow.python.platform import gfile
from tensorflow.python.platform import test


class TraceTest(test.TestCase):

  def setUp(self):
    self._temp_dir = tempfile.mkdtemp()
    self._temp_trace_json = self._temp_dir + 'trace.json'

  def tearDown(self):
    gfile.DeleteRecursively(self._temp_dir)

  def testEmptyGraph(self):
    trace_info = self._store_and_read_trace_info()
    self.assertEqual(len(trace_info.ops), 0)

  def testHasSourceCodeOfThisFile(self):
    constant_op.constant(0)
    trace_info = self._store_and_read_trace_info()

    self.assertTrue(trace_info.files)
    for file_info in trace_info.files:
      if file_info.file_path.endswith('trace_test.py'):
        return
    self.fail('trace_test file not found in the trace info json')

  def testHasTheConstantOp(self):
    constant_op.constant(0)
    trace_info = self._store_and_read_trace_info()

    self.assertTrue(trace_info.ops)

    for op in trace_info.ops:
      if op.op_type == 'Const':
        return
    self.fail('Could not find operation of type `Const` in the graph')

  def testMultilineStatements(self):
    source = """def test():
      a(4,
        3,
        1)

      b(3, 4, 5)

      c((4, 3),
        (),
      )
    """
    line2start = trace.find_multiline_statements(source)

    self.assertEqual(line2start[3], 1)
    self.assertEqual(line2start[9], 7)
    self.assertEqual(len(line2start), 2)

  def _store_and_read_trace_info(self):
    trace.store_trace_info(self._temp_trace_json)
    trace_info = trace.TraceInfo()

    with gfile.Open(self._temp_trace_json) as f:
      text = f.read().decode('utf-8')
    json_format.Parse(text, trace_info)

    return trace_info


if __name__ == '__main__':
  test.main()