-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metrics performance improvements (#157)
* Metrics speedup * Optimize attributes conversions * Add end of line to Tuple * Attributes getter and remove old not valid tests * Optimize attributes * Remove debugging information and format file * Rollback MainActivity * Reformat file. Bring previous metrics logic * Report data builder tests * Move application cache to singleton * SkipNul, remove additional unique event and make application cache thread safe * Application metadata cache test * Update ReportDataBuilder.java * Pull request updates: private constructor, move singleton method, format files, remove tuple * Simplify BacktraceAttributes business logic --------- Co-authored-by: Konrad Dysput <[email protected]>
- Loading branch information
1 parent
93221d6
commit bafe650
Showing
19 changed files
with
481 additions
and
210 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
backtrace-library/src/androidTest/java/backtraceio/library/ApplicationMetadataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package backtraceio.library; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import androidx.test.platform.app.InstrumentationRegistry; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import backtraceio.library.common.ApplicationMetadataCache; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class ApplicationMetadataTest { | ||
private Context context; | ||
|
||
@Before | ||
public void setUp() { | ||
context = InstrumentationRegistry.getInstrumentation().getContext(); | ||
// prepare instance | ||
ApplicationMetadataCache.getInstance(context); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyRetrieveApplicationName() { | ||
ApplicationMetadataCache cache = ApplicationMetadataCache.getInstance(context); | ||
assertEquals(cache.getApplicationName(), context.getOpPackageName()); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyRetrieveApplicationPackageName() { | ||
ApplicationMetadataCache cache = ApplicationMetadataCache.getInstance(context); | ||
assertEquals(cache.getPackageName(), context.getOpPackageName()); | ||
} | ||
|
||
@Test | ||
public void shouldCorrectlyRetrieveApplicationVersion() throws PackageManager.NameNotFoundException { | ||
ApplicationMetadataCache cache = ApplicationMetadataCache.getInstance(context); | ||
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getOpPackageName(), 0); | ||
assertEquals(cache.getApplicationVersion(), String.valueOf(packageInfo.versionCode)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
backtrace-library/src/main/java/backtraceio/library/common/ApplicationMetadataCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package backtraceio.library.common; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
|
||
import backtraceio.library.logger.BacktraceLogger; | ||
|
||
public class ApplicationMetadataCache { | ||
|
||
private static final transient String LOG_TAG = ApplicationMetadataCache.class.getSimpleName(); | ||
|
||
private static volatile ApplicationMetadataCache instance; | ||
|
||
/** | ||
* Cached application name | ||
*/ | ||
private String applicationName; | ||
|
||
/** | ||
* Cached application version | ||
*/ | ||
private String applicationVersion; | ||
|
||
/** | ||
* Cached package name | ||
*/ | ||
private String packageName; | ||
|
||
private final Context context; | ||
|
||
private ApplicationMetadataCache(Context context) { | ||
this.context = context; | ||
} | ||
|
||
/** | ||
* Returns current application cache. This instance is a singleton since we can only operate | ||
* in a single application scope. | ||
* | ||
* @param context Application context | ||
* @return Application metadata cache | ||
*/ | ||
public static ApplicationMetadataCache getInstance(Context context) { | ||
if (instance == null) { | ||
synchronized (ApplicationMetadataCache.class) { | ||
if (instance == null) { | ||
instance = new ApplicationMetadataCache(context); | ||
} | ||
} | ||
} | ||
return instance; | ||
} | ||
|
||
/** | ||
* Retrieves application name from context. The name will be cached over checks | ||
* | ||
* @return application name | ||
*/ | ||
public String getApplicationName() { | ||
if (!BacktraceStringHelper.isNullOrEmpty(applicationName)) { | ||
return applicationName; | ||
} | ||
|
||
applicationName = context.getApplicationInfo().loadLabel(context.getPackageManager()).toString(); | ||
return applicationName; | ||
} | ||
|
||
/** | ||
* Retrieves application version from the context. If the version name is not defined, the version code will be used instead. | ||
* | ||
* @return current application version. | ||
*/ | ||
public String getApplicationVersion() { | ||
if (!BacktraceStringHelper.isNullOrEmpty(applicationVersion)) { | ||
return applicationVersion; | ||
} | ||
try { | ||
PackageInfo info = context.getPackageManager() | ||
.getPackageInfo(context.getPackageName(), 0); | ||
applicationVersion = BacktraceStringHelper.isNullOrEmpty(info.versionName) ? String.valueOf(info.versionCode) : info.versionName; | ||
|
||
return applicationVersion; | ||
} catch (PackageManager.NameNotFoundException e) { | ||
BacktraceLogger.e(LOG_TAG, "Could not resolve application version", e); | ||
return ""; | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves package name from the context. | ||
* | ||
* @return current package name. | ||
*/ | ||
public String getPackageName() { | ||
if (!BacktraceStringHelper.isNullOrEmpty(packageName)) { | ||
return packageName; | ||
} | ||
packageName = context.getApplicationContext().getPackageName(); | ||
|
||
return packageName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ace-library/src/main/java/backtraceio/library/models/attributes/ReportDataAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package backtraceio.library.models.attributes; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ReportDataAttributes { | ||
private final Map<String, String> reportAttributes = new HashMap<>(); | ||
|
||
private final Map<String, Object> reportAnnotations = new HashMap<>(); | ||
|
||
|
||
public void addAnnotation(String key, Object value) { | ||
reportAnnotations.put(key, value); | ||
} | ||
|
||
public void addAttribute(String key, String value) { | ||
reportAttributes.put(key, value); | ||
} | ||
|
||
public Map<String, String> getAttributes() { | ||
return reportAttributes; | ||
} | ||
|
||
public Map<String, Object> getAnnotations() { | ||
return reportAnnotations; | ||
} | ||
} |
Oops, something went wrong.