💄 Generalize rendering of histograms
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / pages / MetricsPageTest.kt
1 /**
2  * Sone - MetricsPageTest.kt - Copyright Â© 2019 David â€˜Bombe’ Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web.pages
19
20 import com.codahale.metrics.*
21 import net.pterodactylus.sone.test.*
22 import net.pterodactylus.sone.web.*
23 import net.pterodactylus.sone.web.page.*
24 import org.hamcrest.MatcherAssert.*
25 import org.hamcrest.Matchers.*
26 import kotlin.test.*
27
28 class MetricsPageTest : WebPageTest() {
29
30         private val metricRegistry = MetricRegistry()
31         override val page by lazy { MetricsPage(webInterface, loaders, templateRenderer, metricRegistry) }
32
33         @Test
34         fun `page returns correct path`() {
35                 assertThat(page.path, equalTo("metrics.html"))
36         }
37
38         @Test
39         fun `page does not require login`() {
40                 assertThat(page.requiresLogin(), equalTo(false))
41         }
42
43         @Test
44         fun `page returns correct title`() {
45                 addTranslation("Page.Metrics.Title", "metrics page title")
46                 assertThat(page.getPageTitle(soneRequest), equalTo("metrics page title"))
47         }
48
49         @Test
50         fun `page can be created by dependency injection`() {
51                 assertThat(baseInjector.getInstance<MetricsPage>(), notNullValue())
52         }
53
54         @Test
55         fun `page is annotated with the correct menuname`() {
56                 assertThat(page.menuName, equalTo("Metrics"))
57         }
58
59         @Test
60         fun `page is annotated with correct template path`() {
61                 assertThat(page.templatePath, equalTo("/templates/metrics.html"))
62         }
63
64         @Test
65         fun `metrics page auto-converts histogram name`() {
66                 createHistogram("sone.random.duration")
67                 page.handleRequest(soneRequest, templateContext)
68                 verifyHistogram("soneRandomDuration")
69         }
70
71         @Test
72         @Suppress("UNCHECKED_CAST")
73         fun `metrics page stores histogram keys in template`() {
74                 createHistogram("sone.random.duration2")
75                 createHistogram("sone.random.duration1")
76                 page.handleRequest(soneRequest, templateContext)
77                 assertThat(templateContext["histogramKeys"] as Iterable<String>, contains("soneRandomDuration1", "soneRandomDuration2"))
78         }
79
80         @Test
81         fun `metrics page stores i18n names for histogram keys`() {
82                 createHistogram("sone.random.duration1")
83                 page.handleRequest(soneRequest, templateContext)
84                 assertThat(templateContext["soneRandomDuration1I18n"] as String, equalTo("SoneRandomDuration1"))
85         }
86
87         @Test
88         fun `metrics page delivers correct histogram size`() {
89                 val histogram = metricRegistry.histogram("sone.parsing.duration")
90                 (0..4000).forEach(histogram::update)
91                 page.handleRequest(soneRequest, templateContext)
92                 assertThat(templateContext["soneParsingDurationCount"] as Long, equalTo(4001L))
93         }
94
95         private fun verifyHistogram(name: String) {
96                 assertThat(templateContext["${name}Count"] as Long, equalTo(5L))
97                 assertThat(templateContext["${name}Min"] as Long, equalTo(1L))
98                 assertThat(templateContext["${name}Max"] as Long, equalTo(10L))
99                 assertThat(templateContext["${name}Median"] as Double, equalTo(8.0))
100                 assertThat(templateContext["${name}Percentile75"] as Double, equalTo(9.0))
101                 assertThat(templateContext["${name}Percentile95"] as Double, equalTo(10.0))
102                 assertThat(templateContext["${name}Percentile98"] as Double, equalTo(10.0))
103                 assertThat(templateContext["${name}Percentile99"] as Double, equalTo(10.0))
104                 assertThat(templateContext["${name}Percentile999"] as Double, equalTo(10.0))
105         }
106
107         private fun createHistogram(name: String) = metricRegistry.histogram(name).run {
108                 update(10)
109                 update(9)
110                 update(1)
111                 update(1)
112                 update(8)
113         }
114
115 }