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 #include <glog/logging.h>
19 #include <folly/Benchmark.h>
20 #include <folly/File.h>
21 #include <folly/gen/Base.h>
22 #include <folly/gen/File.h>
24 using namespace folly::gen;
26 BENCHMARK(ByLine_Pipes, iters) {
35 thread = std::thread([wfd, iters] {
37 PCHECK(::write(wfd, &x, 1) == 1); // signal startup
38 FILE* f = fdopen(wfd, "w");
40 for (size_t i = 1; i <= iters; ++i) {
41 fprintf(f, "%zu\n", i);
46 PCHECK(::read(rfd, &buf, 1) == 1); // wait for startup
50 auto s = byLine(folly::File(rfd)) | eachTo<int64_t>() | sum;
51 folly::doNotOptimizeAway(s);
55 CHECK_EQ(s, int64_t(iters) * (iters + 1) / 2);
60 // Results from an Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
61 // ============================================================================
62 // folly/gen/test/FileBenchmark.cpp relative time/iter iters/s
63 // ============================================================================
64 // ByLine_Pipes 148.63ns 6.73M
65 // ============================================================================
67 int main(int argc, char *argv[]) {
68 gflags::ParseCommandLineFlags(&argc, &argv, true);
69 folly::runBenchmarks();