Skip to content

Commit b6b7214

Browse files
Vivek Bansalsoftvar
authored andcommitted
add logs for debugging init and load times
1 parent 04bd888 commit b6b7214

File tree

10 files changed

+325
-39
lines changed

10 files changed

+325
-39
lines changed

demo/src/main/java/com/vwo/sampleapp/activities/MainActivity.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import android.net.Uri;
88
import android.os.Bundle;
99
import android.os.Handler;
10-
import android.os.Looper;
1110
import android.text.Editable;
1211
import android.text.TextUtils;
1312
import android.text.TextWatcher;
@@ -24,12 +23,12 @@
2423
import com.vwo.mobile.VWO;
2524
import com.vwo.mobile.VWOConfig;
2625
import com.vwo.mobile.events.VWOStatusListener;
26+
import com.vwo.mobile.timetracker.TimeTracker;
2727
import com.vwo.mobile.utils.VWOLog;
2828
import com.vwo.sampleapp.R;
2929
import com.vwo.sampleapp.fragments.FragmentHousingMain;
3030
import com.vwo.sampleapp.fragments.FragmentSortingMain;
3131
import com.vwo.sampleapp.interfaces.NavigationToggleListener;
32-
import com.vwo.sampleapp.utils.Constants;
3332
import com.vwo.sampleapp.utils.SharedPreferencesHelper;
3433

3534
import java.util.HashMap;
@@ -47,7 +46,6 @@
4746
import androidx.appcompat.widget.AppCompatTextView;
4847
import androidx.core.view.GravityCompat;
4948
import androidx.drawerlayout.widget.DrawerLayout;
50-
import androidx.fragment.app.FragmentManager;
5149
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
5250

5351
public class MainActivity extends BaseActivity
@@ -269,6 +267,7 @@ private void initVWO(String key, final boolean showProgress, @Nullable Map<Strin
269267
**/
270268

271269
vwoConfigBuilder.setOptOut(false);
270+
vwoConfigBuilder.setEnableBenchmarking(true);
272271

273272
//vwoConfigBuilder.isChinaCDN(false);
274273

@@ -290,17 +289,26 @@ private void initVWO(String key, final boolean showProgress, @Nullable Map<Strin
290289
@Override
291290
public void onVWOLoaded() {
292291

293-
/* use the below commented code to use MEG functionality
292+
/* Use the below code to print the different sdk-init duration after enabling the benchmarking
293+
if (vwoConfigBuilder.isEnableBenchmarking()) {
294+
String initDuration = "Time taken to before api call -> " + TimeTracker.getBeforeApiInitDuration() + " ms." + " \n" +
295+
"Time taken to load URL response -> " + TimeTracker.getApiInitDuration() + " ms." + " \n" +
296+
"Time taken to after api call -> " + TimeTracker.getAfterApiInitDuration() + " ms." + " \n" +
297+
"Time taken to reach callback -> " + TimeTracker.getTotalInitDuration() + " ms." + " \n";
294298
299+
VWOLog.v("LoadTime", initDuration);
300+
}
301+
**/
302+
303+
/* use the below commented code to use MEG functionality
295304
HashMap<String, String> args = new HashMap<>();
296305
args.put("test_key", "TestKey");
297306
args.put("groupId", "4");
298307
Log.d("ReturnCampaignKey",VWO.getCampaign("[email protected]", args)+" ");
299308
Log.d("VariationNameForTestKey", VWO.getVariationNameForTestKey(VWO.getCampaign("[email protected]", args)));
300-
**/
301-
302-
/* use the below commented code to get the variationName and perform actions accordingly
309+
**/
303310

311+
/* use the below commented code to get the variationName and perform actions accordingly
304312
String testKey = "Camp test 1";
305313
String variationName = VWO.getVariationNameForTestKey(testKey);
306314
if (variationName != null && variationName == "Control") {
@@ -310,17 +318,15 @@ public void onVWOLoaded() {
310318
} else {
311319
// TODO: default case
312320
}
313-
**/
314-
315-
/* use the below code to make visitor part of all campaigns who are using this TestVariable
321+
**/
316322

323+
/* use the below code to make visitor part of all campaigns who are using this TestVariable
317324
int testVariable1 = VWO.getIntegerForKey("TestVariable1", 1);
318-
**/
319-
320-
/* use the below code to make visitor part of the specific campaign whose testKey we are using
325+
**/
321326

327+
/* use the below code to make visitor part of the specific campaign whose testKey we are using
322328
int testVariable1 = VWO.getIntegerForKey(testKey, "TestVariable1", 1);
323-
**/
329+
**/
324330

325331
if (showProgress) {
326332
progressBar.setVisibility(View.GONE);

library/src/main/java/com/vwo/mobile/Initializer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.vwo.mobile;
22

33
import android.Manifest;
4+
45
import androidx.annotation.NonNull;
56
import androidx.annotation.Nullable;
67
import androidx.annotation.RequiresPermission;
78

89
import com.vwo.mobile.events.VWOStatusListener;
10+
import com.vwo.mobile.timetracker.TimeTracker;
911
import com.vwo.mobile.utils.VWOLog;
1012

1113
/**
@@ -38,8 +40,14 @@ public class Initializer {
3840
Manifest.permission.INTERNET,
3941
Manifest.permission.ACCESS_NETWORK_STATE})
4042
public void launch(@Nullable VWOStatusListener statusListener) {
43+
44+
if (vwo.getConfig() != null && vwo.getConfig().isEnableBenchmarking()) {
45+
TimeTracker.startTracking(TimeTracker.KEY_TOTAL_INIT_DURATION);
46+
TimeTracker.startTracking(TimeTracker.KEY_BEFORE_API_INIT_DURATION);
47+
}
48+
4149
if (statusListener != null) {
42-
if(vwo.getConfig() == null) {
50+
if (vwo.getConfig() == null) {
4351
VWOConfig vwoConfig = new VWOConfig.Builder().setVWOStatusListener(statusListener).build();
4452
vwo.setConfig(vwoConfig);
4553
} else {
@@ -92,7 +100,7 @@ private void setup(@Nullable Long timeout) {
92100
} else {
93101
VWOConfig vwoConfig = this.vwo.getConfig();
94102
vwoConfig.setApiKey(apiKey);
95-
if(optOut != null) {
103+
if (optOut != null) {
96104
vwoConfig.setOptOut(optOut);
97105
}
98106
}

library/src/main/java/com/vwo/mobile/VWO.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.vwo.mobile.models.Variation;
2929
import com.vwo.mobile.network.ErrorResponse;
3030
import com.vwo.mobile.network.VWODownloader;
31+
import com.vwo.mobile.timetracker.TimeTracker;
3132
import com.vwo.mobile.utils.VWOLog;
3233
import com.vwo.mobile.utils.VWOPreference;
3334
import com.vwo.mobile.utils.VWOUrlBuilder;
@@ -671,6 +672,7 @@ public static Object getObjectForKey(@NonNull String key, @Nullable Object defau
671672
* This function will return a variation for a given key and testKey. This function will search for key in
672673
* all the currently active campaigns and match the selected campaign testKey with input testKey.
673674
* </p>
675+
*
674676
* @param testKey is the testKey of the campaign which is used along with key to fetch the variation.
675677
* @param key is the key for which variation is to be requested
676678
* @param defaultValue is the default value to be returned if key is not found in any of the campaigns.
@@ -1020,6 +1022,10 @@ private void onLoadSuccess() {
10201022
new Handler(getCurrentContext().getMainLooper()).post(new Runnable() {
10211023
@Override
10221024
public void run() {
1025+
if (vwoConfig.isEnableBenchmarking()) {
1026+
TimeTracker.updateTracking(TimeTracker.KEY_AFTER_API_INIT_DURATION);
1027+
TimeTracker.updateTracking(TimeTracker.KEY_TOTAL_INIT_DURATION);
1028+
}
10231029
vwoConfig.getStatusListener().onVWOLoaded();
10241030
}
10251031
});

library/src/main/java/com/vwo/mobile/VWOConfig.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import androidx.annotation.NonNull;
44
import androidx.annotation.Nullable;
5+
56
import android.text.TextUtils;
67

78
import com.vwo.mobile.events.VWOStatusListener;
@@ -23,6 +24,7 @@ public class VWOConfig {
2324
private String mAppKey;
2425
private String mAccountId;
2526
private boolean optOut;
27+
private boolean enableBenchmarking;
2628

2729
// Timeout in case data is fetched synchronously
2830
private Long timeout;
@@ -41,6 +43,7 @@ private VWOConfig(Builder builder) {
4143
setApiKey(builder.apiKey);
4244
}
4345
this.optOut = builder.optOut;
46+
this.enableBenchmarking = builder.enableBenchmarking;
4447
this.previewEnabled = builder.previewEnabled;
4548
this.statusListener = builder.statusListener;
4649
this.userID = builder.userID;
@@ -56,7 +59,7 @@ void setCustomSegmentationMapping(Map<String, String> customSegmentationMapping)
5659
}
5760

5861
void setApiKey(String apiKey) {
59-
if(!VWOUtils.isValidVwoAppKey(apiKey)) {
62+
if (!VWOUtils.isValidVwoAppKey(apiKey)) {
6063
VWOLog.e(VWOLog.CONFIG_LOGS, new InvalidKeyException("Invalid api key"), false, false);
6164
return;
6265
}
@@ -106,6 +109,13 @@ boolean isOptOut() {
106109
return this.optOut;
107110
}
108111

112+
/**
113+
* @return whether benchmarking is enabled or not.
114+
*/
115+
public boolean isEnableBenchmarking() {
116+
return this.enableBenchmarking;
117+
}
118+
109119
boolean isPreviewEnabled() {
110120
return this.previewEnabled;
111121
}
@@ -124,7 +134,7 @@ void setCustomSegmentKeys(Map<String, String> customSegmentKeys) {
124134
* @param value {@link String} is the value of custom segment.
125135
*/
126136
void addCustomSegment(String key, String value) {
127-
if(customSegmentationMapping == null) {
137+
if (customSegmentationMapping == null) {
128138
customSegmentationMapping = new HashMap<>();
129139
}
130140
this.customSegmentationMapping.put(key, value);
@@ -134,11 +144,15 @@ void setOptOut(boolean optOut) {
134144
this.optOut = optOut;
135145
}
136146

147+
void setEnableBenchmarking(boolean enableBenchmarking) {
148+
this.enableBenchmarking = enableBenchmarking;
149+
}
150+
137151
/**
138152
* @param customSegments add multiple custom segment key value pairs
139153
*/
140154
void addCustomSegments(Map<String, String> customSegments) {
141-
if(customSegmentationMapping == null) {
155+
if (customSegmentationMapping == null) {
142156
customSegmentationMapping = new HashMap<>();
143157
}
144158
this.customSegmentationMapping.putAll(customSegments);
@@ -165,6 +179,7 @@ public static class Builder {
165179
// This variable
166180
private Map<String, String> customSegmentationMapping;
167181
private boolean optOut;
182+
private boolean enableBenchmarking;
168183
private String apiKey = null;
169184
private boolean previewEnabled = true;
170185
private VWOStatusListener statusListener;
@@ -176,7 +191,8 @@ public static class Builder {
176191
* Generate the Configuration for the VWO SDK which can be passed to
177192
* {@link Initializer#config(VWOConfig)} during SDK's initialization.
178193
*/
179-
public Builder(){}
194+
public Builder() {
195+
}
180196

181197
@NonNull
182198
public VWOConfig build() {
@@ -198,8 +214,27 @@ public Builder setOptOut(boolean optOut) {
198214
}
199215

200216
/**
201-
* Function Set the unique ID for the user to serve same variations(subject to few conditions) across devices for users with same ID.
217+
* Function to set whether you want to enable the benchmarking of certain functionalities or not.
218+
* set {@link Boolean#TRUE} to enable the benchmarking otherwise {@link Boolean#FALSE}.
219+
* it defaults to {@link Boolean#FALSE}.
202220
*
221+
* @param enableBenchmarking set {@link Boolean#TRUE} to enable the benchmarking otherwise {@link Boolean#FALSE}.
222+
* * it defaults to {@link Boolean#FALSE}.
223+
* @return the {@link Builder} object
224+
*/
225+
@NonNull
226+
public Builder setEnableBenchmarking(boolean enableBenchmarking) {
227+
this.enableBenchmarking = enableBenchmarking;
228+
return this;
229+
}
230+
231+
public boolean isEnableBenchmarking() {
232+
return enableBenchmarking;
233+
}
234+
235+
/**
236+
* Function Set the unique ID for the user to serve same variations(subject to few conditions) across devices for users with same ID.
237+
* <p>
203238
* Note: user id is case sensitive. And this id is not stored anywhere persistently.
204239
*
205240
* @param userID is the unique user id.
@@ -241,7 +276,6 @@ public Builder disablePreview() {
241276
* @param customSegmentationMapping is the key value pair mapping for custom segments based on
242277
* which it is decided that given user will be a part of
243278
* campaign or not.
244-
*
245279
* @return the current {@link Builder} object.
246280
*/
247281
@NonNull
@@ -257,9 +291,9 @@ public Builder setCustomVariables(@NonNull Map<String, String> customSegmentatio
257291
* This function can be used to set the custom dimensions.
258292
* The custom dimensions will sent along with the track-user network call to VWO servers.
259293
*
260-
* @param customDimensionKey is the key for the custom dimension
261-
* @param customDimensionValue is the value associated with the customDimensionKey.
262-
* @return the current {@link Builder} object.
294+
* @param customDimensionKey is the key for the custom dimension
295+
* @param customDimensionValue is the value associated with the customDimensionKey.
296+
* @return the current {@link Builder} object.
263297
*/
264298
@NonNull
265299
public Builder setCustomDimension(@NonNull String customDimensionKey, @NonNull String customDimensionValue) {

library/src/main/java/com/vwo/mobile/network/NetworkRequest.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import androidx.annotation.Nullable;
66
import androidx.annotation.StringDef;
77

8+
import com.vwo.mobile.timetracker.TimeTracker;
9+
import com.vwo.mobile.timetracker.APITimeTracker;
810
import com.vwo.mobile.utils.NetworkUtils;
911
import com.vwo.mobile.utils.VWOLog;
1012

@@ -48,6 +50,7 @@ public abstract class NetworkRequest<T> implements Runnable, Comparable<NetworkR
4850
private List<Response.ErrorListener> mErrorListeners;
4951
private String requestTag;
5052
private boolean canceled;
53+
private boolean isEnableBenchmarking;
5154
private Thread currentThread;
5255
private boolean executed;
5356

@@ -80,10 +83,10 @@ public NetworkRequest(@NonNull String url,
8083
this.requestTag = requestTag;
8184
this.mResponseListeners = new ArrayList<>();
8285
this.mErrorListeners = new ArrayList<>();
83-
if(listener != null) {
86+
if (listener != null) {
8487
this.mResponseListeners.add(listener);
8588
}
86-
if(errorListener != null) {
89+
if (errorListener != null) {
8790
this.mErrorListeners.add(errorListener);
8891
}
8992
this.priority = PRIORITY_NORMAL;
@@ -161,6 +164,7 @@ private byte[] readFromStream(InputStream inputStream) throws IOException {
161164
@Override
162165
public void run() {
163166
HttpURLConnection urlConnection;
167+
// ResponseTimeTracker.trackFor(url.toString());
164168
try {
165169
urlConnection = (HttpURLConnection) url.openConnection();
166170
} catch (IOException exception) {
@@ -207,13 +211,21 @@ public void run() {
207211
throw new InterruptedException();
208212
}
209213

214+
if (isEnableBenchmarking)
215+
APITimeTracker.trackFor(url.toString());
216+
210217
// Connect
211218
urlConnection.connect();
212219

213220
if (Thread.interrupted() || isCanceled()) {
214221
throw new InterruptedException();
215222
}
216223

224+
if (isEnableBenchmarking) {
225+
APITimeTracker.updateFor(url.toString());
226+
TimeTracker.startTracking(TimeTracker.KEY_AFTER_API_INIT_DURATION);
227+
}
228+
217229
// Check if we get any response.
218230
if ((urlConnection.getResponseCode() >= 200 && urlConnection.getResponseCode() < 299) ||
219231
urlConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
@@ -275,7 +287,7 @@ public void run() {
275287
notifyErrorListeners(new ErrorResponse(exception));
276288

277289
VWOLog.e(VWOLog.NETWORK_LOGS, exception, false, true);
278-
}catch (final IOException exception) {
290+
} catch (final IOException exception) {
279291
notifyErrorListeners(new ErrorResponse(exception));
280292

281293
VWOLog.e(VWOLog.NETWORK_LOGS, exception, true, false);
@@ -293,7 +305,7 @@ public void run() {
293305

294306
private void notifyErrorListeners(ErrorResponse exception) {
295307
executed = true;
296-
for(Response.ErrorListener mErrorListener : mErrorListeners) {
308+
for (Response.ErrorListener mErrorListener : mErrorListeners) {
297309
mErrorListener.onFailure(exception);
298310
}
299311
}
@@ -328,6 +340,10 @@ public boolean isCanceled() {
328340
return canceled;
329341
}
330342

343+
public void setEnableBenchmarking(boolean isEnableBenchmarking) {
344+
this.isEnableBenchmarking = isEnableBenchmarking;
345+
}
346+
331347
public void cancel() {
332348
this.canceled = true;
333349
}

0 commit comments

Comments
 (0)