Raw FIR builder: add performance test to build FIR for all Kotlin
#KT-24086 Fixed
This commit is contained in:
+19
-9
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.cli.common.script.CliScriptDefinitionProvider
|
||||
import org.jetbrains.kotlin.fir.FirRenderer
|
||||
import org.jetbrains.kotlin.fir.FirSessionBase
|
||||
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtPsiFactory
|
||||
@@ -50,20 +51,29 @@ abstract class AbstractRawFirBuilderTestCase : KtParsingTestCase(
|
||||
}
|
||||
|
||||
protected fun doRawFirTest(filePath: String) {
|
||||
doBaseTest(filePath, KtNodeTypes.KT_FILE)
|
||||
doRawFirTest(filePath, checkNeeded = true)
|
||||
}
|
||||
|
||||
private fun doBaseTest(filePath: String, fileType: IElementType) {
|
||||
protected fun doRawFirTest(filePath: String, checkNeeded: Boolean) {
|
||||
val file = createKtFile(filePath)
|
||||
val firFile = file.toFirFile()
|
||||
if (checkNeeded) {
|
||||
val firFileDump = StringBuilder().also { FirRenderer(it).visitFile(firFile) }.toString()
|
||||
val expectedPath = filePath.replace(".kt", ".txt")
|
||||
KotlinTestUtils.assertEqualsToFile(File(expectedPath), firFileDump)
|
||||
}
|
||||
}
|
||||
|
||||
protected fun createKtFile(filePath: String): KtFile {
|
||||
myFileExt = FileUtilRt.getExtension(PathUtil.getFileName(filePath))
|
||||
val file = createFile(filePath, fileType) as KtFile
|
||||
myFile = file
|
||||
|
||||
val firFile = RawFirBuilder(object : FirSessionBase() {}).buildFirFile(file)
|
||||
val firFileDump = StringBuilder().also { FirRenderer(it).visitFile(firFile) }.toString()
|
||||
val expectedPath = filePath.replace(".kt", ".txt")
|
||||
KotlinTestUtils.assertEqualsToFile(File(expectedPath), firFileDump)
|
||||
return (createFile(filePath, KtNodeTypes.KT_FILE) as KtFile).apply {
|
||||
myFile = this
|
||||
}
|
||||
}
|
||||
|
||||
protected fun KtFile.toFirFile(): FirFile =
|
||||
RawFirBuilder(object : FirSessionBase() {}).buildFirFile(this)
|
||||
|
||||
override fun tearDown() {
|
||||
super.tearDown()
|
||||
FileTypeRegistry.ourInstanceGetter = Getter<FileTypeRegistry> { FileTypeManager.getInstance() }
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.fir.builder
|
||||
|
||||
import com.intellij.testFramework.TestDataPath
|
||||
import org.jetbrains.kotlin.fir.FirRenderer
|
||||
import org.jetbrains.kotlin.fir.declarations.FirFile
|
||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners
|
||||
import org.junit.runner.RunWith
|
||||
import java.io.File
|
||||
import kotlin.system.measureNanoTime
|
||||
|
||||
@TestDataPath("\$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners::class)
|
||||
class RawFirBuilderTotalKotlinTestCase : AbstractRawFirBuilderTestCase() {
|
||||
|
||||
fun testTotalKotlin() {
|
||||
val root = File(testDataPath)
|
||||
var counter = 0
|
||||
var time = 0L
|
||||
var totalLength = 0
|
||||
println("BASE PATH: $testDataPath")
|
||||
for (file in root.walkTopDown()) {
|
||||
if (file.isDirectory) continue
|
||||
if (file.path.contains("testData") || file.path.contains("resources")) continue
|
||||
if (file.extension != "kt") continue
|
||||
try {
|
||||
val ktFile = createKtFile(file.toRelativeString(root))
|
||||
var firFile: FirFile? = null
|
||||
time += measureNanoTime {
|
||||
firFile = ktFile.toFirFile()
|
||||
}
|
||||
totalLength += StringBuilder().also { FirRenderer(it).visitFile(firFile!!) }.length
|
||||
counter++
|
||||
} catch (e: Exception) {
|
||||
println("TIME PER FILE: ${(time / counter) * 1e-6} ms, COUNTER: $counter")
|
||||
println("EXCEPTION in: " + file.toRelativeString(root))
|
||||
throw e
|
||||
}
|
||||
}
|
||||
println("SUCCESS!")
|
||||
println("TOTAL LENGTH: $totalLength")
|
||||
println("TIME PER FILE: ${(time / counter) * 1e-6} ms, COUNTER: $counter")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user