Skip to content

Commit c17754d

Browse files
committed
Make the testing more generic. Also add a testing of merely xml parsing
the schema documents.
1 parent 3ee71fb commit c17754d

File tree

1 file changed

+122
-46
lines changed

1 file changed

+122
-46
lines changed

xmlschema/src/jvmTest/kotlin/io/github/pdvrieze/formats/xmlschema/test/TestXSTestSuite.kt

+122-46
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import io.github.pdvrieze.formats.xmlschema.datatypes.serialization.facets.*
2727
import io.github.pdvrieze.formats.xmlschema.resolved.SchemaVersion
2828
import io.github.pdvrieze.formats.xmlschema.resolved.SimpleResolver
2929
import io.github.pdvrieze.formats.xmlschema.test.TestXSTestSuite.NON_TESTED.*
30+
import kotlinx.datetime.Instant
3031
import kotlinx.serialization.KSerializer
3132
import nl.adaptivity.xmlutil.*
3233
import nl.adaptivity.xmlutil.XMLConstants.XSD_NS_URI
34+
import nl.adaptivity.xmlutil.core.KtXmlReader
3335
import nl.adaptivity.xmlutil.core.impl.newReader
3436
import nl.adaptivity.xmlutil.jdk.StAXStreamingFactory
3537
import nl.adaptivity.xmlutil.serialization.XML
@@ -48,65 +50,121 @@ import kotlin.test.assertTrue
4850

