Skip to content

Commit

Permalink
Merge pull request #214 from Automattic/performance-improvements
Browse files Browse the repository at this point in the history
Move decorating events with mutable device info to a background thread
  • Loading branch information
wzieba authored May 7, 2024
2 parents 39a82f2 + 5e56d75 commit e2e7a0d
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.os.Handler;
import android.util.Log;

import androidx.annotation.NonNull;

import com.automattic.android.tracks.Exceptions.EventDetailsException;
import com.automattic.android.tracks.Exceptions.EventNameException;
import com.automattic.android.tracks.datasets.EventTable;
Expand Down Expand Up @@ -113,6 +115,7 @@ public void run() {
// TODO: Remove older events and insert the new ones - See https://github.com/Automattic/Automattic-Tracks-Android/pull/35#discussion_r172458380
if (EventTable.getEventsCount(mContext) < DEFAULT_EVENTS_QUEUE_MAX_SIZE) {
for (Event currentEvent : shadowCopyEventList) {
decorateEventWithMutableDeviceInfo(currentEvent);
EventTable.insertEvent(mContext, currentEvent);
}
}
Expand Down Expand Up @@ -319,6 +322,13 @@ private void reEnqueueEventsAndSetError(NetworkRequestObject request) {
networkThread.start();
}

private void decorateEventWithMutableDeviceInfo(@NonNull final Event currentEvent) {
JSONObject deviceInfo = deviceInformation.getMutableDeviceInfo();
if (deviceInfo != null && deviceInfo.length() > 0) {
currentEvent.setDeviceInfo(deviceInfo);
}
}

private final class NetworkRequestObject {
JSONObject requestObj;
List<Event> src;
Expand Down Expand Up @@ -428,11 +438,6 @@ public void track(String eventName, JSONObject customProps, String user, NosaraU
return;
}

JSONObject deviceInfo = deviceInformation.getMutableDeviceInfo();
if (deviceInfo != null && deviceInfo.length() > 0) {
event.setDeviceInfo(deviceInfo);
}

if (mUserProperties != null && mUserProperties.length() > 0) {
event.setUserProperties(mUserProperties);
}
Expand Down
1 change: 1 addition & 0 deletions benchmark/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
37 changes: 37 additions & 0 deletions benchmark/benchmark-proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-dontobfuscate

-ignorewarnings

-keepattributes *Annotation*

-dontnote junit.framework.**
-dontnote junit.runner.**

-dontwarn androidx.test.**
-dontwarn org.junit.**
-dontwarn org.hamcrest.**
-dontwarn com.squareup.javawriter.JavaWriter

-keepclasseswithmembers @org.junit.runner.RunWith public class *
59 changes: 59 additions & 0 deletions benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
plugins {
id("com.android.library")
id("androidx.benchmark")
id("org.jetbrains.kotlin.android")
}

repositories {
google()
mavenCentral()
}

android {
namespace = "com.automattic.tracks.benchmark"
compileSdk = 34

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = "1.8"
}

defaultConfig {
minSdk = 24

testInstrumentationRunner = "androidx.benchmark.junit4.AndroidBenchmarkRunner"
}

testBuildType = "release"
buildTypes {
debug {
// Since isDebuggable can"t be modified by gradle for library modules,
// it must be done in a manifest - see src/androidTest/AndroidManifest.xml
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"benchmark-proguard-rules.pro"
)
}
release {
isDefault = true
}
}
}

dependencies {
androidTestImplementation("androidx.test:runner:1.5.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.benchmark:benchmark-junit4:1.2.4")

// Add your dependencies here. Note that you cannot benchmark code
// in an app module this way - you will need to move any code you
// want to benchmark to a library module:
// https://developer.android.com/studio/projects/android-library#Convert
androidTestImplementation(project(":AutomatticTracks"))
}
15 changes: 15 additions & 0 deletions benchmark/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<!--
Important: disable debugging for accurate performance results
In a com.android.library project, this flag must be disabled from this
manifest, as it is not possible to override this flag from Gradle.
-->
<application
android:debuggable="false"
tools:ignore="HardcodedDebugMode"
tools:replace="android:debuggable" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.automattic.tracks.benchmark

import android.app.Activity
import androidx.benchmark.junit4.BenchmarkRule
import androidx.benchmark.junit4.measureRepeated
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.automattic.android.tracks.TracksClient
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.reflect.Field

@RunWith(AndroidJUnit4::class)
class TrackingMethodsBenchmark {
@get:Rule
val activityRule = ActivityScenarioRule(SampleActivity::class.java)

@get:Rule
val benchmarkRule = BenchmarkRule()

private lateinit var tracksClient: TracksClient

@Before
fun setUp() {
activityRule.scenario.onActivity {
tracksClient = TracksClient.getClient(it)
}

val field: Field = TracksClient::class.java.getDeclaredField("mTracksRestEndpointURL")
field.isAccessible = true
field.set(tracksClient, "https://not-a-real-url.test")
}

@Test
fun basicTrackMethod() {
benchmarkRule.measureRepeated {
tracksClient.track("test1_test2_final", "user", TracksClient.NosaraUserType.ANON)
}
}

class SampleActivity : Activity()
}
18 changes: 18 additions & 0 deletions benchmark/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />

<application android:usesCleartextTraffic="true">
<activity
android:name=".TrackingMethodsBenchmark$SampleActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pluginManagement {
id 'com.android.application' version gradle.ext.agpVersion
id 'org.jetbrains.kotlin.android' version gradle.ext.kotlinVersion
id 'com.automattic.android.publish-to-s3' version gradle.ext.automatticPublishToS3Version
id 'androidx.benchmark' version '1.2.4'
}
repositories {
maven {
Expand All @@ -26,3 +27,4 @@ include ':AutomatticTracks'
include ':sampletracksapp'
include ':experimentation'
include ':crashlogging'
include ':benchmark'

0 comments on commit e2e7a0d

Please sign in to comment.