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