public void CanTrace_Always_StressTest() { // Create a rate limiter that allows 1_000_000 QPS, which effectively means can trace always. var rateLimiter = new RateLimiter(1_000_000, StopwatchTimer.Create()); int canTraceCounter = 0; int canTraceQuestions = 0; DateTime start = DateTime.UtcNow; DateTime end = start.AddSeconds(5.5); // Create 10 threads to run for a little over five seconds. var threads = Enumerable.Range(0, 10) .Select(_ => new Thread(() => { while (DateTime.UtcNow < end) { // Avoid completely tight-looping, so that we can actually start enough threads. // (Note that Thread.Yield isn't available on .NET Core.) Thread.Sleep(1); Interlocked.Increment(ref canTraceQuestions); if (rateLimiter.CanTrace()) { Interlocked.Increment(ref canTraceCounter); } } })) .ToList(); // Start the threads and wait for them all to finish threads.ForEach(t => t.Start()); threads.ForEach(t => t.Join()); // Since everything is to be traced, we should have as many traces as we attempted. Assert.Equal(canTraceQuestions, canTraceCounter); }
public void CanTrace_StressTest() { // Create a rate limiter that allows .5 QPS var rateLimiter = new RateLimiter(.5, StopwatchTimer.Create()); int canTraceCounter = 0; DateTime start = DateTime.UtcNow; DateTime end = start.AddSeconds(5.5); // Create 10 threads to run for a little over five seconds. var threads = Enumerable.Range(0, 10) .Select(_ => new Thread(() => { while (DateTime.UtcNow < end) { // Avoid completely tight-looping, so that we can actually start enough threads. // (Note that Thread.Yield isn't available on .NET Core.) Thread.Sleep(1); if (rateLimiter.CanTrace()) { Interlocked.Increment(ref canTraceCounter); } } })) .ToList(); // Start the threads and wait for them all to finish threads.ForEach(t => t.Start()); threads.ForEach(t => t.Join()); // We should have exactly 3 traces: one at t~=0, one at t~=2, one at t~=4. // (The test machine would have to be very highly loaded to take half a second between // the check in the while loop and the increment leading to an over-count, and it would have // to take half a second to start the threads to lead to an under-count.) Assert.Equal(3, canTraceCounter); }
public void CanTrace_StressTest() { // Create a rate limiter that allows 1 QPS var rateLimiter = new RateLimiter(1, StopwatchTimer.Create()); int canTraceCounter = 0; DateTime start = DateTime.UtcNow; DateTime end = start.AddSeconds(2.5); // Create 10 threads to run for a little over two seconds. var threads = Enumerable.Range(0, 10) .Select(_ => new Thread(() => { while (DateTime.UtcNow < end) { if (rateLimiter.CanTrace()) { Interlocked.Increment(ref canTraceCounter); } } })) .ToList(); // Start the threads and wait for them all to finish threads.ForEach(t => t.Start()); threads.ForEach(t => t.Join()); // We should have exactly 3 traces: one at t~=0, one at t~=1, one at t~=2. // (The test machine would have to be very highly loaded to take half a second between // the check in the while loop and the increment leading to an over-count, and it would have // to take half a second to start the threads to lead to an under-count.) Assert.Equal(3, canTraceCounter); }
public void GetElapsedMilliseconds_NoStart() { StopwatchTimer timer = StopwatchTimer.Create(); Assert.Equal(0, timer.GetElapsedMilliseconds()); Thread.Sleep(10); Assert.Equal(0, timer.GetElapsedMilliseconds()); }
private int FindIterationCount(StopwatchTimer timer, Action method, long minTicksForMeasurement, Action <string> log) { var iterationCount = 1; var diff = Measurer.MeasureDiffOutsideLoop(timer, new DelegateAction(method), iterationCount); while (diff < minTicksForMeasurement || diff > minTicksForMeasurement * 2) { iterationCount = (int)((iterationCount * minTicksForMeasurement) / Math.Max(diff, 1)); diff = Measurer.MeasureDiffOutsideLoop(timer, new DelegateAction(method), iterationCount); log?.Invoke($"Diff {diff} IterationCount {iterationCount}"); } return(iterationCount); }
private StopwatchTimer InteractionWaitTimer; // Stopwatch timer object which is used for handling the waiting period for interactions /// <summary> /// Unity method callback which is called at the start when running a scene. /// This method handles the initialisation for inhabitant controller. /// </summary> void Start() { // Reference the appropriate components (should never be null due to restriction) InhabitantAnimator = GetComponent <Animator>(); InhabitantNavMeshAgent = GetComponent <NavMeshAgent>(); // Initially set destination = current position, thus inhabitant is ready for movement DestinationPosition = transform.position; // Disable automatic rotation updates - this is handled manually InhabitantNavMeshAgent.updateRotation = false; // Setup timer to handle interaction waiting InteractionWaitTimer = new StopwatchTimer(0f); InteractionWaitTimer.OnDelayElapsed += OnInteractionWaitFinish; }
public void GetElapsedMilliseconds() { StopwatchTimer timer = StopwatchTimer.Create(); timer.Start(); long first = timer.GetElapsedMilliseconds(); Thread.Sleep(10); long second = timer.GetElapsedMilliseconds(); Thread.Sleep(20); long third = timer.GetElapsedMilliseconds(); Assert.True(second > first); Assert.True(third > second); }
public void DeterminePrecisionOfTimer() { var timer = new StopwatchTimer(); var timerSpec = timer.Spec; var jit = timer.Now; for (int i = 0; i < 100; i++) { var before = timer.Now; var after = timer.Now; while (after == before) { after = timer.Now; } var precision = after - before; // The call to Now itself takes time so precision is often many ticks e.g. 100 _output.WriteLine(precision.ToString()); } }
// Start is called before the first frame update void Start() { // initializes timers if they don't exist. if (text == null) { // if no text object has been set, it gets the text component. text = GetComponent <Text>(); } // countdown timer if (timer1 == null) { // searches for countdown timer attached to object. timer1 = FindObjectOfType <CountdownTimer>(); // if no timer is attached, a new one is made. if (timer1 == null) { timer1 = gameObject.AddComponent(typeof(CountdownTimer)) as CountdownTimer; } // sets countdown start time. timer1.SetCountdownStartTime(countdownStart, true); } // stopwatch timer. if (timer2 == null) { // searches for a stopwatch timer attached to the object. timer2 = FindObjectOfType <StopwatchTimer>(); // if no timer is attached, a new one is made. if (timer2 == null) { timer2 = gameObject.AddComponent(typeof(StopwatchTimer)) as StopwatchTimer; } } }
/// <summary> /// Primary utility method which initialises the genetic algorithm and begins evolving /// solutions in the genetic algorithm. /// </summary> public void StartGAEvaluations() { if (EvaluationTimer == null) { if ((SpawnerForPlayer) && (BasketTarget)) { // Initialise genetic algorithm BasketballGA = new GeneticAlgorithm(PopulationSize, MutationRate, UpwardForceThreshold, ForwardForceThreshold); BasketballGA.InitialisePopulation(); // Spawn player and balls + setup balls with chromosome data SpawnerForPlayer.SpawnPlayer(); SpawnerForPlayer.SetPlayerLookTarget(BasketTarget.transform); SpawnerForPlayer.SpawnBalls(PopulationSize, BasketTarget.transform); SetupBallsUsingChromosomeData(); // Start timer, assign action once evaluation complete EvaluationTimer = new StopwatchTimer(DurationBeforeEvolution); EvaluationTimer.OnTimerPoll.AddListener(EvaluationComplete); EvaluationTimer.Reset(); } } }
public QuickPerfMeasurements Run(Action <string> log)//<T>(this QuickPerfConfig<T> config) { Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime; Thread.CurrentThread.Priority = ThreadPriority.Highest; var timer = new StopwatchTimer(); var timerSpec = timer.Spec; Func <double, double> toNs = ticks => 1000 * 1000 * 1000 * ticks / (double)timerSpec.Frequency; const int MinTicksExtraMultiplier = 40; const int PrecisionDigits = 3; int precisionMultiplier = 1; for (int i = 0; i < PrecisionDigits; i++) { precisionMultiplier *= 10; } const int precisionMeasurementCount = 11; const int measurementsPerParamPerMethodCount = 21; // Measure timer precision var precisionMeasurements = ReserveMeasurements(precisionMeasurementCount); TimerMeasurer.MeasurePrecision(timer, precisionMeasurements); precisionMeasurements.Sort(); var min = precisionMeasurements.First(); var median = precisionMeasurements.Middle(); var max = precisionMeasurements.Last(); log?.Invoke($"Precision {min} - {median} - {max} ticks"); var minTicksForMeasurement = median * precisionMultiplier * MinTicksExtraMultiplier; foreach (var param in m_params) { // TODO: When to do m_setup?.Invoke(param); foreach (var namedMethod in m_methods) { var method = namedMethod.Method; var methodAction = new DelegateAction(method); var idleAction = new DelegateAction(IdleAction); // Pre-JIT and warmup var warmupIdleTime = Measurer.MeasureDiffOutsideLoop(timer, idleAction, 3); var warmupTime = Measurer.MeasureDiffOutsideLoop(timer, methodAction, 3); ForceAndWaitForGarbageCollection(); var iterationCount = FindIterationCount(timer, method, minTicksForMeasurement, log); //ForceAndWaitForGarbageCollection(); var idleTime = Measurer.MeasureDiffOutsideLoop(timer, idleAction, iterationCount); log?.Invoke($"Idle Ticks: {idleTime} ns: {toNs(idleTime / (double)iterationCount)}"); var measurements = ReserveMeasurements(measurementsPerParamPerMethodCount); ForceAndWaitForGarbageCollection(); for (int i = 0; i < measurements.Count; i++) { //ForceAndWaitForGarbageCollection(); var methodTime = Measurer.MeasureDiffOutsideLoop(timer, methodAction, iterationCount); measurements.Set(i, methodTime); log?.Invoke($"{namedMethod.Name} Ticks: {methodTime} ns: {toNs(methodTime / (double)iterationCount)}"); } } } return(new QuickPerfMeasurements()); }
/** * * @param i * @param supportPoints * @param minHeight * @return */ public bool checkAddItemRevisitedRight(Item i, List <PotentialPoint> supportPoints, double minHeight) { StopwatchTimer.Start("add"); double zone1, zone2, zone3, zone4; zone1 = this.getZonesDepth(); zone2 = zone1 * 2; zone3 = zone1 * 3; zone4 = this.getDepth(); bool weightOk = true; double rnd = StaticRandom.NextDouble(); // Need to use usableSpace in relation with item to be added // supportPoints.sort(Comparator.comparing(PotentialPoint::getUsableSpace)); if (supportPoints.Any() && this.weightFits(i)) { for (int j = 0; j < supportPoints.Count; j++) { PotentialPoint supportPoint = supportPoints[j]; i.setBottomRightFront(supportPoint); if (((i.bottomLeftFront.getZ() < zone1) && (this.getZone1Weight() + i.getWeight() > this.maxZone1Weight * (1 + weightSurplusPerameter))) || (((i.bottomLeftFront.z >= zone1) && (i.bottomLeftFront.getZ() < zone2)) && (this.getZone2Weight() + i.getWeight() > this.maxZone2Weight * (1 + weightSurplusPerameter))) || (((i.bottomLeftFront.z >= zone2) && (i.bottomLeftFront.getZ() < zone3)) && (this.getZone3Weight() + i.getWeight() > this.maxZone3Weight * (1 + weightSurplusPerameter))) || (((i.bottomLeftFront.z >= zone3) && (i.bottomLeftFront.getZ() < zone4)) && (this.getZone4Weight() + i.getWeight() > this.maxZone3Weight * (1 + weightSurplusPerameter)))) { weightOk = false; } else { weightOk = true; } if ((!this.itemsOverlapping(i)) && (this.dimensionsFitRight(i)) && (supportPoint.cornerCheck(i, this, minHeight)) && weightOk) { PotentialPoint cPointXAxis = PotentialPoint.createXAxisPotentialPoint(i, j, supportPoints); PotentialPoint cPointZAxis = PotentialPoint.createZAxisPotentialPointRight(i, j, supportPoints); // TEST--------------- if (cPointXAxis.x != 0) { PotentialPoint nspZzero = new PotentialPoint(0, cPointXAxis.y, cPointXAxis.z); PotentialPoint.addPointToPotentialPoints(nspZzero, supportPoints); } PotentialPoint.addPointToPotentialPoints(cPointXAxis, supportPoints); PotentialPoint.addPointToPotentialPoints(cPointZAxis, supportPoints); if (i.isStackable()) { PotentialPoint cPointYAxis = PotentialPoint.createYAxisPotentialPointRight(i, j, supportPoints); PotentialPoint.addPointToPotentialPoints(cPointYAxis, supportPoints); int dim = supportPoints.Count; List <PotentialPoint> newList = new List <PotentialPoint>(); double maxSubst; foreach (PotentialPoint sp in supportPoints) { if ((!sp.equals(cPointYAxis)) && sp.getZ() == cPointYAxis.getZ() && sp.getY() == cPointYAxis.getY()) { if (sp.getMaxXAllowed() > cPointYAxis.getMaxXAllowed()) { maxSubst = sp.getMaxXAllowed(); } else { maxSubst = cPointYAxis.getMaxXAllowed(); } PotentialPoint toAdd = new PotentialPoint(sp.getX(), sp.getY(), sp.getZ(), maxSubst, sp.getMaxYAllowed(), sp.getMaxZAllowed()); newList.Add(toAdd); } } foreach (PotentialPoint sp in newList) { // System.err.println("ADDED"); supportPoints.Add(sp); } } /* PotentialPoint cPointXAxisProjectionMax = PotentialPoint.createXAxisPotentialPointProjectionMax(i, j, supportPoints); * PotentialPoint.addPointToPotentialPoints(cPointXAxisProjectionMax, supportPoints);*/ PotentialPoint cPointProjectionZ = PotentialPoint.createPointProjectionZ(i, j, supportPoints); PotentialPoint.addPointToPotentialPoints(cPointProjectionZ, supportPoints); /*°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°*/ /*Remove chosen support point*/ PotentialPoint.usedPotentialPoints.Add(supportPoints[j]); supportPoints.Remove(supportPoints[j]); StopwatchTimer.Stop("add"); return(true); } } } StopwatchTimer.Stop("add"); return(false); }
public void MeasureTimerPrecision_Stopwatch() { var timer = new StopwatchTimer(); MeasureTimerPrecision(timer); }