-
-
Notifications
You must be signed in to change notification settings - Fork 990
Provide a way to run benchmarks under Unity Mono and IL2CPP #1860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for opening this issue. I was the one who asked about it on that thread. One thing that I'm hesitant about is actually supporting the full engine, since BDN currently does not support any applications except console (it used to support others, but dropped support). Of course, in order to do that, Unity would need to make its IL2CPP runtime available separately from the Unity engine, which I'm not sure if they have any interest in doing. Since the IL2CPP runtime comes bundled with the engine, that means if we want to add support for that without Unity's cooperation, a full engine build is required. I'm not sure how we could compile with Unity dependencies from In any case, ignoring Unity dependencies, project generation can be done like so:
[Edit] A template can be created for all this to more easily tweak the necessary values. Then it can be built like this:
I'm not sure if the runtime Mono/IL2CPP can be configured from command line, might need to manually configure the And finally executed like this:
Another thing I'm unsure of is how the Unity process will communicate with the BDN console process. I haven't dug in too deeply how it currently works with spawned benchmark processes. And, of course, all this requires the user to have a licensed copy of Unity (free license is enough).
In this case, I don't think it really matters either way. But BDN already has a C# source generator for external-process, so may as well re-use it (in-process wouldn't even be possible here anyway). Though, another question comes out of this... how would we support code with
Yes, I don't think there's any way around that. And since that is the case, I also don't think that BDN should care about the Unity version (unless/until Unity ships IL2CPP runtime separately).
I would say no. The template can be just the same as the .Net 5/6 template, just with
[Edit] Perhaps there is something useful here? https://github.com/djkaty/Il2CppInspector
With my method of project generation above, this shouldn't be a concern. It has zero Unity dependencies. [Edit] I guess this is only for |
I think that avoiding the usage of the full engine here isn't a good idea, after all without the overhead it introduces, this won't result in testing in a realistic usecase, since nobody would use just their Mono.
The
This could be handled by using
The non framework compatibility levels only limit the available API surface, so the framework ones can be used here in all cases. But settings like IL2CPP build configuration or Incremental GC should be configurable since they have runtime impact.
That's a fair point, I guess it being non versioned might actually be better. But the reference assemblies would still be versioned.
Unity profiler API could possibly be used for this |
All good points.
Net 4.X and NetStandard 2.1 actually diverge in their API surfaces, because Net 4.X does not implement NetStandard 2.1. NetStandard 2.1 has some APIs that Net 4.X does not, and vice-versa. |
Another thing to think about concerning benchmarking Unity APIs is, as it is currently implemented, BDN wouldn't be able to measure any of Unity's AsyncOperations, because those all rely on main thread frame updates for their completions. BDN's engine implementation would need to be adjusted to be able to measure across multiple frames. The current Task measurement would not suffice, because it blocks the calling thread. |
In Unity they actually made the ref assemblies for .Net 4.x include everything. |
Well there are a lot of build options, and even different options between unity versions. So maybe BDN could allow the user to specify the path to a project settings file to be copied into the benchmark project. |
Yes, Unity supports in version 2021.2 and later. |
@joshpeterson I also tried to load SourceGenerator using the same procedure as the Analyzer support, but I don't think it works. |
Yes! I'm not too familiar with the support myself, but you can find Unity's official documentation about them here: https://docs.unity3d.com/2021.2/Documentation/Manual/roslyn-analyzers.html
This is an unfortunate issue because Unity does not yet support .NET 5 class libraries. You can add a definition of
I'm not certain about this - but yes, Unity's generated .csproj files are still in the older format. |
@joshpeterson |
I just like to say, I as probably many others look forward a lot to see BenchmarkDotNet working with Unity. |
So I've been looking for a way to run a set of standardized benchmarks in Unity that use the Graphics engine as well. BenchmarkDotNet seemed like an obvious candidate, however it seems that it currently doesn't support Unity officially at least. One way I can think off is that you build a ready to benchmark executable with Unity and then just use BenchmarkDotNet to run that executable, passing various parameters. However this currently doesn't really instrument very well. You can do this by running executables from within the code being benchmarked but that measures all kind of overhead from the operating system as well. It would be nice if you could add instrumentation to the Unity code and let that communicate with the external benchmarking process. Especially since you might want to benchmark your code on different hardware platforms as well this could take the form of a server-client architecture where Unity acts as the client to a benchmarking process that runs on the machine collecting data but the app under test would run on a device connected via the local network. |
Having thought about this some more I don't see why BenchmarkDotNet wouldn't be able to run from within Unity if it has the required extension points for custom builders etc. Some target platforms require manual intervention anyway such as iOS/Android where you deploy to a remote device. In those cases automation might be hard and BenchmarkDotNet can't run completely autonomous. In those cases it would act as the coordinator which it could do just as well from within Unity, waiting at certain points for the interaction with the built projects to be ready. Does BenchmarkDotNet have extension points for adding runtimes/build scripts/communication with created projects or is it extremely opinionated in that regard? |
We provide such extension points and contributions that add support for new runtimes are welcomed. In #1483 you can see what it took to implement a WASM toolchain. Toolchain consists of generator (generates C# code and a project file), builder (a thing that knows how to build given project) and an executor (a thing that runs the produced exe). |
@tgjones developed recently some specialized toolchain+runtime for running BenchmarkDotNet with Unity Mono, IL2CPP and even Burst. He had only one small change via #2285 to make it possible. We cannot make it OSS but for anyone that would like to develop an OSS version, it's doable to make it working (and Tim might be able to give some high level pointers here if there are questions) |
Impressive!
@xoofx is there any chance you could release it as a NuGet package? |
Currently, there is no official (but there have been attempts it seems, looking at #1838 and #759) way to use BenchmarkDotNet under Unity nor to benchmark code running under Unity. This has resulted in many improper benchmarks with incorrect results being shared around the internet. Providing an option to use Unity Mono and IL2CPP as runtimes would be highly beneficial for game development, since that is an usecase where perfomance often matters.
I thought about how the support should look like and I've came up with a few things:
Open questions:
MemoryDiagnoser
orDisassemblyDiagnoser
(this project could provide some insight with this one for Mono) would be possible to implement.There has been recent talk about this on Unitys forum, which tells us that Unity staff would be interested in this too, at least according to @joshpeterson.
The text was updated successfully, but these errors were encountered: