blob: 671c96e711a08b4f2af9b04cd08c5c0bf6f7257d [file] [log] [blame]
dmartingf0b82952017-09-19 14:54:30 +02001// Copyright 2015 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.profiler;
15
16import com.google.caliper.BeforeExperiment;
17import com.google.caliper.Benchmark;
18import com.google.devtools.build.lib.clock.BlazeClock;
19import com.google.devtools.build.lib.profiler.Profiler.ProfiledTaskKinds;
20import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
21
22/**
23 * Microbenchmarks for the overhead of {@link AutoProfiler} over using {@link Profiler} manually.
24 */
25public class AutoProfilerBenchmark {
26
27 private final Object obj = new Object();
28 private final ProfilerTask profilerTaskType = ProfilerTask.TEST;
29
30 @BeforeExperiment
31 void startProfiler() throws Exception {
32 Profiler.instance().start(ProfiledTaskKinds.ALL,
33 new InMemoryFileSystem().getPath("/out.dat").getOutputStream(), "benchmark", false,
34 BlazeClock.instance(), BlazeClock.instance().nanoTime());
35 }
36
37 @BeforeExperiment
38 void stopProfiler() throws Exception {
39 Profiler.instance().stop();
40 }
41
42 @Benchmark
43 void profiledWithAutoProfiler(int reps) {
44 for (int i = 0; i < reps; i++) {
45 try (AutoProfiler p = AutoProfiler.profiled(obj, profilerTaskType)) {
46 }
47 }
48 }
49
50 @Benchmark
51 void profiledManually(int reps) {
52 for (int i = 0; i < reps; i++) {
53 long startTime = Profiler.nanoTimeMaybe();
54 Profiler.instance().logSimpleTask(startTime, profilerTaskType, obj);
55 }
56 }
57}