private async Task HandleMeasurementEvent(MeasurementEvent measurementEvent) { await _measurementsRepository.Write(new WriteRequest(measurementEvent.DeviceId, measurementEvent.Points)); var channelEvent = new ChannelEvent(measurementEvent.DeviceId, Events.MeasurementOccured, measurementEvent); _hubContext.Clients.Group(measurementEvent.DeviceId).EventArrived(channelEvent); }
public void OnMeasurement() { long bytesRead = Interlocked.Exchange(ref BytesRead, 0); long bytesWritten = Interlocked.Exchange(ref BytesWritten, 0); long curTicks = Environment.TickCount64; long lastTicks = Volatile.Read(ref _lastMeasurement); Volatile.Write(ref _lastMeasurement, curTicks); double rCurTicks = 1000.0 / (curTicks - lastTicks); MeasurementEvent?.Invoke(bytesRead * rCurTicks, bytesWritten * rCurTicks); }
public void Simulate(double time) { CurrentTime = 0; Server server = new Server(); Events.Enqueue(new BandwidthEvent { Time = RandomNumberFromExpDistribution(5), BandwidthChange = BandwidthEvent.NextBandwidthChange() }); Events.Enqueue(new DownloadingFinishedEvent { Time = CHUNK_LENGTH * server.VideoSize / Bandwidth }); Events.Enqueue(new MeasurementEvent { Time = 0 }); int CyclesWithTooMuch = 0, CyclesWithTooLittle = 0; while (CurrentTime < time) { Event nextEvent = Events.Dequeue(); var bufferGone = nextEvent.Time - CurrentTime; if (nextEvent as BandwidthEvent != null) { BandwidthEvent bandwidthEvent = (BandwidthEvent)nextEvent; BufferSize -= bufferGone; BufferSize = BufferSize < 0 ? 0 : BufferSize; CurrentTime = bandwidthEvent.Time; Bandwidth += bandwidthEvent.BandwidthChange; OnLogCreated(new LogEventArgs { Message = "Bandwidth change, time = " + Truncate(CurrentTime.ToString()) + ", buffer = " + Truncate(BufferSize.ToString()) + ", bandwidth = " + Bandwidth + ", size = " + server.VideoSize }); Events.Enqueue(new BandwidthEvent { Time = CurrentTime + RandomNumberFromExpDistribution(10), BandwidthChange = BandwidthEvent.NextBandwidthChange() }); } else if (nextEvent as DownloadingFinishedEvent != null) { DownloadingFinishedEvent downloadingEvent = (DownloadingFinishedEvent)nextEvent; BufferSize -= bufferGone; if (BufferSize < 10) { CyclesWithTooLittle++; } else { CyclesWithTooLittle = 0; } if (CyclesWithTooLittle > 2) { server.SetSmallerVideoSize(); CyclesWithTooLittle = 0; } BufferSize = BufferSize < 0 ? 0 : BufferSize; CurrentTime = downloadingEvent.Time; BufferSize += CHUNK_LENGTH; OnLogCreated(new LogEventArgs { Message = "Downloaded, time = " + Truncate(CurrentTime.ToString()) + ", buffer = " + Truncate(BufferSize.ToString()) + ", bandwidth = " + Bandwidth + ", size = " + server.VideoSize }); /// jeżeli nadwyżka bufora jest duża, to zliczaj, jak długo jest taki stan. Jeżeli więcej niż dwa chunki, to zwiększ jakość obrazu var surplus = (BufferSize - 30 > 0) ? BufferSize - 30 : 0; if (surplus > 0) { CyclesWithTooMuch++; } else { CyclesWithTooMuch = 0; } if (CyclesWithTooMuch > 2) { server.SetBiggerVideoSize(); CyclesWithTooMuch = 0; } OnLogCreated(new LogEventArgs { Message = "Request, time = " + Truncate(CurrentTime.ToString()) + ", buffer = " + Truncate(BufferSize.ToString()) + ", bandwidth = " + Bandwidth + ", size = " + server.VideoSize }); Events.Enqueue(new DownloadingFinishedEvent { Time = CHUNK_LENGTH * server.VideoSize / Bandwidth + CurrentTime + surplus }); } else if (nextEvent as MeasurementEvent != null) { MeasurementEvent measurementEvent = (MeasurementEvent)nextEvent; BufferSize -= bufferGone; BufferSize = BufferSize < 0 ? 0 : BufferSize; CurrentTime = measurementEvent.Time; YGraphValues.Add(BufferSize); BandwidthValues.Add(Bandwidth); SegmentSizeValues.Add(server.VideoSize); Events.Enqueue(new MeasurementEvent { Time = CurrentTime + MeasurementSamplingRate }); } } }