<commit_before>// Created by <NAME> on 7/28/21.
// 

import XCTest
@_spi(Testing) import SwiftDriver
import TSCBasic
import TSCUtility

class CleanBuildPeformanceTests: XCTestCase {
  /// Test the cost of reading `swiftdeps` files without doing a full build. Use the files in "TestInputs/SampleSwiftDeps"
  ///
  /// When doing an incremental but clean build, after every file is compiled, its `swiftdeps` file must be
  /// deserialized and integrated into the `ModuleDependencyGraph`.
  /// This test allows us to profile an optimize this work. (Set up the scheme to run optimized code.)
  /// It reads and integrages every swiftdeps file in a given directory.
  func testCleanBuildSwiftDepsPerformance() throws {
    let packageRootPath = AbsolutePath(#file)
      .parentDirectory
      .parentDirectory
      .parentDirectory
    let swiftDepsDirectoryPath = packageRootPath.appending(components: "TestInputs", "SampleSwiftDeps")

    #if DEBUG
    let limit = 5 // Just a few times to be sure it works
    #else
    let limit = 100 // This is the real test, optimized code.
    #endif

    try testCleanBuildSwiftDepsPerformance(swiftDepsDirectory: swiftDepsDirectoryPath.pathString, atMost: limit)
  }

  /// Test the cost of reading `swiftdeps` files without doing a full build.
  ///
  /// When doing an incremental but clean build, after every file is compiled, its `swiftdeps` file must be
  /// deserialized and integrated into the `ModuleDependencyGraph`.
  /// This test allows us to profile an optimize this work. (Set up the scheme to run optimized code.)
  /// It reads and integrages every swiftdeps file in a given directory.
  /// - Parameters:
  ///    - swiftDepsDirectory: where the swiftdeps files are, either absolute, or relative to the current directory
  ///    - limit: the maximum number of swiftdeps files to process.
  func testCleanBuildSwiftDepsPerformance(swiftDepsDirectory: String, atMost limit: Int = .max) throws {
    let (outputFileMap, inputs) = try createOFMAndInputs(swiftDepsDirectory, atMost: limit)

    let info = IncrementalCompilationState.IncrementalDependencyAndInputSetup
      .mock(options: [], outputFileMap: outputFileMap)
    let g = ModuleDependencyGraph(info, .updatingAfterCompilation)
    measure {readSwiftDeps(for: inputs, into: g)}
  }

  /// Build the `OutputFileMap` and input vector for ``testCleanBuildSwiftDepsPeformance(_, atMost)``
  private func createOFMAndInputs(_ swiftDepsDirectory: String,
                                  atMost limit: Int
  ) throws -> (OutputFileMap, [TypedVirtualPath]) {
    let workingDirectory = localFileSystem.currentWorkingDirectory!<commit_msg>Resolve ambiguity on Linux<commit_after>// Created by <NAME> on 7/28/21.
// 

import XCTest
@_spi(Testing) import SwiftDriver
import TSCBasic
import TSCUtility

class CleanBuildPeformanceTests: XCTestCase {
  /// Test the cost of reading `swiftdeps` files without doing a full build. Use the files in "TestInputs/SampleSwiftDeps"
  ///
  /// When doing an incremental but clean build, after every file is compiled, its `swiftdeps` file must be
  /// deserialized and integrated into the `ModuleDependencyGraph`.
  /// This test allows us to profile an optimize this work. (Set up the scheme to run optimized code.)
  /// It reads and integrages every swiftdeps file in a given directory.
  func testCleanBuildSwiftDepsPerformance() throws {
    let packageRootPath = AbsolutePath(#file)
      .parentDirectory
      .parentDirectory
      .parentDirectory
    let swiftDepsDirectoryPath = packageRootPath.appending(components: "TestInputs", "SampleSwiftDeps")

    #if DEBUG
    let limit = 5 // Just a few times to be sure it works
    #else
    let limit = 100 // This is the real test, optimized code.
    #endif

    try test(swiftDepsDirectory: swiftDepsDirectoryPath.pathString, atMost: limit)
  }

  /// Test the cost of reading `swiftdeps` files without doing a full build.
  ///
  /// When doing an incremental but clean build, after every file is compiled, its `swiftdeps` file must be
  /// deserialized and integrated into the `ModuleDependencyGraph`.
  /// This test allows us to profile an optimize this work. (Set up the scheme to run optimized code.)
  /// It reads and integrages every swiftdeps file in a given directory.
  /// - Parameters:
  ///    - swiftDepsDirectory: where the swiftdeps files are, either absolute, or relative to the current directory
  ///    - limit: the maximum number of swiftdeps files to process.
  func test(swiftDepsDirectory: String, atMost limit: Int = .max) throws {
    let (outputFileMap, inputs) = try createOFMAndInputs(swiftDepsDirectory, atMost: limit)

    let info = IncrementalCompilationState.IncrementalDependencyAndInputSetup
      .mock(options: [], outputFileMap: outputFileMap)
    let g = ModuleDependencyGraph(info, .updatingAfterCompilation)
    measure {readSwiftDeps(for: inputs, into: g)}
  }

  /// Build the `OutputFileMap` and input vector for ``testCleanBuildSwiftDepsPeformance(_, atMost)``
  private func createOFMAndInputs(_ swiftDepsDirectory: String,
                                  atMost limit: Int
  ) throws -> (OutputFileMap, [TypedVirtualPath]) {
    let workingDirectory = localFileSystem.currentWorkingDirectory!
{'fraction_non_alphanumeric': 0.07954329210275927, 'fraction_numerical': 0.0034253092293054233, 'mean_word_length': 3.6267605633802815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4935464', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1398, 'n_words': 548}