2 * Sone - HistogramRendererTest.kt - Copyright © 2019–2020 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.template
20 import com.codahale.metrics.*
21 import net.pterodactylus.sone.freenet.*
22 import net.pterodactylus.sone.test.OverrideLocale
23 import net.pterodactylus.util.template.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
27 import org.jsoup.nodes.*
30 import java.util.Locale.ENGLISH
33 * Unit test for [HistogramRenderer].
35 class HistogramRendererTest {
38 val overrideLocale = OverrideLocale(ENGLISH)
40 private val translation = object : Translation {
41 override val currentLocale = Locale.ENGLISH
42 override fun translate(key: String) = "Metric Name".takeIf { key == "Page.Metrics.TestHistogram.Title" } ?: ""
44 private val metricRenderer = HistogramRenderer()
45 private val templateContext = TemplateContext().apply {
46 addFilter("html", HtmlFilter())
47 addFilter("duration", DurationFormatFilter())
48 addFilter("l10n", L10nFilter(translation))
52 fun `histogram is rendered as table row`() {
53 createAndVerifyTableRow {
54 assertThat(it.nodeName(), equalTo("tr"))
59 fun `histogram has eleven columns`() {
60 createAndVerifyTableRow {
61 assertThat(it.getElementsByTag("td"), hasSize(11))
66 fun `first column contains translated metric name`() {
67 createAndVerifyTableRow(mapOf("name" to "test.histogram")) {
68 assertThat(it.getElementsByTag("td")[0].text(), equalTo("Metric Name"))
73 fun `second column is numeric`() {
74 verifyColumnIsNumeric(1)
78 fun `second column contains count`() {
79 createAndVerifyTableRow {
80 assertThat(it.getElementsByTag("td")[1].text(), equalTo("2001"))
85 fun `third column is numeric`() {
86 verifyColumnIsNumeric(2)
90 fun `third column contains min value`() {
91 createAndVerifyTableRow {
92 assertThat(it.getElementsByTag("td")[2].text(), equalTo("2.0ms"))
97 fun `fourth column is numeric`() {
98 verifyColumnIsNumeric(3)
102 fun `fourth column contains max value`() {
103 createAndVerifyTableRow {
104 assertThat(it.getElementsByTag("td")[3].text(), equalTo("998.0ms"))
109 fun `fifth column is numeric`() {
110 verifyColumnIsNumeric(4)
114 fun `fifth column contains mean value`() {
115 createAndVerifyTableRow {
116 assertThat(it.getElementsByTag("td")[4].text(), equalTo("492.7ms"))
121 fun `sixth column is numeric`() {
122 verifyColumnIsNumeric(5)
126 fun `sixth column contains median value`() {
127 createAndVerifyTableRow {
128 assertThat(it.getElementsByTag("td")[5].text(), equalTo("483.6ms"))
133 fun `seventh column is numeric`() {
134 verifyColumnIsNumeric(6)
138 fun `seventh column contains 75th percentile`() {
139 createAndVerifyTableRow {
140 assertThat(it.getElementsByTag("td")[6].text(), equalTo("740.9ms"))
145 fun `eighth column is numeric`() {
146 verifyColumnIsNumeric(7)
150 fun `eighth column contains 95th percentile`() {
151 createAndVerifyTableRow {
152 assertThat(it.getElementsByTag("td")[7].text(), equalTo("940.9ms"))
157 fun `ninth column is numeric`() {
158 verifyColumnIsNumeric(8)
162 fun `ninth column contains 98th percentile`() {
163 createAndVerifyTableRow {
164 assertThat(it.getElementsByTag("td")[8].text(), equalTo("975.6ms"))
169 fun `tenth column is numeric`() {
170 verifyColumnIsNumeric(9)
174 fun `tenth column contains 99th percentile`() {
175 createAndVerifyTableRow {
176 assertThat(it.getElementsByTag("td")[9].text(), equalTo("991.6ms"))
181 fun `eleventh column is numeric`() {
182 verifyColumnIsNumeric(10)
186 fun `eleventh column contains 99,9th percentile`() {
187 createAndVerifyTableRow {
188 assertThat(it.getElementsByTag("td")[10].text(), equalTo("998.0ms"))
192 private fun createAndVerifyTableRow(parameters: Map<String, Any?>? = null, verify: (Element) -> Unit) =
193 metricRenderer.format(templateContext, histogram, parameters)
194 .let { "<table id='t'>$it</table>" }
195 .let(Jsoup::parseBodyFragment)
196 .getElementById("t").child(0).child(0)
199 private fun verifyColumnIsNumeric(column: Int) =
200 createAndVerifyTableRow {
201 assertThat(it.getElementsByTag("td")[column].classNames(), hasItem("numeric"))
206 private val random = Random(1)
207 private val histogram = MetricRegistry().histogram("test.histogram") { Histogram(SlidingWindowReservoir(1028)) }.apply {
208 (0..2000).map { random.nextInt(1_000_000) }.forEach(this::update)