We are using Roslyn (the nuget package Microsoft.CodeAnalysis.CSharp, version 1.0.0.0-beta2) to compile generated code. We have 5000 CSharp files as String in memory and transform those to SyntaxTree's:
f => CSharpSyntaxTree.ParseText(f)
Then we create a compilation:
CSharpCompilation compilation = CSharpCompilation.Create(assemblyName ,
syntaxTrees: files.Keys,
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Release
)
);
And then we compile:
compilation.Emit(
Path.Combine(assemblyOutputPath, assemblyName + ".dll"),
Path.Combine(assemblyOutputPath, assemblyName + ".pdb"));
Sometimes this takes a couple of minutes (like 5 to 10). But sometimes we see compiletimes of multiple hours (2 hours up to 6 hours).
How can I debug/trace this? Are there any API's for getting verbose information from Roslyn?
Edit: We run this process on a virtual Windows 2012 R2 instance with 4 GB RAM. I'm not sure what the impact is of such a setup.
Edit2: Yes, the process consumes a lot of memory and cpu (I saw usage spiking to 1500 MB ram). But in the case where it is slow, there is still plenty memory as far as I can see.
Edit3: We have run the process with performance counters on. Memory usage seems fine, no where near any limits. CPU also not near any limits, but we do see some spikes now and then:
Edit4: To react on @pharring , we are not compiling binaries. And I'm not sure if it is directly related to the source code. These compile times are not always this high. And only on the virtual machines. We haven't seen it on our development machines. On the other hand, it is a large dll. The resulting dll is 37 MB. I cannot link the source code for others to look at, but I will try to make a dump.