Add counters interface
[folly.git] / folly / compression / Counters.h
1 /*
2  * Copyright 2018-present Facebook, Inc.
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 <string>
20
21 #include <folly/Function.h>
22 #include <folly/Optional.h>
23 #include <folly/Range.h>
24
25 namespace folly {
26 namespace io {
27 enum class CodecType;
28 } // namespace io
29
30 enum class CompressionCounterKey {
31   BYTES_BEFORE_COMPRESSION = 0,
32   BYTES_AFTER_COMPRESSION = 1,
33   BYTES_BEFORE_DECOMPRESSION = 2,
34   BYTES_AFTER_DECOMPRESSION = 3,
35 };
36
37 enum class CompressionCounterType {
38   AVG = 0,
39   SUM = 1,
40 };
41
42 /**
43  * This functions is an extension point when FOLLY_HAVE_WEAK_SYMBOLS is true.
44  * There is a default no-op implementation provided which can be overrided by
45  * linking in a library which provides its own definition.
46  *
47  * @param codecType   The type of the codec for this counter.
48  * @param codecName   The name of the codec for this counter. If the codecName
49  *                    is empty it should be defaulted using the codecType.
50  * @param level       Optionally the level used to construct the codec.
51  * @param key         The key of the counter.
52  * @param counterType The type of the counter.
53  * @returns           A function to increment the counter for the given key and
54  *                    type. It may be an empty folly::Function.
55  */
56 folly::Function<void(double)> makeCompressionCounterHandler(
57     folly::io::CodecType codecType,
58     folly::StringPiece codecName,
59     folly::Optional<int> level,
60     CompressionCounterKey key,
61     CompressionCounterType counterType);
62
63 namespace detail {
64
65 /// Wrapper around the makeCompressionCounterHandler() extension point.
66 class CompressionCounter {
67  public:
68   CompressionCounter() {}
69   CompressionCounter(
70       folly::io::CodecType codecType,
71       folly::StringPiece codecName,
72       folly::Optional<int> level,
73       CompressionCounterKey key,
74       CompressionCounterType counterType) {
75     increment_ = makeCompressionCounterHandler(
76         codecType, codecName, std::move(level), key, counterType);
77   }
78
79   void operator+=(double sum) {
80     if (increment_) {
81       increment_(sum);
82     }
83   }
84
85   void operator++() {
86     *this += 1.0;
87   }
88
89   void operator++(int) {
90     *this += 1.0;
91   }
92
93   bool hasImplementation() const {
94     return static_cast<bool>(increment_);
95   }
96
97  private:
98   folly::Function<void(double)> increment_;
99 };
100
101 } // namespace detail
102 } // namespace folly