|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +using Python.Runtime; |
| 7 | + |
| 8 | +namespace Python.PerformanceTests |
| 9 | +{ |
| 10 | + public class BaselineComparisonBenchmarkBase |
| 11 | + { |
| 12 | + public BaselineComparisonBenchmarkBase() |
| 13 | + { |
| 14 | + Console.WriteLine($"CWD: {Environment.CurrentDirectory}"); |
| 15 | + Console.WriteLine($"Using Python.Runtime from {typeof(PythonEngine).Assembly.Location} {typeof(PythonEngine).Assembly.GetName()}"); |
| 16 | + |
| 17 | + try { |
| 18 | + PythonEngine.Initialize(); |
| 19 | + Console.WriteLine("Python Initialized"); |
| 20 | + if (PythonEngine.BeginAllowThreads() == IntPtr.Zero) |
| 21 | + throw new PythonException(); |
| 22 | + Console.WriteLine("Threading enabled"); |
| 23 | + } |
| 24 | + catch (Exception e) { |
| 25 | + Console.WriteLine(e); |
| 26 | + throw; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + static BaselineComparisonBenchmarkBase() |
| 31 | + { |
| 32 | + string pythonRuntimeDll = Environment.GetEnvironmentVariable(BaselineComparisonConfig.EnvironmentVariableName); |
| 33 | + if (string.IsNullOrEmpty(pythonRuntimeDll)) |
| 34 | + { |
| 35 | + throw new ArgumentException( |
| 36 | + "Required environment variable is missing", |
| 37 | + BaselineComparisonConfig.EnvironmentVariableName); |
| 38 | + } |
| 39 | + |
| 40 | + Console.WriteLine("Preloading " + pythonRuntimeDll); |
| 41 | + Assembly.LoadFrom(pythonRuntimeDll); |
| 42 | + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { |
| 43 | + if (assembly.FullName.StartsWith("Python.Runtime")) |
| 44 | + Console.WriteLine(assembly.Location); |
| 45 | + foreach(var dependency in assembly.GetReferencedAssemblies()) |
| 46 | + if (dependency.FullName.Contains("Python.Runtime")) { |
| 47 | + Console.WriteLine($"{assembly} -> {dependency}"); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve; |
| 52 | + } |
| 53 | + |
| 54 | + static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args) { |
| 55 | + if (!args.Name.StartsWith("Python.Runtime")) |
| 56 | + return null; |
| 57 | + |
| 58 | + var preloaded = AppDomain.CurrentDomain.GetAssemblies() |
| 59 | + .FirstOrDefault(a => a.GetName().Name == "Python.Runtime"); |
| 60 | + if (preloaded != null) return preloaded; |
| 61 | + |
| 62 | + string pythonRuntimeDll = Environment.GetEnvironmentVariable(BaselineComparisonConfig.EnvironmentVariableName); |
| 63 | + if (string.IsNullOrEmpty(pythonRuntimeDll)) |
| 64 | + return null; |
| 65 | + |
| 66 | + return Assembly.LoadFrom(pythonRuntimeDll); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments