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;
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;
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;
/**
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) {
sone.getRootAlbum().addAlbum(album);
}
+ // record the duration
+ stopwatch.stop();
+ soneParsingDurationHistogram.update(stopwatch.elapsed(MICROSECONDS));
+
return sone;
}
package net.pterodactylus.sone.core
+import com.codahale.metrics.*
import com.google.common.base.Optional.*
import freenet.crypt.*
import freenet.keys.InsertableClientSSK.*
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<Sone>()
@BeforeTest
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))
+ }
+
}