From b801125ff5712a0252d321a542da22e8b8d3f5ef Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 6 May 2025 19:37:44 -0400 Subject: [PATCH] Builds a speed logger to better understand data usage --- .../com/vitorpamplona/amethyst/Amethyst.kt | 4 +- .../service/relayClient/RelaySpeedLogger.kt | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelaySpeedLogger.kt diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt index 47da7cfc61..9e00ebe058 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt @@ -41,7 +41,7 @@ import com.vitorpamplona.amethyst.service.ots.OtsBlockHeightCache import com.vitorpamplona.amethyst.service.playback.diskCache.VideoCache import com.vitorpamplona.amethyst.service.playback.diskCache.VideoCacheFactory import com.vitorpamplona.amethyst.service.relayClient.CacheClientConnector -import com.vitorpamplona.amethyst.service.relayClient.RelayLogger +import com.vitorpamplona.amethyst.service.relayClient.RelaySpeedLogger import com.vitorpamplona.amethyst.service.relayClient.authCommand.model.AuthCoordinator import com.vitorpamplona.amethyst.service.relayClient.notifyCommand.model.NotifyCoordinator import com.vitorpamplona.amethyst.service.relayClient.reqCommand.RelaySubscriptionsCoordinator @@ -110,7 +110,7 @@ class Amethyst : Application() { // Authenticates with relays. val authCoordinator = AuthCoordinator(client) - val logger = if (isDebug) RelayLogger(client) else null + val logger = if (isDebug) RelaySpeedLogger(client) else null // Organizes cache clearing val trimmingService = MemoryTrimmingService(cache) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelaySpeedLogger.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelaySpeedLogger.kt new file mode 100644 index 0000000000..f34380adac --- /dev/null +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/RelaySpeedLogger.kt @@ -0,0 +1,72 @@ +/** + * Copyright (c) 2024 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.relayClient + +import android.util.Log +import com.vitorpamplona.ammolite.relays.NostrClient +import com.vitorpamplona.ammolite.relays.Relay +import com.vitorpamplona.quartz.nip01Core.core.Event +import com.vitorpamplona.quartz.utils.TimeUtils + +/** + * Listens to NostrClient's onNotify messages from the relay + */ +class RelaySpeedLogger( + val client: NostrClient, +) { + companion object { + val TAG = RelaySpeedLogger::class.java.simpleName + } + + var time = TimeUtils.now() / 10 + var onEventCount: Long = 0L + + private val clientListener = + object : NostrClient.Listener { + /** A new message was received */ + override fun onEvent( + event: Event, + subscriptionId: String, + relay: Relay, + afterEOSE: Boolean, + ) { + val now = TimeUtils.now() / 10 + if (time == now) { + onEventCount++ + } else { + Log.d(TAG, "EPS: ${onEventCount / 10} ") + time = now + onEventCount = 0 + } + } + } + + init { + Log.d(TAG, "Init, Subscribe") + client.subscribe(clientListener) + } + + fun destroy() { + // makes sure to run + Log.d(TAG, "Destroy, Unsubscribe") + client.unsubscribe(clientListener) + } +}