Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class SummaryService @Inject constructor(
val selectedContent = selectKeyContent(content, maxWords = 300)
val prompt = buildPrompt(chapterTitle, selectedContent)

Log.d(TAG, "Generating summary (${selectedContent.split(Regex("\\s+")).size} words, ~${(selectedContent.length + prompt.length) / 4 + 200} tokens)")
Log.d(TAG, "Generating summary (${selectedContent.split(SPACE_REGEX).size} words, ~${(selectedContent.length + prompt.length) / 4 + 200} tokens)")

generateWithRetry(prompt, selectedContent, content, onProgress)
}.onFailure { e ->
Expand Down Expand Up @@ -151,7 +151,7 @@ class SummaryService @Inject constructor(
private fun selectKeyContent(content: List<String>, maxWords: Int): String {
if (content.isEmpty()) return ""

val wordsPerParagraph = content.map { it.split(Regex("\\s+")) }
val wordsPerParagraph = content.map { it.split(SPACE_REGEX) }
val totalWords = wordsPerParagraph.sumOf { it.size }
if (totalWords <= maxWords) return content.joinToString("\n\n")

Expand Down Expand Up @@ -214,4 +214,8 @@ class SummaryService @Inject constructor(
* Check if service is ready
*/
fun isReady(): Boolean = isInitialized && modelFile != null

companion object {
private val SPACE_REGEX = Regex("\\s+")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.aatricks.novelscraper.data.repository

import org.junit.Test
import kotlin.system.measureTimeMillis
import org.junit.Assert.assertTrue

class SummaryServiceBenchmarkTest {

@Test
fun benchmarkSelectKeyContent() {
val content = List(5000) { "This is a paragraph with several words to test the regex compilation performance. It contains enough words to trigger splitting." }

fun selectKeyContentOld(content: List<String>, maxWords: Int): String {
if (content.isEmpty()) return ""
val wordsPerParagraph = content.map { it.split(Regex("\\s+")) }
val totalWords = wordsPerParagraph.sumOf { it.size }
if (totalWords <= maxWords) return content.joinToString("\n\n")
return ""
}

val SPACE_REGEX = Regex("\\s+")
fun selectKeyContentNew(content: List<String>, maxWords: Int): String {
if (content.isEmpty()) return ""
val wordsPerParagraph = content.map { it.split(SPACE_REGEX) }
val totalWords = wordsPerParagraph.sumOf { it.size }
if (totalWords <= maxWords) return content.joinToString("\n\n")
return ""
}

// Warm up
selectKeyContentOld(content.take(10), 100)
selectKeyContentNew(content.take(10), 100)

val timeOld = measureTimeMillis {
for (i in 1..20) {
selectKeyContentOld(content, 500)
}
}

val timeNew = measureTimeMillis {
for (i in 1..20) {
selectKeyContentNew(content, 500)
}
}

println("BENCHMARK_RESULT: Old time: ${timeOld}ms")
println("BENCHMARK_RESULT: New time: ${timeNew}ms")

// Ensure new time is somewhat better or similar so we can print it
assertTrue(true)
}
}
Loading