Skip to content

Commit

Permalink
Change from file benchmarks to BAOS/IS benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
sandwwraith committed Jul 19, 2021
1 parent dcc4734 commit 7807f6d
Showing 1 changed file with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kotlinx.benchmarks.model.MacroTwitterFeed
import kotlinx.benchmarks.model.MicroTwitterFeed
import kotlinx.serialization.json.*
import org.openjdk.jmh.annotations.*
import java.io.*
import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.TimeUnit
Expand All @@ -21,63 +22,60 @@ import kotlin.io.path.outputStream
@Fork(2)
open class TwitterFeedStreamBenchmark {
val resource = TwitterFeedBenchmark::class.java.getResource("/twitter_macro.json")!!
val bytes = resource.readBytes()
private val twitter = Json.decodeFromString(MacroTwitterFeed.serializer(), resource.readText())

private val jsonIgnoreUnknwn = Json { ignoreUnknownKeys = true }
private val objectMapper: ObjectMapper =
jacksonObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)


private var file: Path? = null

@Setup
fun init() {
file = Files.createTempFile("json_benchmark", "tmp")
}

@TearDown
fun tearDown() {
file?.deleteIfExists()
}
private val inputStream: InputStream
get() = ByteArrayInputStream(bytes)
private val outputStream: OutputStream
get() = ByteArrayOutputStream()

@Benchmark
fun encodeTwitterWriteText() {
file?.outputStream()?.use {
fun encodeTwitterWriteText(): OutputStream {
return outputStream.use {
it.bufferedWriter().write(Json.encodeToString(MacroTwitterFeed.serializer(), twitter))
it
}
}

@Benchmark
fun encodeTwitterWriteStream() {
file?.outputStream()?.use {
fun encodeTwitterWriteStream(): OutputStream {
return outputStream.use {
Json.encodeToStream(MacroTwitterFeed.serializer(), twitter, it)
it
}
}

@Benchmark
fun encodeTwitterJacksonStream() {
file?.outputStream()?.use {
fun encodeTwitterJacksonStream(): OutputStream {
return outputStream.use {
objectMapper.writeValue(it, twitter)
it
}
}

@Benchmark
fun decodeMicroTwitterReadText(): MicroTwitterFeed {
return resource.openStream().use {
return inputStream.use {
jsonIgnoreUnknwn.decodeFromString(MicroTwitterFeed.serializer(), it.bufferedReader().readText())
}
}

@Benchmark
fun decodeMicroTwitterStream(): MicroTwitterFeed {
return resource.openStream().use {
return inputStream.use {
jsonIgnoreUnknwn.decodeFromStream(MicroTwitterFeed.serializer(), it.buffered())
}
}

@Benchmark
fun decodeMicroTwitterJacksonStream(): MicroTwitterFeed {
return resource.openStream().use {
return inputStream.use {
objectMapper.readValue(it, MicroTwitterFeed::class.java)
}
}
Expand Down

0 comments on commit 7807f6d

Please sign in to comment.