-
Notifications
You must be signed in to change notification settings - Fork 624
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JVM integration with InputStream and OutputStream (#1569)
Co-authored-by: Vsevolod Tolstopyatov <[email protected]>
- Loading branch information
1 parent
0e75d25
commit c0c60a6
Showing
49 changed files
with
1,221 additions
and
500 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
benchmark/src/jmh/kotlin/kotlinx/benchmarks/json/TwitterFeedStreamBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package kotlinx.benchmarks.json | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
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 | ||
import kotlin.io.path.deleteIfExists | ||
import kotlin.io.path.outputStream | ||
|
||
@Warmup(iterations = 7, time = 1) | ||
@Measurement(iterations = 7, time = 1) | ||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
@State(Scope.Benchmark) | ||
@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 val inputStream: InputStream | ||
get() = ByteArrayInputStream(bytes) | ||
private val outputStream: OutputStream | ||
get() = ByteArrayOutputStream() | ||
|
||
@Benchmark | ||
fun encodeTwitterWriteText(): OutputStream { | ||
return outputStream.use { | ||
it.bufferedWriter().write(Json.encodeToString(MacroTwitterFeed.serializer(), twitter)) | ||
it | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun encodeTwitterWriteStream(): OutputStream { | ||
return outputStream.use { | ||
Json.encodeToStream(MacroTwitterFeed.serializer(), twitter, it) | ||
it | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun encodeTwitterJacksonStream(): OutputStream { | ||
return outputStream.use { | ||
objectMapper.writeValue(it, twitter) | ||
it | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun decodeMicroTwitterReadText(): MicroTwitterFeed { | ||
return inputStream.use { | ||
jsonIgnoreUnknwn.decodeFromString(MicroTwitterFeed.serializer(), it.bufferedReader().readText()) | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun decodeMicroTwitterStream(): MicroTwitterFeed { | ||
return inputStream.use { | ||
jsonIgnoreUnknwn.decodeFromStream(MicroTwitterFeed.serializer(), it.buffered()) | ||
} | ||
} | ||
|
||
@Benchmark | ||
fun decodeMicroTwitterJacksonStream(): MicroTwitterFeed { | ||
return inputStream.use { | ||
objectMapper.readValue(it, MicroTwitterFeed::class.java) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.