|
| 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 <cstdlib> |
| 20 | +#include <iostream> |
| 21 | + |
| 22 | +#include "arrow/util/logging.h" |
| 23 | +#include "gtest/gtest.h" |
| 24 | + |
| 25 | +// This code is adapted from |
| 26 | +// https://github.com/ray-project/ray/blob/master/src/ray/util/logging_test.cc. |
| 27 | + |
| 28 | +namespace arrow { |
| 29 | + |
| 30 | +int64_t current_time_ms() { |
| 31 | + std::chrono::milliseconds ms_since_epoch = |
| 32 | + std::chrono::duration_cast<std::chrono::milliseconds>( |
| 33 | + std::chrono::steady_clock::now().time_since_epoch()); |
| 34 | + return ms_since_epoch.count(); |
| 35 | +} |
| 36 | + |
| 37 | +// This is not really test. |
| 38 | +// This file just print some information using the logging macro. |
| 39 | + |
| 40 | +void PrintLog() { |
| 41 | + ARROW_LOG(DEBUG) << "This is the" |
| 42 | + << " DEBUG" |
| 43 | + << " message"; |
| 44 | + ARROW_LOG(INFO) << "This is the" |
| 45 | + << " INFO message"; |
| 46 | + ARROW_LOG(WARNING) << "This is the" |
| 47 | + << " WARNING message"; |
| 48 | + ARROW_LOG(ERROR) << "This is the" |
| 49 | + << " ERROR message"; |
| 50 | + ARROW_CHECK(true) << "This is a ARROW_CHECK" |
| 51 | + << " message but it won't show up"; |
| 52 | + // The following 2 lines should not run since it will cause program failure. |
| 53 | + // ARROW_LOG(FATAL) << "This is the FATAL message"; |
| 54 | + // ARROW_CHECK(false) << "This is a ARROW_CHECK message but it won't show up"; |
| 55 | +} |
| 56 | + |
| 57 | +TEST(PrintLogTest, LogTestWithoutInit) { |
| 58 | + // Without ArrowLog::StartArrowLog, this should also work. |
| 59 | + PrintLog(); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(PrintLogTest, LogTestWithInit) { |
| 63 | + // Test empty app name. |
| 64 | + ArrowLog::StartArrowLog("", ArrowLogLevel::ARROW_DEBUG); |
| 65 | + PrintLog(); |
| 66 | + ArrowLog::ShutDownArrowLog(); |
| 67 | +} |
| 68 | + |
| 69 | +// This test will output large amount of logs to stderr, should be disabled in travis. |
| 70 | +TEST(LogPerfTest, PerfTest) { |
| 71 | + ArrowLog::StartArrowLog("/fake/path/to/appdire/LogPerfTest", ArrowLogLevel::ARROW_ERROR, |
| 72 | + "/tmp/"); |
| 73 | + int rounds = 10000; |
| 74 | + |
| 75 | + int64_t start_time = current_time_ms(); |
| 76 | + for (int i = 0; i < rounds; ++i) { |
| 77 | + ARROW_LOG(DEBUG) << "This is the " |
| 78 | + << "ARROW_DEBUG message"; |
| 79 | + } |
| 80 | + int64_t elapsed = current_time_ms() - start_time; |
| 81 | + std::cout << "Testing DEBUG log for " << rounds << " rounds takes " << elapsed << " ms." |
| 82 | + << std::endl; |
| 83 | + |
| 84 | + start_time = current_time_ms(); |
| 85 | + for (int i = 0; i < rounds; ++i) { |
| 86 | + ARROW_LOG(ERROR) << "This is the " |
| 87 | + << "RARROW_ERROR message"; |
| 88 | + } |
| 89 | + elapsed = current_time_ms() - start_time; |
| 90 | + std::cout << "Testing ARROW_ERROR log for " << rounds << " rounds takes " << elapsed |
| 91 | + << " ms." << std::endl; |
| 92 | + |
| 93 | + start_time = current_time_ms(); |
| 94 | + for (int i = 0; i < rounds; ++i) { |
| 95 | + ARROW_CHECK(i >= 0) << "This is a ARROW_CHECK " |
| 96 | + << "message but it won't show up"; |
| 97 | + } |
| 98 | + elapsed = current_time_ms() - start_time; |
| 99 | + std::cout << "Testing ARROW_CHECK(true) for " << rounds << " rounds takes " << elapsed |
| 100 | + << " ms." << std::endl; |
| 101 | + ArrowLog::ShutDownArrowLog(); |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace arrow |
| 105 | + |
| 106 | +int main(int argc, char** argv) { |
| 107 | + ::testing::InitGoogleTest(&argc, argv); |
| 108 | + return RUN_ALL_TESTS(); |
| 109 | +} |
0 commit comments