-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
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,39 @@ | ||
import kotlinx.benchmark.gradle.JvmBenchmarkTarget | ||
import kotlinx.benchmark.gradle.benchmark | ||
import org.jetbrains.kotlin.allopen.gradle.AllOpenExtension | ||
|
||
plugins { | ||
java | ||
kotlin("jvm") | ||
kotlin("plugin.allopen") | ||
id("org.jetbrains.kotlinx.benchmark") | ||
} | ||
|
||
sourceSets.all { | ||
java.setSrcDirs(listOf("$name/src")) | ||
resources.setSrcDirs(listOf("$name/resources")) | ||
} | ||
|
||
configure<AllOpenExtension> { | ||
annotation("org.openjdk.jmh.annotations.State") | ||
} | ||
|
||
dependencies { | ||
implementation(project(":ktoken")) | ||
implementation(libs.kotlinx.benchmark) | ||
} | ||
|
||
benchmark { | ||
configurations { | ||
named("main") { | ||
warmups = 1 | ||
iterations = 5 | ||
} | ||
} | ||
targets { | ||
register("main") { | ||
this as JvmBenchmarkTarget | ||
jmhVersion = "1.37" | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
benchmark/main/src/com/aallam/ktoken/benchmark/EncodeBenchmark.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,29 @@ | ||
package com.aallam.ktoken.benchmark | ||
|
||
import com.aallam.ktoken.Tokenizer | ||
import kotlinx.coroutines.runBlocking | ||
import org.openjdk.jmh.annotations.* | ||
import java.net.URL | ||
import java.nio.charset.StandardCharsets | ||
import java.util.* | ||
|
||
@State(Scope.Benchmark) | ||
@Fork(2) | ||
@BenchmarkMode(Mode.SingleShotTime) | ||
class EncodeBenchmark { | ||
private lateinit var text: String | ||
private lateinit var tokenizer: Tokenizer | ||
|
||
@Setup | ||
fun setUp() = runBlocking { | ||
val url = URL("https://unicode.org/udhr/assemblies/full_all.txt") | ||
Scanner(url.openStream(), StandardCharsets.UTF_8.name()) | ||
.use { scanner -> text = scanner.useDelimiter("\\A").next() } | ||
tokenizer = Tokenizer.encodingForModel("gpt-4") | ||
} | ||
|
||
@Benchmark | ||
fun encode(): Int { | ||
return tokenizer.encode(text).size | ||
} | ||
} |
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