系統架構:容器(引擎)透過反射載入多個「規則」DLL(各自獨立資料夾與配置),為每個規則建立獨立 AppDomain 並載入對應組件。引擎接收資料封包(1萬~3萬/秒,via WCF),根據條件分發至對應規則的 MarshalByRefObject 代理物件。
效能瓶頸:測試發現每秒僅處理 2 千封包,其餘丟失。需驗證跨 AppDomain 呼叫 MarshalByRefObject 的吞吐量。
const int COUNT = 500000;
static void Main() { FunA(); FunB(); FunC(); FunD(); }
static void FunA() { // 跨域建立實體並呼叫介面方法
var domain = AppDomain.CreateDomain("NewAppDomain");
var proxy = (RemoterProxy)domain.CreateInstanceAndUnwrap(...);
var inter = proxy.CreateInstance("ImplementA", ...);
CodeTimer.Time("ImplementA: Across AppDomain", COUNT, inter.FunA);
AppDomain.Unload(domain);
}
static void FunB() { // 同域介面呼叫
CodeTimer.Time("ImplementA: Same AppDomain", COUNT, new ImplementA().FunA);
}
static void FunC() { // 同域代理呼叫
CodeTimer.Time("RemoterProxy: Same AppDomain", COUNT, new RemoterProxy().FunTest);
}
static void FunD() { // 跨域代理呼叫
var domain = AppDomain.CreateDomain("NewAppDomain");
var proxy = (RemoterProxy)domain.CreateInstanceAndUnwrap(...);
CodeTimer.Time("RemoterProxy: Across AppDomain", COUNT, proxy.FunTest);
AppDomain.Unload(domain);
}
CodeTimer 使用 GetThreadTimes 取得 CPU 時間,配合 Stopwatch 測量耗時、呼叫頻率、GC 代數統計。

分析:ImplementA 與 RemoterProxy 在跨域模式下呼叫速度差異大,原因待釐清。歡迎大牛下載代碼協助分析。
隨文原始碼:https://www.bianceng.net/dotnet/201212/661.htm