Skip to content

Commit

Permalink
Add ProjectOperationBenchmarks
Browse files Browse the repository at this point in the history
Related to dotnet#48682
  • Loading branch information
sharwell committed Jan 12, 2021
1 parent 2d4ef5b commit 289f118
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions src/Tools/IdeCoreBenchmarks/ProjectOperationBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Text;
using BenchmarkDotNet.Attributes;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;

namespace IdeCoreBenchmarks
{
public class ProjectOperationBenchmarks
{
[MemoryDiagnoser]
public class IterateDocuments
{
private Workspace _workspace;
private Project _emptyProject;
private Project _hundredProject;
private Project _thousandsProject;

public IterateDocuments()
{
// These fields are initialized in GlobalSetup
_workspace = null!;
_emptyProject = null!;
_hundredProject = null!;
_thousandsProject = null!;
}

[Params(0, 100, 10000)]
public int DocumentCount { get; set; }

private Project Project
{
get
{
return DocumentCount switch
{
0 => _emptyProject,
100 => _hundredProject,
10000 => _thousandsProject,
_ => throw new NotSupportedException($"'{nameof(DocumentCount)}' is out of range"),
};
}
}

[GlobalSetup]
public void GlobalSetup()
{
_workspace = new AdhocWorkspace();

var solution = _workspace.CurrentSolution;
_emptyProject = CreateProject(ref solution, name: "A", documentCount: 0);
_hundredProject = CreateProject(ref solution, name: "A", documentCount: 100);
_thousandsProject = CreateProject(ref solution, name: "A", documentCount: 10000);

static Project CreateProject(ref Solution solution, string name, int documentCount)
{
var projectId = ProjectId.CreateNewId(name);
solution = solution.AddProject(projectId, name, name, LanguageNames.CSharp);

var emptySourceText = SourceText.From("", Encoding.UTF8);
for (var i = 0; i < documentCount; i++)
{
var documentName = $"{i}.cs";
var documentId = DocumentId.CreateNewId(projectId, documentName);
solution = solution.AddDocument(documentId, documentName, emptySourceText);
}

return solution.GetRequiredProject(projectId);
}
}

[Benchmark(Description = "Project.DocumentIds")]
public int DocumentIds()
{
var count = 0;
foreach (var _ in Project.DocumentIds)
{
count++;
}

return count;
}

[Benchmark(Description = "Project.Documents")]
public int Documents()
{
var count = 0;
foreach (var _ in Project.Documents)
{
count++;
}

return count;
}
}
}
}

0 comments on commit 289f118

Please sign in to comment.