2 * Copyright 2016 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.
17 // @author: Bert Maher <bertrand@fb.com>
24 #include <folly/Benchmark.h>
25 #include <folly/ProducerConsumerQueue.h>
26 #include <folly/portability/GFlags.h>
27 #include <folly/stats/Histogram.h>
28 #include <folly/stats/Histogram-defs.h>
29 #include <glog/logging.h>
33 using namespace folly;
35 typedef unsigned int ThroughputType;
36 typedef ProducerConsumerQueue<ThroughputType> ThroughputQueueType;
38 typedef unsigned long LatencyType;
39 typedef ProducerConsumerQueue<LatencyType> LatencyQueueType;
41 template<class QueueType>
42 struct ThroughputTest {
43 explicit ThroughputTest(size_t size, int iters, int cpu0, int cpu1)
55 CPU_SET(cpu0_, &cpuset);
56 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
58 for (int i = 0; i < iters_; ++i) {
59 ThroughputType item = i;
60 while (!queue_.write((ThroughputType) item)) {
69 CPU_SET(cpu1_, &cpuset);
70 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
72 for (int i = 0; i < iters_; ++i) {
73 ThroughputType item = 0;
74 while (!queue_.read(item)) {
76 doNotOptimizeAway(item);
81 std::atomic<bool> done_;
87 template<class QueueType>
89 explicit LatencyTest(size_t size, int iters, int cpu0, int cpu1)
100 void computeTimeCost() {
102 clock_gettime(CLOCK_REALTIME, &start);
103 for (int i = 0; i < iters_; ++i) {
105 clock_gettime(CLOCK_REALTIME, &tv);
107 clock_gettime(CLOCK_REALTIME, &end);
108 time_cost_ = 2 * detail::timespecDiff(end, start) / iters_;
115 CPU_SET(cpu0_, &cpuset);
116 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
118 for (int i = 0; i < iters_; ++i) {
119 timespec sleeptime, sleepstart;
120 clock_gettime(CLOCK_REALTIME, &sleepstart);
122 clock_gettime(CLOCK_REALTIME, &sleeptime);
123 } while (detail::timespecDiff(sleeptime, sleepstart) < 1000000);
126 clock_gettime(CLOCK_REALTIME, &tv);
127 while (!queue_.write((LatencyType) tv.tv_nsec)) {
136 CPU_SET(cpu1_, &cpuset);
137 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
139 for (int i = 0; i < iters_; ++i) {
140 unsigned long enqueue_nsec;
141 while (!queue_.read(enqueue_nsec)) {
145 clock_gettime(CLOCK_REALTIME, &tv);
146 int diff = tv.tv_nsec - enqueue_nsec - time_cost_;
151 // Naive log-scale bucketing.
154 bucket <= 30 && (1 << bucket) <= diff;
157 hist_.addValue(bucket - 1);
161 void printHistogram() {
162 hist_.toTSV(std::cout);
166 std::atomic<bool> done_;
171 Histogram<int> hist_;
174 void BM_ProducerConsumer(int iters, int size) {
175 BenchmarkSuspender susp;
177 ThroughputTest<ThroughputQueueType> *test =
178 new ThroughputTest<ThroughputQueueType>(size, iters, -1, -1);
181 std::thread producer( [test] { test->producer(); } );
182 std::thread consumer( [test] { test->consumer(); } );
190 void BM_ProducerConsumerAffinity(int iters, int size) {
191 BenchmarkSuspender susp;
193 ThroughputTest<ThroughputQueueType> *test =
194 new ThroughputTest<ThroughputQueueType>(size, iters, 0, 1);
197 std::thread producer( [test] { test->producer(); } );
198 std::thread consumer( [test] { test->consumer(); } );
206 void BM_ProducerConsumerLatency(int /* iters */, int size) {
207 BenchmarkSuspender susp;
209 LatencyTest<LatencyQueueType> *test =
210 new LatencyTest<LatencyQueueType>(size, 100000, 0, 1);
213 std::thread producer( [test] { test->producer(); } );
214 std::thread consumer( [test] { test->consumer(); } );
219 test->printHistogram();
224 BENCHMARK_DRAW_LINE();
226 BENCHMARK_PARAM(BM_ProducerConsumer, 1048574);
227 BENCHMARK_PARAM(BM_ProducerConsumerAffinity, 1048574);
228 BENCHMARK_PARAM(BM_ProducerConsumerLatency, 1048574);
232 int main(int argc, char** argv) {
233 google::InitGoogleLogging(argv[0]);
234 gflags::ParseCommandLineFlags(&argc, &argv, true);
242 Benchmark on Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
255 ============================================================================
256 folly/test/ProducerConsumerQueueBenchmark.cpp relative time/iter iters/s
257 ============================================================================
258 ----------------------------------------------------------------------------
259 BM_ProducerConsumer(1048574) 7.52ns 132.90M
260 BM_ProducerConsumerAffinity(1048574) 8.28ns 120.75M
261 BM_ProducerConsumerLatency(1048574) 10.00s 99.98m
262 ============================================================================