Skip to content

Commit 6ac862a

Browse files
Add files via upload
1 parent 89bf8c2 commit 6ac862a

File tree

1 file changed

+56
-69
lines changed

1 file changed

+56
-69
lines changed

README.md

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11

2+
23
# Snippet
34

4-
> Snippet is a extensible android library to measure execution times of the code sections in a way that does not compromise with the readability and can be shipped to production without any additional setup. > New behaviours can be added in the library by extending `MeasuredExecutionPath` - The code path the does the measurement code spans `ReleaseExecutionPath` - A no-op path (default path) that is usually installed in the release variants.
5-
>
5+
`Snippet` is an extensible android library to measure execution times of the code sections in a way that does not compromise with the readability and can be shipped to production without any additional setup. New behaviours can be added in the library by extending Execution paths. 2 execution paths provided with the library are:
6+
1. `MeasuredExecutionPath` - The code path the does the measurement code spans
7+
2. `ReleaseExecutionPath` - A no-op path (default path) that is usually installed in the release variants.
8+
69

710
# Features
811

@@ -35,8 +38,8 @@ Setup in 3 easy steps:
3538
**Below is the sample setup code:**
3639

3740

38-
if(BuildConfig.DEBUG) {
39-
Snippet.install(new Snippet.MeasuredExecutionPath());
41+
if(BuildConfig.DEBUG) {
42+
Snippet.install(new Snippet.MeasuredExecutionPath());
4043
Snippet.newFilter("SampleFilter");
4144
Snippet.addFlag(Snippet.FLAG_METADATA_LINE | Snippet.FLAG_METADATA_THREAD_INFO);
4245
}
@@ -45,17 +48,16 @@ Setup in 3 easy steps:
4548

4649
> There are 3 ways to capture the code path.
4750
48-
1. `Snippet.capture(Closure closure)` - For continuous section of code, pass code as lambda inside
49-
the closure.
50-
2. `Snippet.startCapture()/LogToken.endCapture()` - For non contiguous sections of code possibly
51-
inside the same file . Or places where `capture(closure)` does not work as in case of some
52-
anonymous inner classes.
53-
3. `Snippet.startCapture(String tag)/Snippet.find(tag).endCapture()` - For code flows spanning over
51+
1. `Snippet.capture(Closure closure)` - For continuous section of code, pass code as lambda inside the closure.
52+
53+
2. `Snippet.startCapture()/LogToken.endCapture()` - For non contiguous sections of code possibly inside the same file . Or places where `capture(closure)` does not work as in case of some anonymous inner classes.
54+
55+
4. `Snippet.startCapture(String tag)/Snippet.find(tag).endCapture()` - For code flows spanning over
5456
multiple files. ex. you have started a measurement in `Application.onCreate()` and end the
5557
measurement on the landing activity. Use `Snippet.startCapture(tag)` to start the measurement and
5658
find the token using `Snippet.find(tag)` and call `endCapture()` on that.
5759

58-
> Use case 1: Code that can be passed as lambda.
60+
**Use case 1:** Code that can be passed as lambda.
5961

6062

6163
@Override
@@ -69,10 +71,9 @@ Setup in 3 easy steps:
6971
Snippet.capture(()-> super.onCreate(savedInstanceState));
7072
}
7173

72-
73-
> Use case 2: Measurement starts from a different class and ends in a
74-
> different class. Below the measurement has started in Application
75-
> class and will end in an Activity class
74+
**Use case 2:** Measurement starts from a different class and ends in a
75+
different class.
76+
Below the measurement has started in Application class and will end in an Activity class. We use TAG based API to handle this case.
7677

7778

7879
public class SampleApplication extends Application {
@@ -89,7 +90,7 @@ Setup in 3 easy steps:
8990
}
9091
}
9192
92-
End the measurement in `MainActivity` class
93+
and ended in `MainActivity` class
9394

9495
public class MainActivity extends AppCompatActivity {
9596

@@ -105,9 +106,12 @@ End the measurement in `MainActivity` class
105106
super.onStart();
106107
}
107108
}
109+
110+
**Use case 3:** Using Log Tokens. Log tokens can be used within a class or method easily.
108111

