summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/util/indexing/diagnostic/ProjectIndexingHistoryFusReporterListener.kt
blob: b3643550c5cebc2a3e4c7f0b19c172b15cc6f3ea (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
// 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.diagnostic

import com.intellij.internal.statistic.eventLog.EventLogGroup
import com.intellij.internal.statistic.eventLog.events.EventFields
import com.intellij.internal.statistic.eventLog.events.ObjectEventData
import com.intellij.internal.statistic.eventLog.events.ObjectListEventField
import com.intellij.internal.statistic.service.fus.collectors.CounterUsagesCollector
import com.intellij.internal.statistic.utils.StatisticsUtil
import com.intellij.openapi.fileTypes.FileType
import com.intellij.openapi.fileTypes.FileTypeManager
import com.intellij.openapi.project.Project
import com.intellij.util.indexing.diagnostic.dto.toMillis
import java.util.concurrent.TimeUnit
import kotlin.math.roundToLong

class ProjectIndexingHistoryFusReporterListener : ProjectIndexingHistoryListener {
  override fun onStartedIndexing(projectIndexingHistory: ProjectIndexingHistory) {
    ProjectIndexingHistoryFusReporter.reportIndexingStarted(
      projectIndexingHistory.project,
      projectIndexingHistory.indexingSessionId
    )
  }

  override fun onFinishedIndexing(projectIndexingHistory: ProjectIndexingHistory) {
    val scanningTime = projectIndexingHistory.times.scanFilesDuration.toMillis()
    val numberOfFileProviders = projectIndexingHistory.scanningStatistics.size
    val numberOfScannedFiles = projectIndexingHistory.scanningStatistics.sumOf { it.numberOfScannedFiles }

    val numberOfFilesIndexedByExtensionsDuringScan =
      projectIndexingHistory.scanningStatistics.sumOf { it.numberOfFilesFullyIndexedByInfrastructureExtensions }
    val numberOfFilesIndexedByExtensionsWithLoadingContent =
      projectIndexingHistory.providerStatistics.sumOf { it.totalNumberOfFilesFullyIndexedByExtensions }
    val numberOfFilesIndexedWithLoadingContent = projectIndexingHistory.providerStatistics.sumOf { it.totalNumberOfIndexedFiles }

    val totalContentLoadingTime = projectIndexingHistory.totalStatsPerFileType.values.sumOf { it.totalContentLoadingTimeInAllThreads }
    val totalContentData = projectIndexingHistory.totalStatsPerFileType.values.sumOf { it.totalBytes }
    val averageContentLoadingSpeed = calculateReadSpeed(totalContentData, totalContentLoadingTime)

    val contentLoadingSpeedByFileType = HashMap<FileType, Long>()
    projectIndexingHistory.totalStatsPerFileType.forEach { (fileType, stats) ->
      if (stats.totalContentLoadingTimeInAllThreads != 0L && stats.totalBytes != 0L) {
        contentLoadingSpeedByFileType[FileTypeManager.getInstance().getStdFileType(fileType)] =
          calculateReadSpeed(stats.totalBytes, stats.totalContentLoadingTimeInAllThreads)
      }
    }

    ProjectIndexingHistoryFusReporter.reportIndexingFinished(
      projectIndexingHistory.project,
      projectIndexingHistory.indexingSessionId,
      projectIndexingHistory.times.wasFullIndexing,
      projectIndexingHistory.times.totalUpdatingTime.toMillis(),
      projectIndexingHistory.times.indexingDuration.toMillis(),
      scanningTime,
      numberOfFileProviders,
      numberOfScannedFiles,
      numberOfFilesIndexedByExtensionsDuringScan,
      numberOfFilesIndexedByExtensionsWithLoadingContent,
      numberOfFilesIndexedWithLoadingContent,
      averageContentLoadingSpeed,
      contentLoadingSpeedByFileType
    )
  }

  /**
   * @return speed as bytes per second
   * */
  private fun calculateReadSpeed(bytes: BytesNumber, loadingTime: TimeNano): Long {
    if (bytes == 0L || loadingTime == 0L) return 0L

    val nanoSecondInOneSecond = TimeUnit.SECONDS.toNanos(1)
    return if (bytes * nanoSecondInOneSecond > 0) // avoid hitting overflow; possible if loaded more then 9 223 372 037 bytes
    // as `loadingTime` in nanoseconds tend to be much bigger value then `bytes` prefer to divide as second step
      (bytes * nanoSecondInOneSecond) / loadingTime
    else // do not use by default to avoid unnecessary conversions
      ((bytes.toDouble() / loadingTime) * nanoSecondInOneSecond).roundToLong()
  }
}

object ProjectIndexingHistoryFusReporter : CounterUsagesCollector() {
  private val GROUP = EventLogGroup("indexing.statistics", 5)

  override fun getGroup() = GROUP

  private val indexingSessionId = EventFields.Long("indexing_session_id")

  private val isFullIndexing = EventFields.Boolean("is_full")
  private val totalTime = EventFields.Long("total_time")
  private val indexingTime = EventFields.Long("indexing_time")
  private val scanningTime = EventFields.Long("scanning_time")
  private val numberOfFileProviders = EventFields.Int("number_of_file_providers")
  private val numberOfScannedFiles = EventFields.Int("number_of_scanned_files")

  private val numberOfFilesIndexedByExtensionsDuringScan =
    EventFields.Int("number_of_files_indexed_by_extensions_during_scan")
  private val numberOfFilesIndexedByExtensionsWithLoadingContent =
    EventFields.Int("number_of_files_indexed_by_extensions_with_loading_content")
  private val numberOfFilesIndexedWithLoadingContent =
    EventFields.Int("number_of_files_indexed_with_loading_content")

  private val averageContentLoadingSpeed = EventFields.Long("average_content_loading_speed_bps")
  private val contentLoadingSpeedForFileType = EventFields.Long("average_content_loading_speed_for_file_type_bps")
  private val contentLoadingSpeedByFileType =
    ObjectListEventField("average_content_loading_speeds_by_file_type", EventFields.FileType, contentLoadingSpeedForFileType)

  private val indexingStarted = GROUP.registerVarargEvent(
    "started",
    indexingSessionId
  )

  private val indexingFinished = GROUP.registerVarargEvent(
    "finished",
    indexingSessionId,
    isFullIndexing,
    totalTime,
    indexingTime,
    scanningTime,
    numberOfFileProviders,
    numberOfScannedFiles,
    numberOfFilesIndexedByExtensionsDuringScan,
    numberOfFilesIndexedByExtensionsWithLoadingContent,
    numberOfFilesIndexedWithLoadingContent,
    averageContentLoadingSpeed,
    contentLoadingSpeedByFileType
  )

  fun reportIndexingStarted(project: Project, indexingSessionId: Long) {
    indexingStarted.log(
      project,
      this.indexingSessionId.with(indexingSessionId)
    )
  }

  fun reportIndexingFinished(
    project: Project,
    indexingSessionId: Long,
    wasFullIndexing: Boolean,
    totalTime: Long,
    indexingTime: Long,
    scanningTime: Long,
    numberOfFileProviders: Int,
    numberOfScannedFiles: Int,
    numberOfFilesIndexedByExtensionsDuringScan: Int,
    numberOfFilesIndexedByExtensionsWithLoadingContent: Int,
    numberOfFilesIndexedWithLoadingContent: Int,
    averageContentLoadingSpeed: Long,
    contentLoadingSpeedByFileType: Map<FileType, Long>

  ) {
    indexingFinished.log(
      project,
      this.indexingSessionId.with(indexingSessionId),
      this.isFullIndexing.with(wasFullIndexing),
      this.totalTime.with(totalTime),
      this.indexingTime.with(indexingTime),
      this.scanningTime.with(scanningTime),
      this.numberOfFileProviders.with(numberOfFileProviders),
      this.numberOfScannedFiles.with(StatisticsUtil.roundToHighestDigit(numberOfScannedFiles)),
      this.numberOfFilesIndexedByExtensionsDuringScan.with(StatisticsUtil.roundToHighestDigit(numberOfFilesIndexedByExtensionsDuringScan)),
      this.numberOfFilesIndexedByExtensionsWithLoadingContent.with(
        StatisticsUtil.roundToHighestDigit(numberOfFilesIndexedByExtensionsWithLoadingContent)),
      this.numberOfFilesIndexedWithLoadingContent.with(StatisticsUtil.roundToHighestDigit(numberOfFilesIndexedWithLoadingContent)),
      this.averageContentLoadingSpeed.with(averageContentLoadingSpeed),
      this.contentLoadingSpeedByFileType.with(contentLoadingSpeedByFileType.map { entry ->
        ObjectEventData(EventFields.FileType.with(entry.key), contentLoadingSpeedForFileType.with(entry.value))
      })
    )
  }

}