// ReSharper restore InconsistentNaming protected SolverBase(ModelInfo modelInfo, float duration, int repeats, int samples, IModelBuilder modelBuilder = null) { if (modelBuilder == null) { modelBuilder = new ModelBuilder(); } this.modelInfo = modelInfo; this.duration = duration; SamplingParams = new SamplingParameters { RealizationCount = repeats, SampleCount = samples }; rng = RNGFactory.GetRNG(); _currentTime = 0.0f; model = modelBuilder.BuildModel(modelInfo); _time = model.Parameters.First(p => p.Name == "time"); untriggeredEvents = null; triggeredEvents = new Queue <TriggeredEvent>(); scheduledEvents = model.ScheduledEvents.Count > 0 ? new PriorityQueue <ScheduledEvent>(model.ScheduledEvents.Count) : null; trajectories = AllocateRecordingArrays(model.Observables, SamplingParams); InitializePerformanceMeasurements(); stopWatch = new Stopwatch(); }
public void AdaptiveSampler_CheckNumItemsSampled() { SamplingParameters parameters = new SamplingParameters(this.parent, this.traceId, "test", this.activityKindServer, null, null); AdaptiveSampler sampler = new AdaptiveSampler(5, 0.5); int numSampled = 0; // run for 200 seconds to let adaptive sampling adjust for (int i = 0; i < 200; i++) { // sends 10 events in 1 second int events = 10; for (int j = 0; j < events; j++) { SamplingResult result = sampler.ShouldSample(parameters); if (result.IsSampled) { numSampled += 1; } Thread.Sleep(1000 / events); } } int expectedSampled = 10; Assert.Equal(expectedSampled, numSampled / 200); }
public async Task <ActionResult <List <Chart> > > Post([FromBody] SamplingParameters query) { Dictionary <string, string> currencies = new Dictionary <string, string> { { "Доллар США", "R01235" }, { "Евро", "R01239" } }; Task[] tasks = new Task[2]; ValCurs valCurs1 = null; ValCurs valCurs2 = null; tasks[0] = Task.Factory.StartNew( () => valCurs1 = _requestToCbr .GetCurs(query.DateBegin, query.DateEnd, currencies["Доллар США"])); tasks[1] = Task.Factory.StartNew( () => valCurs2 = _requestToCbr .GetCurs(query.DateBegin, query.DateEnd, currencies["Евро"])); List <Chart> charts = new List <Chart>(); await Task.Factory.ContinueWhenAll(tasks, completedTasks => { var minVal = valCurs1.Record == null || valCurs2.Record == null ? "50" : valCurs1.Record.Concat(valCurs2.Record).Select(valCursRecord => valCursRecord.Value).Min(); charts.Add(_chartFactory.GetChart(valCurs1, minVal, currencies.ElementAt(0).Key)); charts.Add(_chartFactory.GetChart(valCurs2, minVal, currencies.ElementAt(1).Key)); }); return(new ObjectResult(charts)); }
public void AdaptiveSampler_CheckNewProbability_IncreaseMaxAllowed() { double oldProbability = 0.5; int maxSamplesAllowed = 40; SamplingParameters parameters = new SamplingParameters(this.parent, this.traceId, "test", this.activityKindServer, null, null); AdaptiveSampler sampler = new AdaptiveSampler(maxSamplesAllowed, oldProbability); // run for 200 seconds to let adaptive sampling adjust for (int i = 0; i < 200; i++) { // send 20 events each second int events = 20; for (int j = 0; j < events; j++) { sampler.ShouldSample(parameters); Thread.Sleep(1000 / events); } } // since events < maxSamplesAllowed, we expect to see all sampled (probability around 1) double expectedProbability = 1.0; // Assert.True(sampler.ProbSampler.GetProbability() - 0.1< expectedProbability && expectedProbability < sampler.ProbSampler.GetProbability() + 0.2); // Assert.Equal(1.0, sampler.ProbSampler.GetProbability()); Assert.True(expectedProbability - 0.1 <= sampler.ProbSampler.GetProbability() && sampler.ProbSampler.GetProbability() <= expectedProbability + 0.1); }
private static void VerifySamplingParameters(SamplingParameters samplingParameters) { Assert.NotNull(samplingParameters.Tags); Assert.Contains( samplingParameters.Tags, kvp => kvp.Key == SemanticConventions.AttributeDbSystem && (string)kvp.Value == SqlActivitySourceHelper.MicrosoftSqlServerDatabaseSystemName); }
private static void VerifySamplingParameters(SamplingParameters samplingParameters) { Assert.NotNull(samplingParameters.Tags); Assert.Contains( samplingParameters.Tags, kvp => kvp.Key == SemanticConventions.AttributeDbSystem && (string)kvp.Value == "redis"); }
public void AdaptiveSampler_CheckNewProbability_Adjusted() { double oldProbability = 0.5; int maxSamplesAllowed = 10; // should stay at 0.5 SamplingParameters parameters = new SamplingParameters(this.parent, this.traceId, "test", this.activityKindServer, null, null); AdaptiveSampler sampler = new AdaptiveSampler(maxSamplesAllowed, oldProbability); // run for 200 seconds to let adaptive sampling adjust for (int i = 0; i < 200; i++) { // send 20 events each second int events = 20; for (int j = 0; j < events; j++) { sampler.ShouldSample(parameters); Thread.Sleep(1000 / events); } } // probability should be around (10/20) 0.5 double expectedProbability = 0.5; Assert.True(expectedProbability - 0.1 <= sampler.ProbSampler.GetProbability() && sampler.ProbSampler.GetProbability() <= expectedProbability + 0.2); // max increases, so probability should increase to around 1 sampler.CheckMaxTelemetryItemsPerSecond(40); // run for 200 seconds to let adaptive sampling adjust for (int i = 0; i < 200; i++) { // send 20 events each second int events = 20; for (int j = 0; j < events; j++) { sampler.ShouldSample(parameters); Thread.Sleep(1000 / events); } } expectedProbability = 1; Assert.Equal(expectedProbability, sampler.ProbSampler.GetProbability()); Assert.True(expectedProbability - 0.1 <= sampler.ProbSampler.GetProbability() && sampler.ProbSampler.GetProbability() <= expectedProbability + 0.2); }
protected virtual void StepOnce() { float timeOfNextEvent = ExecuteScheduledEvents(duration); float newTau = CalculateProposedTau(timeOfNextEvent); if (newTau > duration) { throw new InvalidTimeStepException(); } CurrentTime = newTau; SamplingParams = trajectories.RecordObservables(model.Observables, SamplingParams, CurrentTime, duration); if (CurrentTime < duration) { ExecuteReactions(); UpdateTriggeredEvents(); } }
public void VerifyHashAlgorithmCorrectness() { byte[] testBytes = new byte[] { 0x8F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, }; byte[] testBytes2 = new byte[] { 0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F, 0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF, }; ActivityTraceId testId = ActivityTraceId.CreateFromBytes(testBytes); ActivityTraceId testId2 = ActivityTraceId.CreateFromBytes(testBytes2); ActivityContext parentContext = default(ActivityContext); SamplingParameters testParams = new SamplingParameters(parentContext, testId, "TestActivity", ActivityKind.Internal); SamplingParameters testParams2 = new SamplingParameters(parentContext, testId2, "TestActivity", ActivityKind.Internal); var zeroSampler = new ApplicationInsightsSampler(0); ApplicationInsightsSampler oneSampler = new ApplicationInsightsSampler(1); // 0.86 is below the sample score for testId1, but strict enough to drop testId2 ApplicationInsightsSampler ratioSampler = new ApplicationInsightsSampler(0.86f); Assert.Equal(SamplingDecision.Drop, zeroSampler.ShouldSample(testParams).Decision); Assert.Equal(SamplingDecision.Drop, zeroSampler.ShouldSample(testParams2).Decision); Assert.Equal(SamplingDecision.RecordAndSample, oneSampler.ShouldSample(testParams).Decision); Assert.Equal(SamplingDecision.RecordAndSample, oneSampler.ShouldSample(testParams2).Decision); Assert.Equal(SamplingDecision.Drop, ratioSampler.ShouldSample(testParams).Decision); Assert.Equal(SamplingDecision.RecordAndSample, ratioSampler.ShouldSample(testParams2).Decision); }
public void AdaptiveSampler_CheckNewProbability_StaySame() { double oldProbability = 0.5; int maxSamplesAllowed = 10; // should stay at 0.5 SamplingParameters parameters = new SamplingParameters(this.parent, this.traceId, "test", this.activityKindServer, null, null); AdaptiveSampler sampler = new AdaptiveSampler(maxSamplesAllowed, oldProbability); // run for 200 seconds to let adaptive sampling adjust for (int i = 0; i < 200; i++) { // send 20 events each second int events = 20; for (int j = 0; j < events; j++) { sampler.ShouldSample(parameters); Thread.Sleep(1000 / events); } } // since events > maxSamplesAllowed, we expect to less sampled (20/10 = 0.5) double expectedProbability = 0.5; Assert.True(expectedProbability - 0.2 <= sampler.ProbSampler.GetProbability() && sampler.ProbSampler.GetProbability() <= expectedProbability + 0.2); }
internal SamplingParameters RecordObservables(IEnumerable <Observable> observables, SamplingParameters samplingParams, float nextReactionTime, float duration) { if (samplingParams.SampleCount > 1) { if (nextReactionTime > duration) { throw new ArgumentException("nextReactionTime must be <= realization duration."); } float reportTime = duration * samplingParams.CurrentSample / (samplingParams.SampleCount - 1); while (reportTime < nextReactionTime) { // ReSharper disable PossibleMultipleEnumeration RecordObservables(observables, samplingParams.CurrentRealization, samplingParams.CurrentSample, reportTime); // ReSharper restore PossibleMultipleEnumeration samplingParams.CurrentSample++; reportTime = duration * samplingParams.CurrentSample / (samplingParams.SampleCount - 1); } } return(samplingParams); }
protected static Trajectories AllocateRecordingArrays(IEnumerable <Observable> observables, SamplingParameters samplingParams) { int numRealizations = samplingParams.RealizationCount; int numSamples = samplingParams.SampleCount; var trajectories = new Trajectories(numSamples); foreach (Observable o in observables) { var runs = new float[numRealizations][]; for (int i = 0; i < numRealizations; i++) { runs[i] = new float[numSamples]; } trajectories.Add(o, runs); } return(trajectories); }
private static long SampleThreadTest(int maxItemsAllowed) { long counter = 0; for (int j = 0; j < 200; j++) { for (int i = 0; i < 10; i++) { // System.Console.WriteLine("on span {0}", counter); // Libraries would simply write the following lines of code to // emit activities, which are the .NET representation of OT Spans. var source = new ActivitySource("MyCompany.MyProduct.MyWebServer"); // The below commented out line shows more likely code in a real world webserver. // using (var parent = source.StartActivity("HttpIn", ActivityKind.Server, HttpContext.Request.Headers["traceparent"] )) using (var parent = source.StartActivity("HttpIn", ActivityKind.Server)) { // counter += 1; SamplingParameters parameters = new SamplingParameters(); SamplingResult result = Sampler.ShouldSample(parameters); if (result.IsSampled) { counter++; System.Console.WriteLine("num of items actually sampled: {0})", counter / 200); } // TagNames can follow the OT guidelines // from https://github.com/open-telemetry/opentelemetry-specification/tree/master/specification/trace/semantic_conventions parent?.AddTag("http.method", "GET"); parent?.AddTag("http.host", "MyHostName"); if (parent != null) { parent.DisplayName = "HttpIn DisplayName"; // IsAllDataRequested is equivalent of Span.IsRecording if (parent.IsAllDataRequested) { parent.AddTag("expensive data", "This data is expensive to obtain. Avoid it if activity is not being recorded"); } } try { // Actual code to achieve the purpose of the library. // For websebserver example, this would be calling // user middlware pipeline. // There can be child activities. // In this example HttpOut is a child of HttpIn. using (var child = source.StartActivity("HttpOut", ActivityKind.Client)) { child?.AddTag("http.url", "www.mydependencyapi.com"); try { // do actual work. child?.AddEvent(new ActivityEvent("sample activity event.")); child?.AddTag("http.status_code", "200"); } catch (Exception) { child?.AddTag("http.status_code", "500"); } } parent?.AddTag("http.status_code", "200"); } catch (Exception) { parent?.AddTag("http.status_code", "500"); } } Thread.Sleep(1000 / 10); // System.Console.WriteLine("num items sampled in: {0}", Sampler.GetItemsSampled()); } } // System.Console.WriteLine("Spans sampled: {0}", counter); System.Console.WriteLine("Press Enter key to exit."); return(Sampler.GetItemsSampled()); }