Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vmishenev committed Nov 4, 2022
1 parent a99eda3 commit 0e6b7f0
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 24 deletions.
@@ -1,5 +1,5 @@
@file:Suppress("LocalVariableName", "UnstableApiUsage")

apply(from = "../template.settings.gradle.kts")
rootProject.name = "strees-0-it"
rootProject.name = "it-sequential-tasks-execution-stress"

Expand Up @@ -5,7 +5,13 @@ import org.junit.runners.Parameterized.Parameters
import java.io.File
import kotlin.test.*

class Stress0IntegrationTest(override val versions: BuildVersions) : AbstractGradleIntegrationTest() {
/**
* Creates 100 tasks for the test project and runs them sequentially under low memory settings.
*
* If the test passes, it's likely there are no noticeable memory leaks.
* If it fails, it's likely that memory is leaking somewhere.
*/
class SequentialTasksExecutionStressTest(override val versions: BuildVersions) : AbstractGradleIntegrationTest() {

companion object {
@get:JvmStatic
Expand All @@ -15,7 +21,7 @@ class Stress0IntegrationTest(override val versions: BuildVersions) : AbstractGra

@BeforeTest
fun prepareProjectFiles() {
val templateProjectDir = File("projects", "it-stress-0")
val templateProjectDir = File("projects", "it-sequential-tasks-execution-stress")

templateProjectDir.listFiles().orEmpty()
.filter { it.isFile }
Expand All @@ -26,11 +32,10 @@ class Stress0IntegrationTest(override val versions: BuildVersions) : AbstractGra

@Test
fun execute() {
//if(!TestEnvironment.isExhaustive) return
val result = createGradleRunner(
"runTasks",
"-i",
"-s",
"--info",
"--stacktrace",
"-Ptask_number=100",
jvmArgs = listOf("-Xmx1G", "-XX:MaxMetaspaceSize=350m")
).buildRelaxed()
Expand Down
Expand Up @@ -2,7 +2,7 @@ package org.jetbrains.dokka.it.gradle

internal object TestedVersions {

val LATEST = BuildVersions("7.4.2", "1.7.10")
val LATEST = BuildVersions("7.4.2", "1.7.20")
val BASE =
BuildVersions.permutations(
gradleVersions = listOf("7.4.2", "6.9"),
Expand Down
Expand Up @@ -25,30 +25,36 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils

internal const val KOTLIN_PLAYGROUND_SCRIPT = "<script src=\"https://unpkg.com/kotlin-playground@1\"></script>"
abstract class SamplesTransformer(val context: DokkaContext) : PageTransformer {

abstract fun processBody(psiElement: PsiElement): String
abstract fun processImports(psiElement: PsiElement): String

final override fun invoke(input: RootPageNode): RootPageNode = runBlocking(Dispatchers.Default) { // to run from thread of Dispatchers.Default
val analysis = setUpAnalysis(context)
val kotlinPlaygroundScript = "<script src=\"https://unpkg.com/kotlin-playground@1\"></script>"

input.transformContentPagesTree { page ->
val samples = (page as? WithDocumentables)?.documentables?.flatMap {
it.documentation.entries.flatMap { entry ->
entry.value.children.filterIsInstance<Sample>().map { entry.key to it }
final override fun invoke(input: RootPageNode): RootPageNode =
/**
* Run from thread of [Dispatchers.Default]. It can help to avoid a memory leaks in `ThreadLocal` (that keeps `URLCLassLoader`)
* since we shut down Dispatchers.Default in the end of each task.
* Currently, all `ThreadLocal`s are in a compiler/IDE codebase.
*/
runBlocking(Dispatchers.Default) {
val analysis = setUpAnalysis(context)

input.transformContentPagesTree { page ->
val samples = (page as? WithDocumentables)?.documentables?.flatMap {
it.documentation.entries.flatMap { entry ->
entry.value.children.filterIsInstance<Sample>().map { entry.key to it }
}
}
}

samples?.fold(page as ContentPage) { acc, (sampleSourceSet, sample) ->
acc.modified(
content = acc.content.addSample(page, sampleSourceSet, sample.name, analysis),
embeddedResources = acc.embeddedResources + kotlinPlaygroundScript
)
} ?: page
samples?.fold(page as ContentPage) { acc, (sampleSourceSet, sample) ->
acc.modified(
content = acc.content.addSample(page, sampleSourceSet, sample.name, analysis),
embeddedResources = acc.embeddedResources + KOTLIN_PLAYGROUND_SCRIPT
)
} ?: page
}
}
}

private fun setUpAnalysis(context: DokkaContext) = context.configuration.sourceSets.associateWith { sourceSet ->
if (sourceSet.samples.isEmpty()) context.plugin<DokkaBase>()
Expand Down
Expand Up @@ -89,7 +89,10 @@ abstract class AbstractDokkaTask : DefaultTask() {
internal open fun generateDocumentation() {
DokkaBootstrap(runtime, DokkaBootstrapImpl::class).apply {
configure(buildDokkaConfiguration().toJsonString(), createProxyLogger())
// run in a new thread to avoid memory leaks that are related to ThreadLocal
/**
* Run in a new thread to avoid memory leaks that are related to ThreadLocal (that keeps `URLCLassLoader`)
* Currently, all `ThreadLocal`s are in a compiler/IDE codebase.
*/
Thread { generate() }.apply {
start()
join()
Expand Down

0 comments on commit 0e6b7f0

Please sign in to comment.