From bf8eeae4a5fb7bc9341b753d91611fec901e3e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matu=CC=81s=CC=8C=20Tomlein?= Date: Wed, 29 Nov 2023 18:06:29 +0100 Subject: [PATCH] Change default buffer option to single (close #649) --- .../snowplowtrackerdemojava/Demo.java | 1 - .../snowplowdemokotlin/Demo.kt | 2 +- .../snowplow/tracker/EmitterTest.kt | 22 ++++++------- .../core/emitter/EmitterDefaults.kt | 2 +- .../configuration/EmitterConfiguration.kt | 9 +++--- .../snowplow/emitter/BufferOption.kt | 31 ++++++++++++++----- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/snowplow-demo-java/src/main/java/com/snowplowanalytics/snowplowtrackerdemojava/Demo.java b/snowplow-demo-java/src/main/java/com/snowplowanalytics/snowplowtrackerdemojava/Demo.java index 3ea06bc70..968d8d8f3 100644 --- a/snowplow-demo-java/src/main/java/com/snowplowanalytics/snowplowtrackerdemojava/Demo.java +++ b/snowplow-demo-java/src/main/java/com/snowplowanalytics/snowplowtrackerdemojava/Demo.java @@ -295,7 +295,6 @@ private boolean setupWithLocalConfig() { NetworkConfiguration networkConfiguration = new NetworkConfiguration(uri, method); EmitterConfiguration emitterConfiguration = new EmitterConfiguration() .requestCallback(getRequestCallback()) - .bufferOption(BufferOption.DefaultGroup) .threadPoolSize(20) .emitRange(500) .byteLimitPost(52000); diff --git a/snowplow-demo-kotlin/src/main/java/com/snowplowanalytics/snowplowdemokotlin/Demo.kt b/snowplow-demo-kotlin/src/main/java/com/snowplowanalytics/snowplowdemokotlin/Demo.kt index 9acb2dce4..5a28d29c9 100644 --- a/snowplow-demo-kotlin/src/main/java/com/snowplowanalytics/snowplowdemokotlin/Demo.kt +++ b/snowplow-demo-kotlin/src/main/java/com/snowplowanalytics/snowplowdemokotlin/Demo.kt @@ -260,7 +260,7 @@ class Demo : Activity(), LoggerDelegate { val networkConfiguration = NetworkConfiguration(uri, method) val emitterConfiguration = EmitterConfiguration() .requestCallback(requestCallback) - .bufferOption(BufferOption.DefaultGroup) + .bufferOption(BufferOption.SmallGroup) .threadPoolSize(20) .emitRange(500) .byteLimitPost(52000) diff --git a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/EmitterTest.kt b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/EmitterTest.kt index 9eb1d156d..3ab0c8c7c 100755 --- a/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/EmitterTest.kt +++ b/snowplow-tracker/src/androidTest/java/com/snowplowanalytics/snowplow/tracker/EmitterTest.kt @@ -45,12 +45,12 @@ class EmitterTest { var builder = { emitter: Emitter -> emitter.bufferOption = BufferOption.Single } var emitter = Emitter(context, "com.acme", builder) Assert.assertEquals(BufferOption.Single, emitter.bufferOption) - builder = { emitter1: Emitter -> emitter1.bufferOption = BufferOption.DefaultGroup } + builder = { emitter1: Emitter -> emitter1.bufferOption = BufferOption.SmallGroup } emitter = Emitter(context, "com.acme", builder) - Assert.assertEquals(BufferOption.DefaultGroup, emitter.bufferOption) - builder = { emitter2: Emitter -> emitter2.bufferOption = BufferOption.HeavyGroup } + Assert.assertEquals(BufferOption.SmallGroup, emitter.bufferOption) + builder = { emitter2: Emitter -> emitter2.bufferOption = BufferOption.LargeGroup } emitter = Emitter(context, "com.acme", builder) - Assert.assertEquals(BufferOption.HeavyGroup, emitter.bufferOption) + Assert.assertEquals(BufferOption.LargeGroup, emitter.bufferOption) } @Test @@ -76,7 +76,7 @@ class EmitterTest { var emitter = Emitter(context, uri, builder) Assert.assertEquals("http://$uri/i", emitter.emitterUri) builder = { emitter1: Emitter -> - emitter1.bufferOption = BufferOption.DefaultGroup + emitter1.bufferOption = BufferOption.SmallGroup emitter1.httpMethod = HttpMethod.POST emitter1.requestSecurity = Protocol.HTTP } @@ -86,14 +86,14 @@ class EmitterTest { emitter.emitterUri ) builder = { emitter2: Emitter -> - emitter2.bufferOption = BufferOption.DefaultGroup + emitter2.bufferOption = BufferOption.SmallGroup emitter2.httpMethod = HttpMethod.GET emitter2.requestSecurity = Protocol.HTTPS } emitter = Emitter(context, uri, builder) Assert.assertEquals("https://$uri/i", emitter.emitterUri) builder = { emitter3: Emitter -> - emitter3.bufferOption = BufferOption.DefaultGroup + emitter3.bufferOption = BufferOption.SmallGroup emitter3.httpMethod = HttpMethod.POST emitter3.requestSecurity = Protocol.HTTPS } @@ -173,8 +173,8 @@ class EmitterTest { Assert.assertEquals("https://$uri/i", emitter.emitterUri) emitter.emitterUri = "com.acme" Assert.assertEquals("https://com.acme/i", emitter.emitterUri) - emitter.bufferOption = BufferOption.HeavyGroup - Assert.assertEquals(BufferOption.HeavyGroup, emitter.bufferOption) + emitter.bufferOption = BufferOption.LargeGroup + Assert.assertEquals(BufferOption.LargeGroup, emitter.bufferOption) emitter.flush() emitter.flush() Thread.sleep(500) @@ -189,8 +189,8 @@ class EmitterTest { "https://com.foo/com.snowplowanalytics.snowplow/tp2", emitter.emitterUri ) - emitter.bufferOption = BufferOption.DefaultGroup - Assert.assertEquals(BufferOption.HeavyGroup, emitter.bufferOption) + emitter.bufferOption = BufferOption.SmallGroup + Assert.assertEquals(BufferOption.LargeGroup, emitter.bufferOption) emitter.shutdown() builder = { emitter1: Emitter -> emitter1.bufferOption = BufferOption.Single diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/emitter/EmitterDefaults.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/emitter/EmitterDefaults.kt index 4d073ab21..a31ca5ed4 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/core/emitter/EmitterDefaults.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/core/emitter/EmitterDefaults.kt @@ -21,7 +21,7 @@ import java.util.concurrent.TimeUnit object EmitterDefaults { var httpMethod = HttpMethod.POST - var bufferOption = BufferOption.DefaultGroup + var bufferOption = BufferOption.Single var httpProtocol = Protocol.HTTPS var tlsVersions: EnumSet = EnumSet.of(TLSVersion.TLSv1_2) var emitRange: Int = 150 diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/EmitterConfiguration.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/EmitterConfiguration.kt index 79e8c64ff..48a07f872 100644 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/EmitterConfiguration.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/configuration/EmitterConfiguration.kt @@ -23,7 +23,7 @@ import org.json.JSONObject * Configure how the tracker should send the events to the collector. * * Default values: - * - bufferOption: [BufferOption.DefaultGroup] + * - bufferOption: [BufferOption.Single] * - serverAnonymisation: false * - emitRange: 150 - maximum number of events to process at a time * - threadPoolSize: 15 @@ -45,7 +45,7 @@ open class EmitterConfiguration() : Configuration, EmitterConfigurationInterface private var _bufferOption: BufferOption? = null override var bufferOption: BufferOption - get() = _bufferOption ?: sourceConfig?.bufferOption ?: BufferOption.DefaultGroup + get() = _bufferOption ?: sourceConfig?.bufferOption ?: EmitterDefaults.bufferOption set(value) { _bufferOption = value } private var _emitRange: Int? = null @@ -96,8 +96,7 @@ open class EmitterConfiguration() : Configuration, EmitterConfigurationInterface // Builders /** - * How many events to send in each request. By default, this is set to [BufferOption.DefaultGroup], - * a maximum of 10 events per request. + * How many events to send in each request. By default, this is set to [BufferOption.Single]. */ fun bufferOption(bufferOption: BufferOption): EmitterConfiguration { this.bufferOption = bufferOption @@ -205,7 +204,7 @@ open class EmitterConfiguration() : Configuration, EmitterConfigurationInterface */ constructor(jsonObject: JSONObject) : this() { if (jsonObject.has("bufferOption")) { - _bufferOption = BufferOption.valueOf(jsonObject.getString("bufferOption")) + _bufferOption = BufferOption.fromString(jsonObject.getString("bufferOption")) } if (jsonObject.has("emitRange")) { _emitRange = jsonObject.getInt("emitRange") } if (jsonObject.has("threadPoolSize")) { _threadPoolSize = jsonObject.getInt("threadPoolSize") } diff --git a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/emitter/BufferOption.kt b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/emitter/BufferOption.kt index 4305c16b2..58be82ee6 100755 --- a/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/emitter/BufferOption.kt +++ b/snowplow-tracker/src/main/java/com/snowplowanalytics/snowplow/emitter/BufferOption.kt @@ -17,20 +17,35 @@ package com.snowplowanalytics.snowplow.emitter */ enum class BufferOption(val code: Int) { /** - * Sends both GET and POST requests with only a single event. Can cause a spike in - * network traffic if used in correlation with a large amount of events. + * Sends both GET and POST requests with only a single event. + * This is the default setting. + * Can cause a spike in network traffic if used in correlation with a large amount of events. */ Single(1), /** - * Sends POST requests in groups of 10 events. This is the default amount of events to - * package into a POST. All GET requests will still emit one at a time. + * Sends POST requests in groups of 10 events. + * All GET requests will still emit one at a time. */ - DefaultGroup(10), + SmallGroup(10), /** - * Sends POST requests in groups of 25 events. Useful for situations where many events - * need to be sent. All GET requests will still emit one at a time. + * Sends POST requests in groups of 25 events. + * Useful for situations where many events need to be sent. + * All GET requests will still emit one at a time. */ - HeavyGroup(25); + LargeGroup(25); + + companion object { + fun fromString(string: String): BufferOption? { + return when (string) { + "Single" -> Single + "SmallGroup" -> SmallGroup + "DefaultGroup" -> SmallGroup + "LargeGroup" -> LargeGroup + "HeavyGroup" -> LargeGroup + else -> null + } + } + } }