Skip to content

Optimize NUnit file check by reading only initial portion of file to find <test-run tag #288

@GabLeRoux

Description

@GabLeRoux

To enhance performance in the CI workflow, we propose optimizing the NUnit XML file check in results-check.ts by reading only the initial 4KB of each file when verifying the presence of the <test-run> tag. This change will reduce unnecessary I/O operations on large files and prevent processing of non-NUnit XML files in the artifacts directory. This improvement is particularly useful for projects with large test suites that produce sizeable XML files, as it minimizes memory usage and enhances CI performance.

For more details, refer to the discussion on the GitHub pull request here.

Proposed Code Change

Replace the current full-file read logic with the following snippet to implement the optimization:

try {
  const filePath = path.join(artifactsPath, filepath);
  const fd = fs.openSync(filePath, 'r');
  const bufferSize = 4096; // Read the first 4KB
  const buffer = Buffer.alloc(bufferSize);
  const bytesRead = fs.readSync(fd, buffer, 0, bufferSize, 0);
  fs.closeSync(fd);

  const contentStart = buffer.toString('utf8', 0, bytesRead);
  if (!contentStart.includes('<test-run')) {
    core.warning(`File does not appear to be a NUnit XML file: ${filepath}`);
    return;
  }

  const fileData = await ResultsParser.parseResults(filePath);
  core.info(fileData.summary);
  runs.push(fileData);
} catch (error: any) {
  core.warning(`Failed to parse ${filepath}: ${error.message}`);
}

Benefits

  • Performance Boost: Minimizes I/O operations and memory usage by only reading a small portion of each file.
  • Improved Efficiency: Enhances processing speed for large projects with extensive XML test results.
  • Streamlined Workflow: Reduces CI workflow interruptions by logging warnings for non-NUnit files instead of failing the entire run.

This improvement streamlines error handling and increases the resilience of file parsing in CI environments, benefiting large-scale Unity projects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions