public static async Task OnMessage(ClientWebSocket webSocket) { var bytes = new byte[10240]; var buffer = new ArraySegment <byte>(bytes); while (webSocket.State == WebSocketState.Open) { try { var result = await webSocket.ReceiveAsync(buffer, CancellationToken.None); if (result.MessageType == WebSocketMessageType.Close) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); } else { byte[] msgBytes = buffer.Skip(buffer.Offset).Take(result.Count).ToArray(); string message = Encoding.UTF8.GetString(msgBytes); if (message == "Start") { memUsageBefore = GC.GetTotalAllocatedBytes(true); finishedSorting = false; if (treeSort) { tree = new BinarySearchTree(); } else if (mergeSort) { merge = new MergeSort(); } else if (heapSort) { heap = new MaxHeap(); } } else if (message == "End") { FinishSorting(); } else { List <GPSData> points = JsonConvert.DeserializeObject <List <GPSData> >(message); HandleMessage(points); } } } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } } }
private static void FinishSorting() { if (treeSort) { tree.Inorder(tree.root); result = tree.sorted.Take(10).ToList(); tree = null; csvPath = @"E:\SKOLGREJER\Examen\analys\ServerTreeMem.csv"; } else if (mergeSort) { for (int i = merge.queue.Count - 1; i > 0; i--) { List <GPSData> mergedSection = merge.Merge(merge.queue[i], merge.queue[i - 1]); merge.queue[i - 1] = mergedSection; merge.queue.RemoveAt(i); } result = merge.queue[0].Take(10).ToList(); merge = null; csvPath = @"E:\SKOLGREJER\Examen\analys\ServerMergeMem.csv"; } else if (heapSort) { result = heap.Sort().Take(10).ToList(); heap = null; csvPath = @"E:\SKOLGREJER\Examen\analys\ServerHeapMem.csv"; } finishedSorting = true; memUsageAfter = GC.GetTotalAllocatedBytes(true); memUsageList.Add(memUsageAfter - memUsageBefore); GC.Collect(); if (memUsageList.Count == 500) { string csv = String.Join("\n", memUsageList.Select(x => x.ToString()).ToArray()); File.WriteAllText(csvPath, csv); } }