NBodyBenchmark.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using BenchmarkDotNet.Attributes;
  2. using Lua;
  3. using Lua.Standard;
  4. using MoonSharp.Interpreter;
  5. [Config(typeof(BenchmarkConfig))]
  6. public class NBodyBenchmark
  7. {
  8. BenchmarkCore core = default!;
  9. LuaValue[] buffer = new LuaValue[1];
  10. [IterationSetup]
  11. public void Setup()
  12. {
  13. core = new();
  14. core.Setup("n-body.lua");
  15. core.LuaCSharpState.OpenStandardLibraries();
  16. }
  17. [IterationCleanup]
  18. public void Cleanup()
  19. {
  20. core.Dispose();
  21. core = default!;
  22. GC.Collect();
  23. }
  24. // [Benchmark(Description = "MoonSharp (RunString)")]
  25. // public DynValue Benchmark_MoonSharp_String()
  26. // {
  27. // return core.MoonSharpState.DoString(core.SourceText);
  28. // }
  29. //
  30. // [Benchmark(Description = "MoonSharp (RunFile)")]
  31. // public DynValue Benchmark_MoonSharp_File()
  32. // {
  33. // return core.MoonSharpState.DoFile(core.FilePath);
  34. // }
  35. [Benchmark(Description = "NLua (DoString)", Baseline = true)]
  36. public object[] Benchmark_NLua_String()
  37. {
  38. return core.NLuaState.DoString(core.SourceText);
  39. }
  40. // [Benchmark(Description = "NLua (DoFile)")]
  41. // public object[] Benchmark_NLua_File()
  42. // {
  43. // return core.NLuaState.DoFile(core.FilePath);
  44. // }
  45. [Benchmark(Description = "Lua-CSharp (DoString)")]
  46. public async Task<LuaValue> Benchmark_LuaCSharp_String()
  47. {
  48. await core.LuaCSharpState.DoStringAsync(core.SourceText, buffer);
  49. return buffer[0];
  50. }
  51. // [Benchmark(Description = "Lua-CSharp (DoFileAsync)")]
  52. // public async Task<LuaValue> Benchmark_LuaCSharp_File()
  53. // {
  54. // await core.LuaCSharpState.DoFileAsync(core.FilePath, buffer);
  55. // return buffer[0];
  56. // }
  57. }