2 * Copyright 2018-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <folly/Function.h>
22 #include <folly/Optional.h>
23 #include <folly/Range.h>
30 enum class CompressionCounterKey {
31 BYTES_BEFORE_COMPRESSION = 0,
32 BYTES_AFTER_COMPRESSION = 1,
33 BYTES_BEFORE_DECOMPRESSION = 2,
34 BYTES_AFTER_DECOMPRESSION = 3,
37 COMPRESSION_MILLISECONDS = 6,
38 DECOMPRESSION_MILLISECONDS = 7,
41 enum class CompressionCounterType {
47 * This functions is an extension point when FOLLY_HAVE_WEAK_SYMBOLS is true.
48 * There is a default no-op implementation provided which can be overrided by
49 * linking in a library which provides its own definition.
51 * @param codecType The type of the codec for this counter.
52 * @param codecName The name of the codec for this counter. If the codecName
53 * is empty it should be defaulted using the codecType.
54 * @param level Optionally the level used to construct the codec.
55 * @param key The key of the counter.
56 * @param counterType The type of the counter.
57 * @returns A function to increment the counter for the given key and
58 * type. It may be an empty folly::Function.
60 folly::Function<void(double)> makeCompressionCounterHandler(
61 folly::io::CodecType codecType,
62 folly::StringPiece codecName,
63 folly::Optional<int> level,
64 CompressionCounterKey key,
65 CompressionCounterType counterType);
69 /// Wrapper around the makeCompressionCounterHandler() extension point.
70 class CompressionCounter {
72 CompressionCounter() {}
74 folly::io::CodecType codecType,
75 folly::StringPiece codecName,
76 folly::Optional<int> level,
77 CompressionCounterKey key,
78 CompressionCounterType counterType) {
79 increment_ = makeCompressionCounterHandler(
80 codecType, codecName, std::move(level), key, counterType);
83 void operator+=(double sum) {
93 void operator++(int) {
97 bool hasImplementation() const {
98 return static_cast<bool>(increment_);
102 folly::Function<void(double)> increment_;
105 } // namespace detail