109-
> Use case 3: Using Log Tokens. Log tokens can be used within a class or method easily. If you need to use log tokens across multiple files, it would be difficult try using tags.
110-
112+
If you need to use log tokens to measure code spread across multiple files and your measurements do not deal with multiple threads, use TAG based API discussed above.
113+
114+
**LogTokens** will shine if you need to fire multiple threads at the same time and measurement is inside the common code that all the threads execute. Then create separate log token and hand over to different threads. We are working to fix this limitation.
111115

112116
public class MainActivity extends AppCompatActivity {
113117
@@ -125,29 +129,23 @@ End the measurement in `MainActivity` class
125129
}
126130
127131

128-
> The measurements looks like below all the specified information is
129-
> captured out of the box.
130-
132+
Snippet capture all the context information such as class, method, thread, line number out of the box and creates pretty logs as shown below. So that you can locate your logs easily.
133+
On top of that if you need more verbosity, then each API has a string based overload too. You can explore that also.
131134

132-
133135
2021-12-11 15:11:24.197 11400-11400/com.microsoft.sample D/SampleFilter: [Class = MainActivity]|::::|[Method = onCreate]|::::|<Line no. 21>|::::|[Thread name = main]|::::||::::|(18 ms)
134136
2021-12-11 15:11:24.376 11400-11400/com.microsoft.sample D/SampleFilter: Time to set the content view|::::|[Class = MainActivity]|::::|[Method = onCreate]|::::|<Line no. 29>|::::|[Thread name = main]|::::||::::|(178 ms)
135137
2021-12-11 15:11:24.377 11400-11400/com.microsoft.sample D/SampleFilter: [Class = MainActivity]|::::|[Method = onCreate]|::::|<Line no. 32>|::::|[Thread name = main]|::::||::::|(295 ms)
136138

137-
We can create multiple execution path implementations by extending MeasuredExecutionPath classes,
138-
and do additional work with the data that is provided by the measured path such as logging it in
139-
remote servers, putting all the data to a DB, files etc. Check `FileExecutionPath` in the sample
140-
app.
139+
We can create multiple execution path implementations by extending MeasuredExecutionPath classes, and do customised work with the data that is provided by the measured path such as logging it in remote servers, putting all the data to a DB, files etc. Check `FileExecutionPath` in the sample app.
141140

142-
Check out the sample app in `app/` to see it in action.
141+
Check out the sample app in `app/` to see the process in action.
143142

144143
## Splits
145144

146-
**Splits** can be defined as a logical span of code within a capture. Splits can span within same
147-
file or different files. The purpose is to double down on the focussed areas, and help in debugging.
148-
It could be used for seeing what is the contribution of a small portion of code within a capture and
149-
find of problem areas.
150-
**It is advisable to always use splits only for debugging purposes.** **It is not advised to ship splits related code to production.** **How to use splits**
145+
**Splits** can be defined as a logical span of code within a capture. Splits can span within same file or different files. The purpose is to double down on the focussed areas, and help in debugging.
146+
It could be used for seeing what is the contribution of a small portion of code within a capture and find of problem areas.
147+
148+
**It is advisable to always use splits only for debugging purposes.** **It is not advised to ship splits related code to production.** **Below is the demo on how to use splits**
151149

152150
1. Once you get a log token using `Snippet.startCapture()` call.
153151
2. You can call `logtoken.addSplit()` call. It will print the amount of time that has passed since
@@ -162,8 +160,7 @@ find of problem areas.
162160
## ThreadLocks
163161

164162
Thread lock is the functionality where the thread that started the measurement
165-
using `startCapture()` could only end it. If other thread tries to do that, an error is logged and
166-
action is skipped. It can be enabled easily like this
163+
using `startCapture()` could only end it. If other thread tries to do that, an error is logged and action is skipped. It can be enabled easily like this
167164

168165
Snippet.startCapture().enableThreadLock()
169166

@@ -174,30 +171,27 @@ Snippet.startCapture().enableThreadLock()
174171
* may want to add some extra information into the existing information and add it to files.
175172
* We can plugin a custom execution path or method through `Snippet.install(executionPath)` method.
176173

