1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | #include <chrono> |
19 | #include <cstdint> |
20 | #include <iostream> |
21 | |
22 | #include <gtest/gtest.h> |
23 | |
24 | #include "arrow/util/logging.h" |
25 | |
26 | // This code is adapted from |
27 | // https://github.com/ray-project/ray/blob/master/src/ray/util/logging_test.cc. |
28 | |
29 | namespace arrow { |
30 | namespace util { |
31 | |
32 | int64_t current_time_ms() { |
33 | std::chrono::milliseconds ms_since_epoch = |
34 | std::chrono::duration_cast<std::chrono::milliseconds>( |
35 | std::chrono::steady_clock::now().time_since_epoch()); |
36 | return ms_since_epoch.count(); |
37 | } |
38 | |
39 | // This is not really test. |
40 | // This file just print some information using the logging macro. |
41 | |
42 | void PrintLog() { |
43 | ARROW_LOG(DEBUG) << "This is the" |
44 | << " DEBUG" |
45 | << " message" ; |
46 | ARROW_LOG(INFO) << "This is the" |
47 | << " INFO message" ; |
48 | ARROW_LOG(WARNING) << "This is the" |
49 | << " WARNING message" ; |
50 | ARROW_LOG(ERROR) << "This is the" |
51 | << " ERROR message" ; |
52 | ARROW_CHECK(true) << "This is a ARROW_CHECK" |
53 | << " message but it won't show up" ; |
54 | // The following 2 lines should not run since it will cause program failure. |
55 | // ARROW_LOG(FATAL) << "This is the FATAL message"; |
56 | // ARROW_CHECK(false) << "This is a ARROW_CHECK message but it won't show up"; |
57 | } |
58 | |
59 | TEST(PrintLogTest, LogTestWithoutInit) { |
60 | // Without ArrowLog::StartArrowLog, this should also work. |
61 | PrintLog(); |
62 | } |
63 | |
64 | TEST(PrintLogTest, LogTestWithInit) { |
65 | // Test empty app name. |
66 | ArrowLog::StartArrowLog("" , ArrowLogLevel::ARROW_DEBUG); |
67 | PrintLog(); |
68 | ArrowLog::ShutDownArrowLog(); |
69 | } |
70 | |
71 | // This test will output large amount of logs to stderr, should be disabled in travis. |
72 | TEST(LogPerfTest, PerfTest) { |
73 | ArrowLog::StartArrowLog("/fake/path/to/appdire/LogPerfTest" , ArrowLogLevel::ARROW_ERROR, |
74 | "/tmp/" ); |
75 | int rounds = 10000; |
76 | |
77 | int64_t start_time = current_time_ms(); |
78 | for (int i = 0; i < rounds; ++i) { |
79 | ARROW_LOG(DEBUG) << "This is the " |
80 | << "ARROW_DEBUG message" ; |
81 | } |
82 | int64_t elapsed = current_time_ms() - start_time; |
83 | std::cout << "Testing DEBUG log for " << rounds << " rounds takes " << elapsed << " ms." |
84 | << std::endl; |
85 | |
86 | start_time = current_time_ms(); |
87 | for (int i = 0; i < rounds; ++i) { |
88 | ARROW_LOG(ERROR) << "This is the " |
89 | << "RARROW_ERROR message" ; |
90 | } |
91 | elapsed = current_time_ms() - start_time; |
92 | std::cout << "Testing ARROW_ERROR log for " << rounds << " rounds takes " << elapsed |
93 | << " ms." << std::endl; |
94 | |
95 | start_time = current_time_ms(); |
96 | for (int i = 0; i < rounds; ++i) { |
97 | ARROW_CHECK(i >= 0) << "This is a ARROW_CHECK " |
98 | << "message but it won't show up" ; |
99 | } |
100 | elapsed = current_time_ms() - start_time; |
101 | std::cout << "Testing ARROW_CHECK(true) for " << rounds << " rounds takes " << elapsed |
102 | << " ms." << std::endl; |
103 | ArrowLog::ShutDownArrowLog(); |
104 | } |
105 | |
106 | } // namespace util |
107 | } // namespace arrow |
108 | |
109 | int main(int argc, char** argv) { |
110 | ::testing::InitGoogleTest(&argc, argv); |
111 | return RUN_ALL_TESTS(); |
112 | } |
113 | |