blob: ae2f93f7e50cb20727691ebf55249d4975c62ba8 [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 {
dmartingf0b82952017-09-19 14:54:30 +020026 private final ProfilerTask profilerTaskType = ProfilerTask.TEST;
27
28 @BeforeExperiment
29 void startProfiler() throws Exception {
30 Profiler.instance().start(ProfiledTaskKinds.ALL,
31 new InMemoryFileSystem().getPath("/out.dat").getOutputStream(), "benchmark", false,
32 BlazeClock.instance(), BlazeClock.instance().nanoTime());
33 }
34
35 @BeforeExperiment
36 void stopProfiler() throws Exception {
37 Profiler.instance().stop();
38 }
39
40 @Benchmark
41 void profiledWithAutoProfiler(int reps) {
42 for (int i = 0; i < reps; i++) {
janakr52859b42018-04-01 19:12:26 -070043 try (AutoProfiler p = AutoProfiler.profiled("profiling", profilerTaskType)) {}
dmartingf0b82952017-09-19 14:54:30 +020044 }
45 }
46
47 @Benchmark
48 void profiledManually(int reps) {
49 for (int i = 0; i < reps; i++) {
50 long startTime = Profiler.nanoTimeMaybe();
janakr52859b42018-04-01 19:12:26 -070051 Profiler.instance().logSimpleTask(startTime, profilerTaskType, "description");
dmartingf0b82952017-09-19 14:54:30 +020052 }
53 }
54}