From b55dc075f9cd34fb32baa44bbbc54a89d05e80b9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 27 Jul 2019 00:27:37 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Record=20Sone=20parsing=20durations?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- src/main/java/net/pterodactylus/sone/core/SoneParser.java | 13 ++++++++++++- .../kotlin/net/pterodactylus/sone/core/SoneParserTest.kt | 12 +++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 01c056e..b12866a 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -1,5 +1,6 @@ package net.pterodactylus.sone.core; +import static java.util.concurrent.TimeUnit.*; import static java.util.logging.Logger.getLogger; import static net.pterodactylus.sone.utils.NumberParsers.parseInt; import static net.pterodactylus.sone.utils.NumberParsers.parseLong; @@ -11,6 +12,7 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -32,6 +34,8 @@ import net.pterodactylus.sone.database.SoneBuilder; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; +import com.codahale.metrics.*; +import com.google.common.base.*; import org.w3c.dom.Document; /** @@ -42,15 +46,18 @@ public class SoneParser { private static final Logger logger = getLogger(SoneParser.class.getName()); private static final int MAX_PROTOCOL_VERSION = 0; private final Database database; + private final Histogram soneParsingDurationHistogram; @Inject - public SoneParser(Database database) { + public SoneParser(Database database, MetricRegistry metricRegistry) { this.database = database; + this.soneParsingDurationHistogram = metricRegistry.histogram("sone.parsing.duration"); } public Sone parseSone(Sone originalSone, InputStream soneInputStream) throws SoneException { /* TODO - impose a size limit? */ + Stopwatch stopwatch = Stopwatch.createStarted(); Document document; /* XML parsing is not thread-safe. */ synchronized (this) { @@ -336,6 +343,10 @@ public class SoneParser { sone.getRootAlbum().addAlbum(album); } + // record the duration + stopwatch.stop(); + soneParsingDurationHistogram.update(stopwatch.elapsed(MICROSECONDS)); + return sone; } diff --git a/src/test/kotlin/net/pterodactylus/sone/core/SoneParserTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/SoneParserTest.kt index 3a7010a..0648d1f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/SoneParserTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/SoneParserTest.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.core +import com.codahale.metrics.* import com.google.common.base.Optional.* import freenet.crypt.* import freenet.keys.InsertableClientSSK.* @@ -21,7 +22,8 @@ import kotlin.test.* class SoneParserTest { private val database = MemoryDatabase(Configuration(MapConfigurationBackend())) - private val soneParser = SoneParser(database) + private val metricRegistry = MetricRegistry() + private val soneParser = SoneParser(database, metricRegistry) private val sone = mock() @BeforeTest @@ -395,4 +397,12 @@ class SoneParserTest { assertThat(sone.profile.avatar, equalTo("image-id")) } + @Test + fun `successful parsing adds histogram entry`() { + val inputStream = javaClass.getResourceAsStream("sone-parser-without-images.xml") + assertThat(soneParser.parseSone(sone, inputStream), notNullValue()) + val histogram = metricRegistry.histogram("sone.parsing.duration") + assertThat(histogram.count, equalTo(1L)) + } + } -- 2.7.4