From: David ‘Bombe’ Roden Date: Fri, 26 Jul 2019 22:27:37 +0000 (+0200) Subject: ✨ Record Sone parsing durations X-Git-Tag: v81^2~178 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b55dc075f9cd34fb32baa44bbbc54a89d05e80b9;p=Sone.git ✨ Record Sone parsing durations --- 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)) + } + }