4951
class TestXSTestSuite {
5052

51-
var xml: XML = XML {}
53+
var xml: XML = XML {
54+
xmlStreaming.setFactory(xmlStreaming.genericFactory)
55+
recommended_0_87_0()
56+
}
5257

5358
@Test
54-
fun testParseGeneric() {
59+
fun testParseGenericSpeed() {
60+
val urls = testXmlSchemaUrls()
61+
var dummy1: Any?
62+
var dummy2: Any?
63+
var dummy3: Any?
64+
var dummy4: Any?
65+
measure("Parse xml ${urls.size} schema documents") {
66+
for ((_, url) in urls) {
67+
url.openStream().use { instr ->
68+
KtXmlReader(instr).use { r ->
69+
for (e in r) {
70+
when (e) {
71+
EventType.START_DOCUMENT -> {
72+
dummy1 = r.version
73+
dummy2 = r.relaxed
74+
dummy3 = r.standalone
75+
}
76+
EventType.END_ELEMENT,
77+
EventType.START_ELEMENT -> {
78+
for (i in 0 until r.attributeCount) {
79+
dummy1 = r.localName
80+
dummy2 = r.namespaceURI
81+
dummy3 = r.prefix
82+
dummy4 = r.getAttributeValue(i)
83+
}
84+
dummy1 = r.localName
85+
dummy2 = r.namespaceURI
86+
dummy3 = r.prefix
87+
}
88+
EventType.TEXT,
89+
EventType.CDSECT,
90+
EventType.ENTITY_REF,
91+
EventType.IGNORABLE_WHITESPACE,
92+
EventType.PROCESSING_INSTRUCTION,
93+
EventType.COMMENT -> {
94+
dummy1 = r.text
95+
}
96+
EventType.DOCDECL -> { dummy1 = r.text}
97+
EventType.END_DOCUMENT -> {}
98+
EventType.ATTRIBUTE -> error("unexpected attribute")
99+
}
100+
}
101+
}
102+
}
103+
104+
}
105+
}
106+
}
107+
108+
@Test
109+
fun testDeserializeGenericSpeed() {
55110
xml = XML {
56-
isUnchecked = true
111+
isUnchecked = false
57112
defaultPolicy {
58113
autoPolymorphic = true
59114
throwOnRepeatedElement = true
60115
}
61116
}
62117
xmlStreaming.setFactory(xmlStreaming.genericFactory)
63-
testParseSpeed()
118+
testDeserializeSpeed()
64119
xmlStreaming.setFactory(null)
65120
}
66121

67122
@Test
68-
fun testParseStax() {
123+
fun testDeserializeStaxSpeed() {
69124
xml = XML {
70125
defaultPolicy {
71126
autoPolymorphic = true
72127
throwOnRepeatedElement = true
73128
}
74129
}
75130
xmlStreaming.setFactory(StAXStreamingFactory())
76-
testParseSpeed()
131+
testDeserializeSpeed()
77132
xmlStreaming.setFactory(null)
78133
}
79134

80-
fun testParseSpeed() {
81-
val suiteURL: URL = javaClass.getResource("/xsts/suite.xml")
82-
83-
val override = javaClass.getResource("/override.xml").withXmlReader {
84-
val compact = xml.decodeFromReader<CompactOverride>(it)
85-
OTSSuite(compact)
135+
inline fun measure(name:String, rounds: Int = 20, warmups: Int = 1, action: MeasureInfo.() -> Unit): Long {
136+
val initTime = System.currentTimeMillis()
137+
var startTime = initTime
138+
val iterCount = rounds+warmups
139+
for (i in 0 until iterCount) {
140+
if (i==warmups+1) {
141+
startTime = System.currentTimeMillis()
142+
}
143+
MeasureInfo(i-warmups, rounds, warmups).action()
86144
}
145+
val endTime = System.currentTimeMillis()
146+
println ("Init: ${Instant.fromEpochMilliseconds(initTime)}")
147+
println ("Start: ${Instant.fromEpochMilliseconds(startTime)}")
148+
println ("End: ${Instant.fromEpochMilliseconds(endTime)}")
149+
if (rounds==0) {
150+
val duration = (endTime - initTime)/warmups
151+
println("$name: Duration time: $duration ms")
152+
return duration
153+
} else {
154+
val duration = (endTime - startTime)/rounds
155+
val warmupExtra = (startTime - initTime - duration)
156+
println("$name: Duration time × $rounds): $duration ms (+${warmupExtra} ms)")
157+
return duration
158+
}
159+
}
87160

88-
val nodes = mutableListOf<DynamicNode>()
89-
val schemaUrls: List<Pair<URL, URL>> = suiteURL.withXmlReader { xmlReader ->
90-
val suite = xml.decodeFromReader<TSTestSuite>(xmlReader)
91-
suite.testSetRefs
92-
// .filter { arrayOf("sunMeta/").any { m -> it.href.contains(m) } }
93-
.flatMap { setRef ->
94-
val setBaseUrl: URL = javaClass.getResource("/xsts/${setRef.href}")
95-
val testSet = override.applyTo(setBaseUrl.withXmlReader { r -> xml.decodeFromReader<TSTestSet>(r) })
96-
97-
val folderName = setRef.href.substring(0, setRef.href.indexOf('/')).removeSuffix("Meta")
98-
99-
val tsName = "$folderName - ${testSet.name}"
161+
fun testDeserializeSpeed() {
100162

101-
testSet.testGroups.flatMap { gr ->
102-
gr.schemaTest?.takeIf { it.expected.any { it.validity.parsable } }?.schemaDocuments?.mapNotNull { sd ->
103-
(setBaseUrl to setBaseUrl.resolve(sd.href))
104-
} ?: emptyList()
105-
}
106-
}
107-
}//.filter { it.second.toString().contains("wildcards") }
163+
val schemaUrls: List<Pair<URL, URL>> =
164+
testXmlSchemaUrls()//.filter { it.second.toString().contains("wildcards") }
108165

109166
val xml = this.xml.copy {
167+
isUnchecked = true
110168
defaultPolicy {
111169
autoPolymorphic = true
112170
throwOnRepeatedElement = true
@@ -116,13 +174,9 @@ class TestXSTestSuite {
116174
}
117175
assertTrue(xml.config.isUnchecked)
118176

119-
val initTime = System.currentTimeMillis()
120-
var startTime = initTime
121-
val iterCount = 10
122-
123-
for (i in 0 .. iterCount) {
124-
if (i == 1) startTime = System.currentTimeMillis()
125-
177+
print("Iterating: ")
178+
val duration = measure("Parsing and deserializing ${schemaUrls.size} schema documents") {
179+
if(round<0) print("*") else if (round+1<rounds) print("×") else println("×")
126180
for ((setBaseUri, uri) in schemaUrls) {
127181
val resolver = SimpleResolver(xml, setBaseUri)
128182
try {
@@ -132,17 +186,37 @@ class TestXSTestSuite {
132186
}
133187
}
134188
}
135-
val endTime = System.currentTimeMillis()
136-
val duration = (endTime-startTime)/iterCount.coerceAtLeast(1)
189+
println()
190+
assertTrue(duration<10000, "Duration expected less than 10 seconds" )
191+
}
137192

138-
if (iterCount==0) {
139-
println("Duration time (${schemaUrls.size} documents): $duration ms")
140-
} else {
141-
val initialTime = startTime-initTime
142-
println("Duration time (${schemaUrls.size} documents × $iterCount): $duration ms (+${initialTime-duration} ms)")
193+
private fun testXmlSchemaUrls(): List<Pair<URL, URL>> {
194+
val suiteURL: URL = javaClass.getResource("/xsts/suite.xml")
143195

196+
val override = javaClass.getResource("/override.xml").withXmlReader {
197+
val compact = xml.decodeFromReader<CompactOverride>(it)
198+
OTSSuite(compact)
199+
}
200+
201+
return suiteURL.withXmlReader { xmlReader ->
202+
val suite = xml.decodeFromReader<TSTestSuite>(xmlReader)
203+
suite.testSetRefs
204+
// .filter { arrayOf("sunMeta/").any { m -> it.href.contains(m) } }
205+
.flatMap { setRef ->
206+
val setBaseUrl: URL = javaClass.getResource("/xsts/${setRef.href}")
207+
val testSet = override.applyTo(setBaseUrl.withXmlReader { r -> xml.decodeFromReader<TSTestSet>(r) })
208+
209+
val folderName = setRef.href.substring(0, setRef.href.indexOf('/')).removeSuffix("Meta")
210+
211+
val tsName = "$folderName - ${testSet.name}"
212+
213+
testSet.testGroups.flatMap { gr ->
214+
gr.schemaTest?.takeIf { it.expected.any { it.validity.parsable } }?.schemaDocuments?.mapNotNull { sd ->
215+
(setBaseUrl to setBaseUrl.resolve(sd.href))
216+
} ?: emptyList()
217+
}
218+
}
144219
}
145-
assertTrue(duration<10000, "Duration expected less than 10 seconds" )
146220
}
147221

148222
@DisplayName("Test suites: suite.xml")
@@ -715,3 +789,5 @@ inline fun <R> URL.withXmlReader(body: (XmlReader) -> R): R {
715789
fun URL.resolve(path: String): URL {
716790
return URL(this, path)
717791
}
792+
793+
data class MeasureInfo(val round: Int, val rounds: Int, val warmups: Int)

0 commit comments

Comments
 (0)