You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+56-69Lines changed: 56 additions & 69 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,11 @@
1
1
2
+
2
3
# Snippet
3
4
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.
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
54
56
multiple files. ex. you have started a measurement in `Application.onCreate()` and end the
55
57
measurement on the landing activity. Use `Snippet.startCapture(tag)` to start the measurement and
56
58
find the token using `Snippet.find(tag)` and call `endCapture()` on that.
57
59
58
-
> Use case 1: Code that can be passed as lambda.
60
+
**Use case 1:** Code that can be passed as lambda.
> 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.
76
77
77
78
78
79
public class SampleApplication extends Application {
@@ -89,7 +90,7 @@ Setup in 3 easy steps:
89
90
}
90
91
}
91
92
92
-
End the measurement in `MainActivity` class
93
+
and ended in `MainActivity` class
93
94
94
95
public class MainActivity extends AppCompatActivity {
95
96
@@ -105,9 +106,12 @@ End the measurement in `MainActivity` class
105
106
super.onStart();
106
107
}
107
108
}
109
+
110
+
**Use case 3:** Using Log Tokens. Log tokens can be used within a class or method easily.
108
111
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.
111
115
112
116
public class MainActivity extends AppCompatActivity {
113
117
@@ -125,29 +129,23 @@ End the measurement in `MainActivity` class
125
129
}
126
130
127
131
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.
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)
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.
141
140
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.
143
142
144
143
## Splits
145
144
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**
151
149
152
150
1. Once you get a log token using `Snippet.startCapture()` call.
153
151
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.
162
160
## ThreadLocks
163
161
164
162
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
* may want to add some extra information into the existing information and add it to files.
175
172
* We can plugin a custom execution path or method through `Snippet.install(executionPath)` method.
176
173
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.
181
175
182
-
if(BuildConfig.DEBUG) { // For debug builds prints the measurements
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.
201
195
202
196
**NOTE**: In almost all the cases, every new execution path that would be created, would require a new extension of `ExtendableLogToken`
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.
290
283
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.
296
285
297
286
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.
0 commit comments