xref: /aosp_15_r20/external/perfmark/agent/src/test/java/io/perfmark/agent/TransformerTestClasses.java (revision 27e8546d0ef5f99cf83d5252272c7dd38d18d29a)
1 /*
2  * Copyright 2021 Google LLC
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 package io.perfmark.agent;
18 
19 import io.perfmark.Link;
20 import io.perfmark.PerfMark;
21 import io.perfmark.Tag;
22 import io.perfmark.TaskCloseable;
23 import java.io.Closeable;
24 import java.io.IOException;
25 import java.util.concurrent.Executor;
26 import java.util.function.Consumer;
27 import javax.annotation.Nullable;
28 
29 final class TransformerTestClasses {
30 
31   static final class ClzAutoRecord {
ClzAutoRecord()32     public ClzAutoRecord() {
33       recordMe();
34     }
35 
recordMe()36     void recordMe() {
37       // seemingly nothing here.
38     }
39   }
40 
SomeRecord(@uppressWarnings"unused") int hi)41   record SomeRecord(@SuppressWarnings("unused") int hi) {
42     public SomeRecord {
43       PerfMark.startTask("task");
44       PerfMark.stopTask("task");
45     }
46   }
47 
48   static final class ClzCtorLambda implements Executor {
ClzCtorLambda()49     public ClzCtorLambda() {
50       execute(
51           () -> {
52             PerfMark.startTask("task");
53             PerfMark.stopTask("task");
54           });
55     }
56 
57     @Override
execute(@ullable final Runnable command)58     public void execute(@Nullable final Runnable command) {
59       command.run();
60     }
61   }
62 
63   static final class ClzWithClinit {
64     static {
65       Tag tag = PerfMark.createTag("tag", 1);
66       PerfMark.startTask("task");
67       PerfMark.stopTask("task");
68       PerfMark.startTask("task", tag);
69       PerfMark.stopTask("task", tag);
70     }
71   }
72 
73   static final class ClzWithInit {
74     {
75       Tag tag = PerfMark.createTag("tag", 1);
76       PerfMark.startTask("task");
77       PerfMark.stopTask("task");
78       PerfMark.startTask("task", tag);
79       PerfMark.stopTask("task", tag);
80     }
81   }
82 
83   static final class ClzWithCtor {
ClzWithCtor()84     public ClzWithCtor() {
85       Tag tag = PerfMark.createTag("tag", 1);
86       PerfMark.startTask("task");
87       PerfMark.stopTask("task");
88       PerfMark.startTask("task", tag);
89       PerfMark.stopTask("task", tag);
90     }
91   }
92 
93   static final class ClzWithLinks {
ClzWithLinks()94     public ClzWithLinks() {
95       PerfMark.startTask("task");
96       Link link = PerfMark.linkOut();
97       PerfMark.linkIn(link);
98       PerfMark.stopTask("task");
99     }
100   }
101 
102   static final class ClzWithCloseable {
ClzWithCloseable()103     public ClzWithCloseable() {
104       try (TaskCloseable discard = PerfMark.traceTask("task")) {}
105     }
106   }
107 
108   static final class ClzWithWrongCloseable {
ClzWithWrongCloseable()109     public ClzWithWrongCloseable() throws IOException {
110       try (Closeable discard = PerfMark.traceTask("task")) {}
111     }
112   }
113 
114   public interface InterfaceWithDefaults {
record()115     default void record() {
116       Tag tag = PerfMark.createTag("tag", 1);
117       PerfMark.startTask("task");
118       PerfMark.stopTask("task");
119       PerfMark.startTask("task", tag);
120       PerfMark.stopTask("task", tag);
121     }
122   }
123 
124   static final class Bar implements InterfaceWithDefaults {
Bar()125     public Bar() {
126       record();
127     }
128   }
129 
130   static final class ClzWithMethodRefs {
ClzWithMethodRefs()131     public ClzWithMethodRefs() {
132       execute(PerfMark::startTask);
133       execute(PerfMark::stopTask);
134     }
135 
execute(Consumer<String> method)136     void execute(Consumer<String> method) {
137       method.accept("task");
138     }
139   }
140 
TransformerTestClasses()141   private TransformerTestClasses() {}
142 }
143 
144 final class ClzFooter {
145   {
146     Tag tag = PerfMark.createTag("tag", 1);
147     PerfMark.startTask("task");
148     PerfMark.stopTask("task");
149     PerfMark.startTask("task", tag);
150     PerfMark.stopTask("task", tag);
151   }
152 }
153