/// <summary> /// Processes the payload and invokes the <see cref="LavalinkSocket.PayloadReceived"/> /// event asynchronously. (Can be override for event catching) /// </summary> /// <param name="eventArgs">the event arguments</param> /// <returns>a task that represents the asynchronous operation</returns> protected override async Task OnPayloadReceived(PayloadReceivedEventArgs eventArgs) { var payload = eventArgs.Payload; // received an event if (payload is EventPayload eventPayload) { await OnEventReceived(eventPayload); } // received a payload for a player if (payload is IPlayerPayload playerPayload) { await OnPlayerPayloadReceived(playerPayload); } // statistics update received if (payload is StatsUpdatePayload statsUpdate) { Statistics = new NodeStatistics(statsUpdate.Players, statsUpdate.PlayingPlayers, statsUpdate.Uptime, statsUpdate.Memory, statsUpdate.Processor, statsUpdate.FrameStatistics); await OnStatisticsUpdateAsync(new NodeStatisticsUpdateEventArgs(Statistics)); } await base.OnPayloadReceived(eventArgs); }
public void TestScoreBalancingStrategy() { var badNode = new NodeStatistics(10, 200, TimeSpan.FromHours(60), new MemoryStatistics { AllocatedMemory = 10000, FreeMemory = 1000, ReservableMemory = 1000, UsedMemory = 5000 }, new ProcessorStatistics { Cores = 10, NodeLoad = 100, SystemLoad = 100 }, new FrameStatistics { AverageDeficitFrames = 10, AverageFramesSent = 100000, AverageNulledFrames = 100 }); var goodNode = new NodeStatistics(1, 1, TimeSpan.FromSeconds(100), new MemoryStatistics { AllocatedMemory = 1000, FreeMemory = 500, ReservableMemory = 100, UsedMemory = 1000 }, new ProcessorStatistics { Cores = 54, NodeLoad = 20, SystemLoad = 30 }, new FrameStatistics { AverageDeficitFrames = 0, AverageFramesSent = 10000, AverageNulledFrames = 0 }); var badScore = LoadBalancingStrategies.CalculateScore(badNode); var goodScore = LoadBalancingStrategies.CalculateScore(goodNode); Assert.True(badScore < goodScore, $"bad score ({badScore}) < good score ({goodScore})"); }
public NodeHandler(Node node, NodeManager node_manager) { this.node = node; this.node_manager = node_manager; this.socket_address = new IPEndPoint(IPAddress.Parse(node.Host), node.Port); this.node_statistics = new NodeStatistics(node); ChangeState(NodeHandlerState.Discovered); }
public void ComputeNode() { Node node = new Node(); node.id = 2; node.left = 90; node.right = 10; node.op = "div"; NodeStatistics nodestats = new NodeStatistics(); nodestats.ComputeNode(node); Assert.AreEqual(10, nodestats.result); }
/// <summary> /// Calculates the node score. /// </summary> /// <param name="statistics">the node statistics</param> /// <returns>the score for the node (higher = better)</returns> public static double CalculateScore(NodeStatistics statistics) { // no statistics retrieved for the node. if (statistics is null || statistics.Processor is null || statistics.Memory is null || statistics.FrameStatistics is null) { return(0d); } // factors = the number of factors including the average calculation var factors = 0d; var totalWeight = 0d; void IncludeFactor(double score, double weight) { factors += weight; totalWeight += score * weight; } // statistics.Processor.Cores (4x) higher = better IncludeFactor(statistics.Processor.Cores, 4); // statistics.Processor.NodeLoad (3x) lower = better IncludeFactor(-statistics.Processor.NodeLoad, 3); // statistics.Processor.SystemLoad (4x) lower = better IncludeFactor(-statistics.Processor.SystemLoad, 4); // statistics.Memory.FreeMemory (2x) higher = better IncludeFactor(statistics.Memory.FreeMemory, 2); // statistics.PlayingPlayers (3x) lower = better IncludeFactor(-statistics.PlayingPlayers, 3); // statistics.FrameStatistics.AverageDeficitFrames (0.5x) lower = better IncludeFactor(-statistics.FrameStatistics.AverageDeficitFrames, .5d); // statistics.FrameStatistics.AverageNulledFrames (0.7x) lower = better (nulled frames // are worser than deficit) IncludeFactor(-statistics.FrameStatistics.AverageNulledFrames, .7d); // calculate average of weight return(totalWeight / factors); }
/// <summary> /// Returns the node statistics for a given "root" /// </summary> /// <param name="node"></param> /// <returns></returns> public NodeStatistics InternalStatistics(Node node) { if (node == null) { return(new NodeStatistics(0, 0)); } if (node.IsLeaf) { return(new NodeStatistics(1, 0)); } var res = new NodeStatistics(0, 1); for (var i = 0; i <= node.Count; ++i) { res.Add(InternalStatistics(node.GetChild(i))); } return(res); }
public void InputOutputAdapter_works() { var outputNode = new TestSourceNode((ctx, e) => { for (var i = 1; i <= 5; i++) { e.Emit(new Row { ["number"] = i }); } e.SignalEnd(); }); var inputNode = new TestSinkNode(); var nodeStatistics = new NodeStatistics(); nodeStatistics.RegisterNode(outputNode); nodeStatistics.RegisterNode(inputNode); var ioAdapter = new InputOutputAdapter <Row>(outputNode); ioAdapter.SetNodeStatisticsCollector(nodeStatistics); ioAdapter.AttachConsumer(inputNode); var context = new EtlPipelineContext(); outputNode.Execute(context); inputNode.Execute(context); inputNode.ReceivedItems.Count.Should().Be(5); for (var i = 1; i <= 5; i++) { inputNode.ReceivedItems[i - 1]["number"].Should().Be(i); } nodeStatistics.TotalReads.Should().Be(5); nodeStatistics.TotalWrites.Should().Be(5); nodeStatistics.TotalErrors.Should().Be(0); }
private static void Assert(NodeStatistics nodesMetada) { nodesMetada.Should().NotBeNull(); nodesMetada.Total.Should().BeGreaterThan(0); nodesMetada.Successful.Should().BeGreaterThan(0); }
/// <summary> /// Initializes a new instance of the No /// </summary> /// <param name="statistics">the statistics for the node</param> /// <exception cref="ArgumentNullException"> /// thrown if the specified <paramref name="statistics"/> parameter is <see langword="null"/>. /// </exception> public NodeStatisticsUpdateEventArgs(NodeStatistics statistics) => Statistics = statistics ?? throw new ArgumentNullException(nameof(statistics));
/// <summary> /// Adds another set of statistics to these stats. /// </summary> /// <param name="x"></param> /// <returns></returns> public NodeStatistics Add(NodeStatistics x) { LeafNodes += x.LeafNodes; InternalNodes += x.InternalNodes; return(this); }
public void InitNode(byte[] node_id, int remote_port) { this.node = new Node(node_id, this.socket_address.Address.MapToIPv4().ToString(), remote_port); this.node_statistics = Manager.Instance.NodeManager.GetNodeStatistics(node); Manager.Instance.NodeManager.GetNodeHandler(node).Node = node; }