summaryrefslogtreecommitdiff
path: root/java/java-tests/testSrc/com/intellij/util/indexing/IndexDiagnosticTest.kt
blob: cbda9b713438c20098aa50a51ebcee933d62c42a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.util.indexing

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import com.intellij.idea.TestFor
import com.intellij.openapi.application.PathManager
import com.intellij.openapi.project.getProjectCachePath
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase
import com.intellij.util.SystemProperties
import com.intellij.util.indexing.diagnostic.IndexDiagnosticDumper
import com.intellij.util.indexing.diagnostic.ScanningType
import com.intellij.util.indexing.diagnostic.dto.*
import com.intellij.util.indexing.diagnostic.dump.paths.PortableFilePath
import org.junit.Assert
import java.nio.file.Files
import java.time.ZonedDateTime
import kotlin.streams.toList

/**
 * Tests for [IndexDiagnosticDumper].
 */
class IndexDiagnosticTest : JavaCodeInsightFixtureTestCase() {
  private var previousLogDir: String? = null

  override fun setUp() {
    previousLogDir = System.setProperty(PathManager.PROPERTY_LOG_PATH, createTempDir().path)
    IndexDiagnosticDumper.shouldDumpInUnitTestMode = true
    super.setUp()
  }

  override fun tearDown() {
    @Suppress("LocalVariableName") val _previousLogDir = previousLogDir
    super.tearDown()
    IndexDiagnosticDumper.shouldDumpInUnitTestMode = false
    SystemProperties.setProperty(PathManager.PROPERTY_LOG_PATH, _previousLogDir)
  }

  @TestFor(issues = ["IDEA-252012"])
  fun `test index diagnostics are laid out per project`() {
    myFixture.addFileToProject("A.java", "class A { void m() { } }")
    val indexingDiagnosticDir = IndexDiagnosticDumper.indexingDiagnosticDir
    val allDirs = Files.list(indexingDiagnosticDir).use { it.toList() }
    val projectDir = myFixture.project.getProjectCachePath(IndexDiagnosticDumper.indexingDiagnosticDir)
    assertEquals(listOf(projectDir), allDirs)
/*
    for (dir in allDirs) {
      val files = Files.list(dir).use { it.toList() }
      val jsonFiles = files.filter { it.extension == "json" }
      val htmlFiles = files.filter { it.extension == "html" }
      assertTrue(files.isNotEmpty())
      assertEquals(files.joinToString { it.toString() }, files.size, jsonFiles.size + htmlFiles.size)
      assertEquals(files.joinToString { it.toString() }, jsonFiles.map { it.nameWithoutExtension }.toSet(), htmlFiles.map { it.nameWithoutExtension }.toSet())
    }
*/
  }

  fun `test empty index diagnostic with default fields can be deserialized`() {
    val mapper = jacksonObjectMapper().registerKotlinModule()

    val indexDiagnostic = JsonIndexDiagnostic()
    println(mapper.writeValueAsString(indexDiagnostic))

    val deserialized = mapper.readValue<JsonIndexDiagnostic>(mapper.writeValueAsString(indexDiagnostic))
    Assert.assertEquals(indexDiagnostic, deserialized)
  }

  fun `test index diagnostics json can be deserialized`() {
    val indexDiagnostic = JsonIndexDiagnostic(
      JsonIndexDiagnosticAppInfo.create(),
      JsonRuntimeInfo.create(),
      JsonProjectIndexingHistory(
        projectName = "projectName",
        times = JsonProjectIndexingHistoryTimes(
          "reason",
          ScanningType.PARTIAL,
          JsonDuration(123),
          JsonDuration(456),
          JsonDuration(789),
          JsonDuration(110),
          JsonDuration(234),
          JsonDuration(345),
          JsonDuration(345),
          false,
          JsonDuration(),
          JsonDateTime(ZonedDateTime.now()),
          JsonDateTime(ZonedDateTime.now()),
          JsonDuration(333),
          false
        ),
        fileCount = JsonProjectIndexingFileCount(
          numberOfFileProviders = 0,
          numberOfScannedFiles = 0,
          numberOfFilesIndexedByInfrastructureExtensionsDuringScan = 0,
          numberOfFilesScheduledForIndexingAfterScan = 0,
          numberOfFilesIndexedByInfrastructureExtensionsDuringIndexingStage = 0,
          numberOfFilesIndexedWithLoadingContent = 0
        ),
        totalStatsPerFileType = listOf(
          JsonProjectIndexingHistory.JsonStatsPerFileType(
            "java",
            JsonPercentages(30, 100),
            JsonPercentages(40, 100),
            22,
            JsonFileSize(333),
            JsonProcessingSpeed(444, 555),
            listOf(
              JsonProjectIndexingHistory.JsonStatsPerFileType.JsonBiggestFileTypeContributor(
                "providerName",
                444,
                JsonFileSize(555),
                JsonPercentages(8, 10)
              )
            )
          )
        ),
        totalStatsPerIndexer = listOf(
          JsonProjectIndexingHistory.JsonStatsPerIndexer(
            "IdIndex",
            JsonPercentages(5, 10),
            444,
            555,
            JsonFileSize(123),
            JsonProcessingSpeed(111, 222),
            JsonProjectIndexingHistory.JsonStatsPerIndexer.JsonSnapshotInputMappingStats(33, 44, 55)
          )
        ),
        scanningStatistics = listOf(
          JsonScanningStatistics(
            "providerName",
            333,
            11,
            55,
            33,
            filesFullyIndexedByInfrastructureExtensions = listOf(PortableFilePath.RelativePath(PortableFilePath.ProjectRoot, "src/a.java").presentablePath),
            JsonDuration(123),
            JsonDuration(456),
            JsonDuration(789),
            JsonDuration(222),
            roots = listOf("<project root>"),
            scannedFiles = listOf(
              JsonScanningStatistics.JsonScannedFile(
                path = PortableFilePath.RelativePath(PortableFilePath.ProjectRoot, "src/a.java"),
                isUpToDate = true,
                wasFullyIndexedByInfrastructureExtension = false
              )
            )
          )
        ),
        fileProviderStatistics = listOf(
          JsonFileProviderIndexStatistics(
            providerName = "providerName",
            totalNumberOfIndexedFiles = 444,
            totalNumberOfFilesFullyIndexedByExtensions = 33,
            totalIndexingVisibleTime = JsonDuration(123),
            contentLoadingVisibleTime = JsonDuration(456),
            numberOfTooLargeForIndexingFiles = 1,
            slowIndexedFiles = listOf(
              JsonFileProviderIndexStatistics.JsonSlowIndexedFile(
                "file",
                JsonDuration(123),
                JsonDuration(456),
                JsonDuration(789)
              )
            ),
            indexedFiles = listOf(
              JsonFileProviderIndexStatistics.JsonIndexedFile(
                path = PortableFilePath.RelativePath(PortableFilePath.ProjectRoot, "src/a.java"),
                wasFullyIndexedByExtensions = true
              )
            ),
            isAppliedAllValuesSeparately = true,
            separateApplyingIndexesVisibleTime = JsonDuration(362)
          )
        )
      )
    )

    val mapper = jacksonObjectMapper().registerKotlinModule()

    println(mapper.writeValueAsString(indexDiagnostic))

    val deserialized = mapper.readValue<JsonIndexDiagnostic>(mapper.writeValueAsString(indexDiagnostic))
    Assert.assertEquals(indexDiagnostic, deserialized)
  }
}