177-
Snippet comes with a `MeasuredExecutionPath` & `ReleaseExecutionPath` , measured path is the one
178-
that routes the the Snippet API calls to the core library functionality, but `ReleaseExecutionPath`
179-
make Snippet no-op. Release path is the default path for Snippet. User has to set the path for
180-
specific build types using `Snippet.install(executionPath)`
174+
Snippet comes with a `MeasuredExecutionPath` & `ReleaseExecutionPath` , measured path is the one that routes the the Snippet API calls to the core library functionality, and `ReleaseExecutionPath` make Snippet no-op. Release path is the default path for Snippet. User has to set the path for specific build types using `Snippet.install(executionPath)` . Below is an example, where we set the `MeasuredExecutionPath` on DEBUG builds and `FileExecutionPath` on RELEASE builds.
181175

182-
if(BuildConfig.DEBUG) { // For debug builds prints the measurements
183-
Snippet.install(new Snippet.MeasuredExecutionPath()); Snippet.newFilter("SampleFilter");
184-
} else {
185-
// For release builds write the measurememnts to file.
186-
Snippet.install(new FileExecutionPath()); Snippet.newFilter("ReleaseFilter");
187-
}
188-
Snippet.addFlag(Snippet.FLAG_METADATA_LINE | Snippet.FLAG_METADATA_THREAD_INFO);
176+
177+
if(BuildConfig.DEBUG) {
178+
Snippet.install(new Snippet.MeasuredExecutionPath());
179+
Snippet.newFilter("SampleFilter");
180+
} else {
181+
Snippet.install(new FileExecutionPath());
182+
Snippet.newFilter("ReleaseFilter");
183+
}
184+
Snippet.addFlag(Snippet.FLAG_METADATA_LINE | Snippet.FLAG_METADATA_THREAD_INFO);
185+
189186

190187
## Writing a custom execution path
191188

192189
1. Extend `ExecutionPath`, in our example we will extend `MeasuredExecutionPath`.
193-
2. Override `ExecutionPath#capture(Closure)` and `ExecutionPath#capture(String, Closure)` This will
194-
make sure that you are implementing a custom code for lambda based API.
195-
3. You need to provide a custom log token also and override the `LogToken#endCapture()`
196-
and `LogToken#endCapture(String)` so that you can perform the custom actions on all types(
197-
contiguous/non contiguous) of code.
198-
4. For doing this extend ExtendableLogToken and override `ExtendableLogToken#endCapture(String)`,
199-
and `ExtendableLogToken#endCapture()`. Once done, return the `ExtendableLogToken` instance
200-
from `Snippet#startCapture(String)`, and `Snippet#startCapture(String)` methods.
190+
191+
2. Override `ExecutionPath#capture(Closure)` and `ExecutionPath#capture(String, Closure)` This will make sure that you are implementing a custom code for lambda based API.
192+
4. You need to provide a custom log token also and override the `LogToken#endCapture()` and `LogToken#endCapture(String)` so that you can perform the custom actions on all types(contiguous/non contiguous) of code.
193+
5. For doing this extend ExtendableLogToken and override `ExtendableLogToken#endCapture(String)`,
194+
and `ExtendableLogToken#endCapture()`. Once done, return the `ExtendableLogToken` instance from `Snippet#startCapture(String)`, and `Snippet#startCapture(String)` methods.
201195

202196
**NOTE**: In almost all the cases, every new execution path that would be created, would require a new extension of `ExtendableLogToken`
203197

@@ -267,9 +261,10 @@ Snippet.addFlag(Snippet.FLAG_METADATA_LINE | Snippet.FLAG_METADATA_THREAD_INFO);
267261
ExecutionContext context = super.endCapture();
268262
writeToFile(context);
269263
return context;
270-
}
271-
}
272-
}
264+
}
265+
}
266+
}
267+
273268

274269

275270
Finally, install it at the application create,
@@ -284,22 +279,13 @@ Cheers,
284279
285280
## Contributing
286281
287-
This project welcomes contributions and suggestions. Most contributions require you to agree to a
288-
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
289-
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
282+
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
290283
291-
When you submit a pull request, a CLA bot will automatically determine whether you need to
292-
provide
293-
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the
294-
instructions
295-
provided by the bot. You will only need to do this once across all repos using our CLA.
284+
When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
296285
297286
This project has adopted
298-
the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
299-
For more information see
300-
the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
301-
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or
302-
comments.
287+
the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see
288+
the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
303289
304290
## Trademarks
305291
@@ -327,3 +313,4 @@ dependencies {
327313
implementation 'com.github.microsoft:snippet-timekeeper:Tag'
328314
}
329315
```
316+

0 commit comments

Comments
 (0)