-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement saving benchmark results to JSON for browser #5327
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
Merged
Nikita Lipsky (pjBooms)
merged 3 commits into
master
from
pjBooms/benchamarks-save-for-web-via-local-server
Jun 3, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
6 changes: 6 additions & 0 deletions
6
benchmarks/multiplatform/benchmarks/src/appleMain/kotlin/BenchmarksSave.apple.kt
This file contains hidden or 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,6 @@ | ||
| /* | ||
| * Copyright 2020-2025 JetBrains s.r.o. and respective authors and developers. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
| */ | ||
|
|
||
| actual fun saveBenchmarkStats(name: String, stats: BenchmarkStats) = saveBenchmarkStatsOnDisk(name, stats) |
This file contains hidden or 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 hidden or 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 hidden or 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
6 changes: 6 additions & 0 deletions
6
benchmarks/multiplatform/benchmarks/src/desktopMain/kotlin/BenchmarksSave.desktop.kt
This file contains hidden or 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,6 @@ | ||
| /* | ||
| * Copyright 2020-2025 JetBrains s.r.o. and respective authors and developers. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
| */ | ||
|
|
||
| actual fun saveBenchmarkStats(name: String, stats: BenchmarkStats) = saveBenchmarkStatsOnDisk(name, stats) |
92 changes: 92 additions & 0 deletions
92
benchmarks/multiplatform/benchmarks/src/desktopMain/kotlin/BenchmarksSaveServer.kt
This file contains hidden or 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,92 @@ | ||
| /* | ||
| * Copyright 2020-2025 JetBrains s.r.o. and respective authors and developers. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
| */ | ||
|
|
||
| import io.ktor.http.* | ||
| import io.ktor.serialization.kotlinx.json.* | ||
| import io.ktor.server.application.* | ||
| import io.ktor.server.engine.* | ||
| import io.ktor.server.netty.* | ||
| import io.ktor.server.plugins.contentnegotiation.* | ||
| import io.ktor.server.plugins.cors.routing.CORS | ||
| import io.ktor.server.request.* | ||
| import io.ktor.server.response.* | ||
| import io.ktor.server.routing.* | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import kotlinx.io.files.Path | ||
| import kotlinx.io.files.SystemFileSystem | ||
| import kotlinx.serialization.Serializable | ||
|
|
||
| /** | ||
| * Data class for receiving benchmark results from client. | ||
| */ | ||
| @Serializable | ||
| data class BenchmarkResultFromClient( | ||
| val name: String, | ||
| val stats: String // JSON string of BenchmarkStats | ||
| ) | ||
|
|
||
| /** | ||
| * Starts a Ktor server to receive benchmark results from browsers | ||
| * and save them to disk in the same format as the direct disk saving mechanism. | ||
| */ | ||
| object BenchmarksSaveServer { | ||
| private var server: EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration>? = null | ||
|
|
||
| fun start(port: Int = BENCHMARK_SERVER_PORT) { | ||
| if (server != null) { | ||
| println("Benchmark server is already running") | ||
| return | ||
| } | ||
|
|
||
| server = embeddedServer(Netty, port = port) { | ||
| install(ContentNegotiation) { | ||
| json() | ||
| } | ||
| install(CORS) { | ||
| allowMethod(HttpMethod.Get) | ||
| allowMethod(HttpMethod.Post) | ||
| allowHeader(HttpHeaders.ContentType) | ||
| anyHost() | ||
| } | ||
| routing { | ||
| post("/benchmark") { | ||
| val result = call.receive<BenchmarkResultFromClient>() | ||
| if (result.name.isEmpty()) { | ||
| println("Stopping server! Received empty name from client") | ||
| call.respond(HttpStatusCode.OK, "Server stopped.") | ||
| stop() | ||
| return@post | ||
| } | ||
| println("Received benchmark result for: ${result.name}") | ||
|
|
||
| withContext(Dispatchers.IO) { | ||
| if (Config.saveStatsToJSON) { | ||
| saveJson(result.name, result.stats) | ||
| } | ||
|
|
||
| if (Config.saveStatsToCSV) { | ||
| // TODO: for CSV, we would need to convert JSON to the values | ||
| println("CSV results are not yet supported for the browser.") | ||
| } | ||
| } | ||
|
|
||
| call.respond(HttpStatusCode.OK, "Benchmark result saved") | ||
| } | ||
|
|
||
| get("/") { | ||
| call.respondText("Benchmark server is running", ContentType.Text.Plain) | ||
| } | ||
| } | ||
| }.start(wait = true) | ||
| } | ||
|
|
||
| fun stop() { | ||
| server?.stop(1000, 2000) | ||
| server = null | ||
| println("Benchmark server stopped") | ||
| System.exit(0) | ||
| } | ||
| } | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.