1 /* Copyright 2017 The TensorFlow Authors All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "tensorflow/core/profiler/rpc/client/trace_events_to_json.h" 17 #include "include/json/json.h" 18 #include "tensorflow/core/platform/protobuf.h" 19 #include "tensorflow/core/platform/test.h" 20 #include "tensorflow/core/profiler/trace_events.pb.h" 21 22 namespace tensorflow { 23 24 namespace profiler { 25 namespace client { 26 namespace { 27 28 string ConvertTextFormattedTraceToJson(const string& trace_str) { 29 Trace trace; 30 ::tensorflow::protobuf::TextFormat::ParseFromString(trace_str, &trace); 31 return TraceEventsToJson(trace); 32 } 33 34 Json::Value ToJsonValue(const string& json_str) { 35 Json::Value json; 36 Json::Reader reader; 37 EXPECT_TRUE(reader.parse(json_str, json)); 38 return json; 39 } 40 41 TEST(TraceEventsToJson, JsonConversion) { 42 string json_output = ConvertTextFormattedTraceToJson(R"( 43 devices { key: 2 value { 44 name: 'D2' 45 device_id: 2 46 resources { key: 2 value { 47 resource_id: 2 48 name: 'R2.2' 49 } } 50 } } 51 devices { key: 1 value { 52 name: 'D1' 53 device_id: 1 54 resources { key: 2 value { 55 resource_id: 1 56 name: 'R1.2' 57 } } 58 } } 59 60 trace_events { 61 device_id: 1 62 resource_id: 2 63 name: 'E1.2.1' 64 timestamp_ps: 100000 65 duration_ps: 10000 66 } 67 trace_events { 68 device_id: 2 69 resource_id: 2 70 name: 'E2.2.1 # "comment"' 71 timestamp_ps: 105000 72 } 73 )"); 74 string expected_json = R"( 75 { 76 "displayTimeUnit": "ns", 77 "metadata": { "highres-ticks": true }, 78 "traceEvents": [ 79 {"ph":"M", "pid":1, "name":"process_name", "args":{"name":"D1"}}, 80 {"ph":"M", "pid":1, "name":"process_sort_index", "args":{"sort_index":1}}, 81 {"ph":"M", "pid":1, "tid":2, "name":"thread_name", 82 "args":{"name":"R1.2"}}, 83 {"ph":"M", "pid":1, "tid":2, "name":"thread_sort_index", 84 "args":{"sort_index":2}}, 85 {"ph":"M", "pid":2, "name":"process_name", "args":{"name":"D2"}}, 86 {"ph":"M", "pid":2, "name":"process_sort_index", "args":{"sort_index":2}}, 87 {"ph":"M", "pid":2, "tid":2, "name":"thread_name", 88 "args":{"name":"R2.2"}}, 89 {"ph":"M", "pid":2, "tid":2, "name":"thread_sort_index", 90 "args":{"sort_index":2}}, 91 92 { 93 "ph" : "X", 94 "pid" : 1, 95 "tid" : 2, 96 "name" : "E1.2.1", 97 "ts" : 0.1, 98 "dur" : 0.01 99 }, 100 { 101 "ph" : "i", 102 "pid" : 2, 103 "tid" : 2, 104 "name" : "E2.2.1 # \"comment\"", 105 "ts" : 0.105, 106 "s" : "t" 107 }, 108 {} 109 ] 110 })"; 111 EXPECT_EQ(ToJsonValue(json_output), ToJsonValue(expected_json)); 112 } 113 114 } // namespace 115 } // namespace client 116 } // namespace profiler 117 } // namespace tensorflow 118