1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved. 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 #define LOG_TAG "TraceFileReaderTest" 17 #include <fstream> 18 #include <hwext/gtest-ext.h> 19 #include <hwext/gtest-tag.h> 20 #include <memory> 21 #include <unistd.h> 22 #include <vector> 23 24 #include "common_types.pb.h" 25 #include "logging.h" 26 #include "trace_file_reader.h" 27 #include "trace_file_writer.h" 28 29 using namespace testing::ext; 30 31 namespace { 32 #if defined(__i386__) || defined(__x86_64__) 33 const std::string DEFAULT_TEST_PATH("./"); 34 #else 35 const std::string DEFAULT_TEST_PATH("/data/local/tmp/"); 36 #endif 37 38 class TraceFileReaderTest : public ::testing::Test { 39 protected: 40 std::string path = "trace.bin"; 41 SetUpTestCase()42 static void SetUpTestCase() {} TearDownTestCase()43 static void TearDownTestCase() 44 { 45 std::string name = "325.ht"; 46 std::string path = DEFAULT_TEST_PATH + name; 47 48 if (access(path.c_str(), F_OK) == 0) { 49 system(std::string("rm " + path).c_str()); 50 } 51 } 52 SetUp()53 void SetUp() override {} 54 TearDown()55 void TearDown() override {} 56 }; 57 58 /** 59 * @tc.name: server 60 * @tc.desc: write read. 61 * @tc.type: FUNC 62 */ 63 HWTEST_F(TraceFileReaderTest, WriteRead, TestSize.Level1) 64 { 65 std::string path = "trace-write-msg.bin"; 66 auto writer = std::make_shared<TraceFileWriter>(path); 67 ASSERT_NE(writer, nullptr); 68 69 constexpr int n = 100; 70 for (int i = 1; i <= n; i++) { 71 ProfilerPluginData pluginData{}; 72 pluginData.set_name("test_name"); 73 pluginData.set_status(i); 74 pluginData.set_data("Hello, World!"); 75 long bytes = writer->Write(pluginData); 76 EXPECT_EQ(bytes, sizeof(uint32_t) + pluginData.ByteSizeLong()); 77 PROFILER_LOG_INFO(LOG_CORE, "[%d/%d] write %ld bytes to %s.", i, n, bytes, path.c_str()); 78 } 79 writer.reset(); // make sure write done! 80 81 auto reader = std::make_shared<TraceFileReader>(); 82 ASSERT_NE(reader, nullptr); 83 ASSERT_TRUE(reader->Open(path)); 84 85 for (int i = 1; i <= n; i++) { 86 ProfilerPluginData data{}; 87 long bytes = reader->Read(data); 88 PROFILER_LOG_INFO(LOG_CORE, "data = {%s, %d, %s}", data.name().c_str(), data.status(), data.data().c_str()); 89 PROFILER_LOG_INFO(LOG_CORE, "read %ld bytes from %s", bytes, path.c_str()); 90 } 91 } 92 93 /** 94 * @tc.name: server 95 * @tc.desc: write read break. 96 * @tc.type: FUNC 97 */ 98 HWTEST_F(TraceFileReaderTest, WriteReadBreak, TestSize.Level1) 99 { 100 std::string path = "trace-write-msg.bin"; 101 auto writer = std::make_shared<TraceFileWriter>(path); 102 ASSERT_NE(writer, nullptr); 103 104 constexpr int n = 100; 105 for (int i = 1; i <= n; i++) { 106 ProfilerPluginData pluginData{}; 107 pluginData.set_name("test_name"); 108 pluginData.set_status(i); 109 pluginData.set_data("Hello, World!"); 110 long bytes = writer->Write(pluginData); 111 EXPECT_EQ(bytes, sizeof(uint32_t) + pluginData.ByteSizeLong()); 112 PROFILER_LOG_INFO(LOG_CORE, "[%d/%d] write %ld bytes to %s.", i, n, bytes, path.c_str()); 113 } 114 writer.reset(); // make sure write done! 115 116 auto reader = std::make_shared<TraceFileReader>(); 117 ASSERT_NE(reader, nullptr); 118 ASSERT_TRUE(reader->Open(path)); 119 120 long bytes = 0; 121 do { 122 ProfilerPluginData data{}; 123 bytes = reader->Read(data); 124 PROFILER_LOG_INFO(LOG_CORE, "data = {%s, %d, %s}", data.name().c_str(), data.status(), data.data().c_str()); 125 PROFILER_LOG_INFO(LOG_CORE, "read %ld bytes from %s", bytes, path.c_str()); 126 } while (bytes > 0); 127 } 128 129 /** 130 * @tc.name: server 131 * @tc.desc: read. 132 * @tc.type: FUNC 133 */ 134 HWTEST_F(TraceFileReaderTest, Read, TestSize.Level1) 135 { 136 std::string name = "325.ht"; 137 std::string path = DEFAULT_TEST_PATH + name; 138 139 if (access(path.c_str(), F_OK) != 0) { 140 std::unique_ptr<FILE, decltype(fclose)*> fptr(fopen(path.c_str(), "wb+"), fclose); 141 TraceFileHeader header = {}; // default header data 142 fwrite(&header, sizeof(char), sizeof(header), fptr.get()); 143 } 144 auto reader = std::make_shared<TraceFileReader>(); 145 ASSERT_NE(reader, nullptr); 146 ASSERT_TRUE(reader->Open(path)); 147 148 long bytes = 0; 149 do { 150 ProfilerPluginData data{}; 151 bytes = reader->Read(data); 152 fprintf(stderr, "data = [%s, %d, ...%zu], len = %zu, bytes = %ld\n", data.name().c_str(), data.status(), 153 data.data().size(), data.ByteSizeLong(), bytes); 154 } while (bytes > 0); 155 if (access(path.c_str(), F_OK) == 0) { 156 system(std::string("rm " + path).c_str()); 157 } 158 } 159 } // namespace 160