Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.3.1 Improve tests, fix several bugs. (1/2) #38

Merged
merged 4 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ name: Check

on:
workflow_dispatch:
pull_request:

jobs:
check:
Expand All @@ -21,4 +20,4 @@ jobs:
- name: Grant Gradle Wrapper Permission
run: chmod +x gradlew
- name: Check
run: ./gradlew check
run: ./gradlew core:check
5 changes: 3 additions & 2 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Publish

on: workflow_dispatch
on:
workflow_dispatch:

env:
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.MAVEN_USERNAME }}
Expand All @@ -22,4 +23,4 @@ jobs:
- name: Grant Gradle Wrapper Permission
run: chmod +x gradlew
- name: Publish
run: ./gradlew publish --stacktrace
run: ./gradlew core:publish --stacktrace
44 changes: 44 additions & 0 deletions benchmark/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm")
kotlin("plugin.serialization")
kotlin("plugin.allopen")
kotlin("kapt")

id("me.champeau.jmh")
}

dependencies {
jmh("org.openjdk.jmh:jmh-core:1.36")
kaptJmh("org.openjdk.jmh:jmh-generator-annprocess:1.36")

// tomlkt
jmh(project(":core"))
// toml4j
jmh("com.moandjiezana.toml:toml4j:0.7.2")
// ktoml
jmh("com.akuleshov7:ktoml-core:0.5.0")
// jackson
jmh("com.fasterxml.jackson.dataformat:jackson-dataformat-toml:2.15.1")
jmh("com.fasterxml.jackson.module:jackson-module-kotlin:2.15.1")
jmh("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.1")
// ...

}

jmh {
includes.set(listOf("test.Benchmark"))
}

allOpen {
annotation("org.openjdk.jmh.annotations.Measurement")
}

tasks {
withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
82 changes: 82 additions & 0 deletions benchmark/src/jmh/kotlin/test/Benchmark.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package test

import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import kotlinx.serialization.decodeFromString
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.BenchmarkMode
import org.openjdk.jmh.annotations.Fork
import org.openjdk.jmh.annotations.Measurement
import org.openjdk.jmh.annotations.Mode
import org.openjdk.jmh.annotations.OutputTimeUnit
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.Threads
import org.openjdk.jmh.annotations.Warmup
import java.util.concurrent.TimeUnit

@State(Scope.Thread)
object TomlObjects {
val tomlkt = net.peanuuutz.tomlkt.Toml
val toml4j = com.moandjiezana.toml.Toml()
val ktoml = com.akuleshov7.ktoml.Toml
val jackson = com.fasterxml.jackson.dataformat.toml.TomlMapper().apply {
registerKotlinModule()
registerModule(JavaTimeModule())
}
}

/*
Benchmark Mode Cnt Score Error Units
Benchmark.jacksonWithType avgt 10 7696.731 ± 112.218 ns/op
Benchmark.jacksonWithClass avgt 10 8090.255 ± 420.727 ns/op
Benchmark.tomlktWithoutSerializer avgt 10 10265.799 ± 72.684 ns/op
Benchmark.tomlktWithSerializer avgt 10 10901.727 ± 1002.641 ns/op
Benchmark.toml4j avgt 10 20390.178 ± 251.729 ns/op
Benchmark.ktomlWithSerializer avgt 10 46123.130 ± 1389.639 ns/op
Benchmark.ktomlWithoutSerializer avgt 10 46555.647 ± 1047.836 ns/op
*/
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 4)
@Measurement(iterations = 5)
@Threads(4)
@Fork(2)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
class Benchmark {
@Benchmark
fun tomlktWithoutSerializer() {
TomlObjects.tomlkt.decodeFromString<Config>(SampleConfig)
}

@Benchmark
fun tomlktWithSerializer() {
TomlObjects.tomlkt.decodeFromString(Config.serializer(), SampleConfig)
}

@Benchmark
fun toml4j() {
TomlObjects.toml4j.read(SampleConfig).to(Config::class.java)
}

@Benchmark
fun ktomlWithoutSerializer() {
TomlObjects.ktoml.decodeFromString<Config>(SampleConfig)
}

@Benchmark
fun ktomlWithSerializer() {
TomlObjects.ktoml.decodeFromString(Config.serializer(), SampleConfig)
}

@Benchmark
fun jacksonWithClass() {
TomlObjects.jackson.readValue(SampleConfig, Config::class.java)
}

@Benchmark
fun jacksonWithType() {
TomlObjects.jackson.readValue<Config>(SampleConfig)
}
}
87 changes: 87 additions & 0 deletions benchmark/src/jmh/kotlin/test/Objects.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package test

import kotlinx.datetime.serializers.InstantIso8601Serializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import net.peanuuutz.tomlkt.TomlDecoder
import net.peanuuutz.tomlkt.TomlOffsetDateTimeSerializer
import org.intellij.lang.annotations.Language

@Serializable
data class Config(
val title: String,
val owner: Owner,
val database: Database,
// val servers: Map<String, Server>
)

@Serializable
data class Owner(
val name: String,
val birthday: @Serializable(OffsetDateTimeSerializer::class) Any
)

@Serializable
data class Database(
val enabled: Boolean,
val ports: List<Short>,
val temperature: Temperature
)

@Serializable
data class Temperature(
val cpu: Float,
val case: Float
)

@Serializable
data class Server(
val ip: String,
val role: String
)

@Language("toml")
const val SampleConfig: String = """
# This is a TOML document

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
birthday = 1979-05-27T07:32:00-08:00

[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
temperature = { cpu = 79.5, case = 72.0 }

# [servers]

# [servers.alpha]
# ip = "10.0.0.1"
# role = "frontend"

# [servers.beta]
# ip = "10.0.0.2"
# role = "backend"
"""

object OffsetDateTimeSerializer : KSerializer<Any> {
override val descriptor: SerialDescriptor
get() = InstantIso8601Serializer.descriptor

override fun serialize(encoder: Encoder, value: Any) {
TODO("Not yet implemented")
}

override fun deserialize(decoder: Decoder): Any {
return if (decoder is TomlDecoder) {
TomlOffsetDateTimeSerializer().deserialize(decoder)
} else {
InstantIso8601Serializer.deserialize(decoder)
}
}
}
Loading