|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using UnityEngine; |
| 4 | +using NativeWebSocket; |
| 5 | + |
| 6 | +public class GCAllocBench |
| 7 | +{ |
| 8 | + public IEnumerator Run(string serverUrl, int payloadSize, BenchmarkLogger logger) |
| 9 | + { |
| 10 | + var ws = new WebSocket(serverUrl); |
| 11 | + bool connected = false; |
| 12 | + bool closed = false; |
| 13 | + int receivedCount = 0; |
| 14 | + |
| 15 | + ws.OnOpen += () => connected = true; |
| 16 | + ws.OnMessage += (data) => receivedCount++; |
| 17 | + ws.OnClose += (code) => closed = true; |
| 18 | + |
| 19 | + _ = ws.Connect(); |
| 20 | + |
| 21 | + float timeout = 0; |
| 22 | + while (!connected && !closed && timeout < 10f) |
| 23 | + { |
| 24 | + ws.DispatchMessageQueue(); |
| 25 | + timeout += Time.unscaledDeltaTime; |
| 26 | + yield return null; |
| 27 | + } |
| 28 | + if (!connected) yield break; |
| 29 | + |
| 30 | + byte[] payload = new byte[payloadSize]; |
| 31 | + int iterations = 500; |
| 32 | + |
| 33 | + // --- Warmup --- |
| 34 | + for (int i = 0; i < 50; i++) { _ = ws.Send(payload); } |
| 35 | + yield return null; |
| 36 | + ws.DispatchMessageQueue(); |
| 37 | + yield return new WaitForSeconds(0.5f); |
| 38 | + ws.DispatchMessageQueue(); |
| 39 | + |
| 40 | + // --- Measure Send allocations --- |
| 41 | + GC.Collect(); |
| 42 | + GC.WaitForPendingFinalizers(); |
| 43 | + GC.Collect(); |
| 44 | + |
| 45 | + int gen0Before = GC.CollectionCount(0); |
| 46 | + int gen1Before = GC.CollectionCount(1); |
| 47 | + int gen2Before = GC.CollectionCount(2); |
| 48 | + long memBefore = GC.GetTotalMemory(false); |
| 49 | + |
| 50 | + for (int i = 0; i < iterations; i++) { _ = ws.Send(payload); } |
| 51 | + |
| 52 | + long memAfter = GC.GetTotalMemory(false); |
| 53 | + int gen0After = GC.CollectionCount(0); |
| 54 | + int gen1After = GC.CollectionCount(1); |
| 55 | + int gen2After = GC.CollectionCount(2); |
| 56 | + |
| 57 | + double sendAllocPerOp = (double)(memAfter - memBefore) / iterations; |
| 58 | + logger.LogResult("GCAlloc_Send", payloadSize, "bytes_per_op", sendAllocPerOp); |
| 59 | + logger.LogResult("GCAlloc_Send", payloadSize, "gen0_collections", gen0After - gen0Before); |
| 60 | + logger.LogResult("GCAlloc_Send", payloadSize, "gen1_collections", gen1After - gen1Before); |
| 61 | + logger.LogResult("GCAlloc_Send", payloadSize, "gen2_collections", gen2After - gen2Before); |
| 62 | + |
| 63 | + // Wait for echoes to arrive |
| 64 | + yield return new WaitForSeconds(2f); |
| 65 | + |
| 66 | + // --- Measure Receive/Dispatch allocations --- |
| 67 | + // Ask server to flood, then wait for messages to queue up |
| 68 | + receivedCount = 0; |
| 69 | + _ = ws.SendText(string.Format("flood:{0}:{1}", iterations, payloadSize)); |
| 70 | + yield return new WaitForSeconds(2f); |
| 71 | + |
| 72 | + GC.Collect(); |
| 73 | + GC.WaitForPendingFinalizers(); |
| 74 | + GC.Collect(); |
| 75 | + |
| 76 | + gen0Before = GC.CollectionCount(0); |
| 77 | + memBefore = GC.GetTotalMemory(false); |
| 78 | + int countBefore = receivedCount; |
| 79 | + |
| 80 | + // Dispatch queued messages |
| 81 | + ws.DispatchMessageQueue(); |
| 82 | + |
| 83 | + memAfter = GC.GetTotalMemory(false); |
| 84 | + gen0After = GC.CollectionCount(0); |
| 85 | + int dispatched = receivedCount - countBefore; |
| 86 | + |
| 87 | + if (dispatched > 0) |
| 88 | + { |
| 89 | + double dispatchAllocPerOp = (double)(memAfter - memBefore) / dispatched; |
| 90 | + logger.LogResult("GCAlloc_Dispatch", payloadSize, "bytes_per_op", dispatchAllocPerOp); |
| 91 | + logger.LogResult("GCAlloc_Dispatch", payloadSize, "messages_dispatched", dispatched); |
| 92 | + logger.LogResult("GCAlloc_Dispatch", payloadSize, "gen0_collections", gen0After - gen0Before); |
| 93 | + } |
| 94 | + |
| 95 | + UnityEngine.Debug.Log(string.Format("GC: send={0:F0} bytes/op, dispatch={1} bytes/op (n={2})", |
| 96 | + sendAllocPerOp, |
| 97 | + dispatched > 0 ? ((double)(memAfter - memBefore) / dispatched).ToString("F0") : "N/A", |
| 98 | + dispatched)); |
| 99 | + |
| 100 | + closed = false; |
| 101 | + _ = ws.Close(); |
| 102 | + timeout = 0; |
| 103 | + while (!closed && timeout < 5f) |
| 104 | + { |
| 105 | + ws.DispatchMessageQueue(); |
| 106 | + timeout += Time.unscaledDeltaTime; |
| 107 | + yield return null; |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments