Skip to content

Commit 0b6ceb9

Browse files
committed
Update settings.gradle.kts to include the benchmark module: add :benchmark to the project structure for enhanced performance testing capabilities.
1 parent 865eb51 commit 0b6ceb9

File tree

5 files changed

+192
-1
lines changed

5 files changed

+192
-1
lines changed

benchmark/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
plugins {
2+
kotlin("jvm") version "2.0.21"
3+
id("me.champeau.jmh") version "0.7.2"
4+
}
5+
6+
jmh {
7+
warmupIterations.set(2)
8+
iterations.set(5)
9+
fork.set(1)
10+
timeOnIteration.set("1s")
11+
benchmarkMode.set(listOf("avgt"))
12+
timeUnit.set("ms")
13+
resultFormat.set("JSON")
14+
}
15+
16+
kotlin {
17+
jvmToolchain(17)
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dev.vyp.stringcare.benchmark
2+
3+
import org.openjdk.jmh.annotations.Benchmark
4+
import org.openjdk.jmh.annotations.Param
5+
import org.openjdk.jmh.annotations.Scope
6+
import org.openjdk.jmh.annotations.Setup
7+
import org.openjdk.jmh.annotations.State
8+
import java.util.Base64
9+
10+
@State(Scope.Benchmark)
11+
open class EndToEndBenchmark {
12+
@Param("100", "1000")
13+
var sampleSize: Int = 100
14+
15+
private lateinit var sample: List<String>
16+
private val encoder = Base64.getEncoder()
17+
private val decoder = Base64.getDecoder()
18+
19+
@Setup
20+
fun setup() {
21+
sample =
22+
List(sampleSize) { index ->
23+
"String value #$index"
24+
}
25+
}
26+
27+
@Benchmark
28+
fun obfuscateAndRevealBatch(): Int {
29+
val obfuscated = sample.map { encoder.encodeToString(it.toByteArray()) }
30+
val revealed = obfuscated.map { String(decoder.decode(it)) }
31+
return revealed.count { it.startsWith("String value #") }
32+
}
33+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package dev.vyp.stringcare.benchmark
2+
3+
import org.openjdk.jmh.annotations.Benchmark
4+
import org.openjdk.jmh.annotations.Level
5+
import org.openjdk.jmh.annotations.Param
6+
import org.openjdk.jmh.annotations.Scope
7+
import org.openjdk.jmh.annotations.Setup
8+
import org.openjdk.jmh.annotations.State
9+
import java.io.File
10+
import java.nio.file.Files
11+
import kotlin.io.path.extension
12+
import kotlin.io.path.name
13+
import kotlin.io.path.pathString
14+
15+
@State(Scope.Benchmark)
16+
open class FileScanningBenchmark {
17+
@Param("100", "1000", "10000")
18+
var totalFiles: Int = 100
19+
20+
private lateinit var projectRoot: File
21+
22+
@Setup(Level.Trial)
23+
fun setup() {
24+
projectRoot = Files.createTempDirectory("scan-benchmark-").toFile()
25+
createFixtureTree(projectRoot, totalFiles)
26+
}
27+
28+
@Benchmark
29+
fun locateResources(): Int =
30+
projectRoot
31+
.toPath()
32+
.toFile()
33+
.walkTopDown()
34+
.count { file ->
35+
file.isFile && file.name == "strings.xml" &&
36+
!file.toPath().pathString.contains("/build/") &&
37+
!file.toPath().pathString.contains("/.gradle/")
38+
}
39+
40+
@Benchmark
41+
fun locateAssets(): Int =
42+
projectRoot
43+
.toPath()
44+
.toFile()
45+
.walkTopDown()
46+
.count { file ->
47+
file.isFile && file.toPath().name == "payload.bin" && file.toPath().extension == "bin"
48+
}
49+
50+
private fun createFixtureTree(root: File, files: Int) {
51+
val module = File(root, "app/src/main")
52+
val resources = File(module, "res")
53+
val assets = File(module, "assets")
54+
resources.mkdirs()
55+
assets.mkdirs()
56+
57+
val perType = files / 2
58+
repeat(perType) { idx ->
59+
val valuesDir = File(resources, "values-$idx")
60+
valuesDir.mkdirs()
61+
File(valuesDir, "strings.xml").writeText(
62+
"""
63+
<resources>
64+
<string name="v_$idx" hidden="true">value_$idx</string>
65+
</resources>
66+
""".trimIndent(),
67+
)
68+
}
69+
70+
repeat(files - perType) { idx ->
71+
val nested = File(assets, "folder-$idx")
72+
nested.mkdirs()
73+
File(nested, "payload.bin").writeBytes(byteArrayOf(1, 2, 3, 4))
74+
}
75+
}
76+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dev.vyp.stringcare.benchmark
2+
3+
import org.openjdk.jmh.annotations.Benchmark
4+
import org.openjdk.jmh.annotations.Level
5+
import org.openjdk.jmh.annotations.Param
6+
import org.openjdk.jmh.annotations.Scope
7+
import org.openjdk.jmh.annotations.Setup
8+
import org.openjdk.jmh.annotations.State
9+
import java.io.File
10+
import java.nio.file.Files
11+
import javax.xml.parsers.SAXParserFactory
12+
import kotlin.random.Random
13+
import org.xml.sax.Attributes
14+
import org.xml.sax.helpers.DefaultHandler
15+
16+
@State(Scope.Benchmark)
17+
open class XmlParsingBenchmark {
18+
@Param("10", "100", "1000")
19+
var stringCount: Int = 10
20+
21+
private lateinit var xmlFile: File
22+
23+
@Setup(Level.Trial)
24+
fun setup() {
25+
xmlFile = Files.createTempFile("strings-", ".xml").toFile()
26+
xmlFile.writeText(buildXml(stringCount))
27+
}
28+
29+
@Benchmark
30+
fun parseStringsXml(): Int {
31+
var matched = 0
32+
val parser = SAXParserFactory.newInstance().newSAXParser()
33+
parser.parse(
34+
xmlFile,
35+
object : DefaultHandler() {
36+
override fun startElement(
37+
uri: String?,
38+
localName: String?,
39+
qName: String?,
40+
attributes: Attributes?,
41+
) {
42+
if (qName == "string" && attributes?.getValue("hidden") == "true") {
43+
matched += 1
44+
}
45+
}
46+
},
47+
)
48+
return matched
49+
}
50+
51+
private fun buildXml(items: Int): String {
52+
val payload =
53+
buildString {
54+
append("<resources>\n")
55+
repeat(items) { index ->
56+
val value = Random.nextInt(1_000_000, 9_999_999)
57+
append(""" <string name="k_$index" hidden="true">value_$value</string>""")
58+
append('\n')
59+
}
60+
append("</resources>\n")
61+
}
62+
return payload
63+
}
64+
}

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ dependencyResolutionManagement {
2828
}
2929

3030
rootProject.name = "stringcare-android"
31-
include(":app", ":library")
31+
include(":app", ":library", ":benchmark")

0 commit comments

Comments
 (0)