Skip to content

Commit 54cb36f

Browse files
Daniel Munozfacebook-github-bot
authored andcommitted
Move MeasureTime to common namespace (facebookincubator#9567)
Summary: Pull Request resolved: facebookincubator#9567 Make it more general. Reviewed By: kgpai, Sullivan-Patrick Differential Revision: D56436083 fbshipit-source-id: 635105e87157345ecd994ff934bee00d8ff18148
1 parent 38a7335 commit 54cb36f

File tree

5 files changed

+117
-34
lines changed

5 files changed

+117
-34
lines changed

velox/dwio/common/MeasureTime.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <chrono>
20+
#include <functional>
21+
#include <optional>
22+
23+
namespace facebook {
24+
namespace velox {
25+
namespace dwio {
26+
namespace common {
27+
28+
class MeasureTime {
29+
public:
30+
explicit MeasureTime(
31+
const std::function<void(std::chrono::high_resolution_clock::duration)>&
32+
callback)
33+
: callback_{callback},
34+
startTime_{std::chrono::high_resolution_clock::now()} {}
35+
36+
MeasureTime(const MeasureTime&) = delete;
37+
MeasureTime(MeasureTime&&) = delete;
38+
MeasureTime& operator=(const MeasureTime&) = delete;
39+
MeasureTime& operator=(MeasureTime&& other) = delete;
40+
41+
~MeasureTime() {
42+
callback_(std::chrono::high_resolution_clock::now() - startTime_);
43+
}
44+
45+
private:
46+
const std::function<void(std::chrono::high_resolution_clock::duration)>&
47+
callback_;
48+
const std::chrono::time_point<std::chrono::high_resolution_clock> startTime_;
49+
};
50+
51+
// Make sure you don't pass a lambda to this function, because that will cause a
52+
// std::function to be created on the fly (implicitly), and when we return from
53+
// this function that std::function won't exist anymore. So when MeasureTime is
54+
// destroyed, it will try to access a non-existing std::function.
55+
inline std::optional<MeasureTime> measureTimeIfCallback(
56+
const std::function<void(std::chrono::high_resolution_clock::duration)>&
57+
callback) {
58+
if (callback) {
59+
return std::make_optional<MeasureTime>(callback);
60+
}
61+
return std::nullopt;
62+
}
63+
64+
} // namespace common
65+
} // namespace dwio
66+
} // namespace velox
67+
} // namespace facebook

velox/dwio/common/OnDemandUnitLoader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
#include "velox/dwio/common/OnDemandUnitLoader.h"
1818

1919
#include "velox/common/base/Exceptions.h"
20+
#include "velox/dwio/common/MeasureTime.h"
2021
#include "velox/dwio/common/UnitLoaderTools.h"
2122

22-
using facebook::velox::dwio::common::unit_loader_tools::measureWithCallback;
23+
using facebook::velox::dwio::common::measureTimeIfCallback;
2324

2425
namespace facebook::velox::dwio::common {
2526

@@ -49,7 +50,7 @@ class OnDemandUnitLoader : public UnitLoader {
4950
}
5051

5152
{
52-
auto measure = measureWithCallback(blockedOnIoCallback_);
53+
auto measure = measureTimeIfCallback(blockedOnIoCallback_);
5354
loadUnits_[unit]->load();
5455
}
5556
loadedUnit_ = unit;

velox/dwio/common/UnitLoaderTools.h

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,6 @@
2828

2929
namespace facebook::velox::dwio::common::unit_loader_tools {
3030

31-
class Measure {
32-
public:
33-
explicit Measure(
34-
const std::function<void(std::chrono::high_resolution_clock::duration)>&
35-
callback)
36-
: callback_{callback},
37-
startTime_{std::chrono::high_resolution_clock::now()} {}
38-
39-
Measure(const Measure&) = delete;
40-
Measure(Measure&&) = delete;
41-
Measure& operator=(const Measure&) = delete;
42-
Measure& operator=(Measure&& other) = delete;
43-
44-
~Measure() {
45-
callback_(std::chrono::high_resolution_clock::now() - startTime_);
46-
}
47-
48-
private:
49-
const std::function<void(std::chrono::high_resolution_clock::duration)>&
50-
callback_;
51-
const std::chrono::time_point<std::chrono::high_resolution_clock> startTime_;
52-
};
53-
54-
inline std::optional<Measure> measureWithCallback(
55-
const std::function<void(std::chrono::high_resolution_clock::duration)>&
56-
callback) {
57-
if (callback) {
58-
return std::make_optional<Measure>(callback);
59-
}
60-
return std::nullopt;
61-
}
62-
6331
// This class can create many callbacks that can be distributed to unit loader
6432
// factories. Only when the last created callback is activated, this class will
6533
// emit the original callback.

velox/dwio/common/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_executable(
2727
LocalFileSinkTest.cpp
2828
MemorySinkTest.cpp
2929
LoggedExceptionTest.cpp
30+
MeasureTimeTests.cpp
3031
ParallelForTest.cpp
3132
RangeTests.cpp
3233
ReadFileInputStreamTests.cpp
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <gtest/gtest.h>
18+
19+
#include "velox/dwio/common/MeasureTime.h"
20+
21+
using namespace ::testing;
22+
using namespace ::facebook::velox::dwio::common;
23+
24+
TEST(MeasureTimeTests, DoesntCreateMeasureIfNoCallback) {
25+
EXPECT_FALSE(measureTimeIfCallback(nullptr).has_value());
26+
}
27+
28+
TEST(MeasureTimeTests, CreatesMeasureIfCallback) {
29+
auto callback =
30+
std::function<void(std::chrono::high_resolution_clock::duration)>(
31+
[](const auto&) {});
32+
EXPECT_TRUE(measureTimeIfCallback(callback).has_value());
33+
}
34+
35+
TEST(MeasureTimeTests, MeasuresTime) {
36+
bool measured{false};
37+
{
38+
auto callback =
39+
std::function<void(std::chrono::high_resolution_clock::duration)>(
40+
[&measured](const auto&) { measured = true; });
41+
auto measure = measureTimeIfCallback(callback);
42+
EXPECT_TRUE(measure.has_value());
43+
EXPECT_FALSE(measured);
44+
}
45+
EXPECT_TRUE(measured);
46+
}

0 commit comments

Comments
 (0)