private async Task DoWork(object state)
        {
            TaskState taskState = (TaskState)state;
            Stopwatch stopwatch = new Stopwatch();

            while (active)
            {
                taskState.Status = State.Running;
                if (!await CanProcess() && downloadersActive)
                {
                    taskState.Status = State.Paused;
                    await Task.Delay(250);

                    continue;
                }

                if (!PageProcessingQueue.IsEmpty)
                {
                    UnparsedPage page;
                    if (!PageProcessingQueue.TryDequeue(out page))
                    {
                        //another thread grabbed the page
                        continue;
                    }
                    stopwatch.Time(() =>
                    {
                        return(ProcessThreadPage(page.Thread, page.Html));
                    },
                                   (long time) => TelemetryManager.Incriment(TelemetryType.processed_pages, taskState.Id, time)
                                   );
                }

                if (!ProcessingQueue.IsEmpty)
                {
                    UnparsedThread thread;
                    if (!ProcessingQueue.TryDequeue(out thread))
                    {
                        continue;
                    }

                    stopwatch.Time(() =>
                    {
                        ProcessThread(thread.Id, thread.Html);
                    },
                                   (long time) => TelemetryManager.Incriment(TelemetryType.processed_threads, taskState.Id, time)
                                   );
                }
                else if (!downloadersActive) //Queue is empty, no more downloads are being performed, set active to false after 1s delay
                {
                    taskState.Status = State.Paused;
                    await Task.Delay(1000);

                    active = false;
                    break;
                }
            }
            taskState.Status = State.Complete;
        }
示例#2
0
 public void TimingTest()
 {
     Stopwatch timer = new Stopwatch();
     int iterations = 10;
     long ms = timer.Time(() => ReadFromTestProvider(1024 * 1024, 4096), iterations);
     Console.WriteLine("{0} ms", ms);
 }
        public void PerformanceTest()
        {
            var input1 = new TestSampleProvider(32000, 1);
            var input2 = new TestSampleProvider(32000, 1);
            var input3 = new TestSampleProvider(32000, 1);
            var input4 = new TestSampleProvider(32000, 1);
            var mp     = new MultiplexingSampleProvider(new ISampleProvider[] { input1, input2, input3, input4 }, 4);

            mp.ConnectInputToOutput(0, 3);
            mp.ConnectInputToOutput(1, 2);
            mp.ConnectInputToOutput(2, 1);
            mp.ConnectInputToOutput(3, 0);

            float[]   buffer   = new float[input1.WaveFormat.AverageBytesPerSecond / 4];
            Stopwatch s        = new Stopwatch();
            var       duration = s.Time(() =>
            {
                // read one hour worth of audio
                for (int n = 0; n < 60 * 60; n++)
                {
                    mp.Read(buffer, 0, buffer.Length);
                }
            });

            Console.WriteLine("Performance test took {0}ms", duration);
        }
示例#4
0
        public void ShortTest()
        {
            Stopwatch timer = new Stopwatch();
            long      ms    = timer.Time(() => ReadFromTestProvider(16 * 1024, 4096));

            Console.WriteLine("{0} ms", ms);
        }
        static void TestTokenizing(int n, long max)
        {
            Stopwatch stopwatch = new Stopwatch();

            string[] expressions = new string[n];
            MathExpression.MathExpression   mathExpression = new MathExpression.MathExpression();
            Func <string, List <string> >[] funcs          = new Func <string, List <string> >[]
            {
                TokenizeByEnumerator,
                TokenizeByRegex,
            };

            for (int i = 0; i < n; i++)
            {
                expressions[i] = mathExpression.GenerateRandom(n, max);
            }

            foreach (Func <string, List <string> > func in funcs)
            {
                Action   action       = () => Array.ForEach(expressions, e => func(e));
                TimeSpan timeSpan     = stopwatch.Time(action);
                int      milliSeconds = Convert.ToInt32(timeSpan.TotalMilliseconds);
                Console.WriteLine($"{func.Method.Name,-27} : {milliSeconds,4} ms");
            }
        }
        static void TestLongAppend(int n)
        {
            Stopwatch stopwatch = new Stopwatch();

            Tuple <long, long>[]      appendableRandoms = new Tuple <long, long> [n];
            Func <long, long, long>[] funcs             = new Func <long, long, long>[]
            {
                LongExtensions.LongExtensions.AppendByRangeChecked,
                LongExtensions.LongExtensions.AppendLoop,
                LongExtensions.LongExtensions.AppendByRange,
                LongExtensions.LongExtensions.AppendByMagnitude,
            };
            for (int i = 0; i < n; i++)
            {
                appendableRandoms[i] = GetRandomAppendableTuple();
            }

            Console.WriteLine($"n = {n}");
            foreach (Func <long, long, long> func in funcs)
            {
                Action   action       = () => Array.ForEach(appendableRandoms, t => func(t.Item1, t.Item2));
                TimeSpan timeSpan     = stopwatch.Time(action);
                int      milliSeconds = Convert.ToInt32(timeSpan.TotalMilliseconds);
                Console.WriteLine($"{func.Method.Name,-27} : {milliSeconds,4} ms");
            }
        }
        static void TestLongMagnitude(int n)
        {
            long[]    randoms   = new long[n];
            Stopwatch stopwatch = new Stopwatch();

            Func <long, long>[] funcs = new Func <long, long>[]
            {
                LongExtensions.LongExtensions.MagnitudeByMultiplication,
                LongExtensions.LongExtensions.MagnitudeByMultiplication2,
                LongExtensions.LongExtensions.MagnitudeByRange,
                LongExtensions.LongExtensions.MagnitudeByMultiplication3,
                LongExtensions.LongExtensions.MagnitudeByDivision,
            };
            for (int i = 0; i < n; i++)
            {
                randoms[i] = Rand.NextLong();
            }

            Console.WriteLine($"n = {n}");
            foreach (Func <long, long> func in funcs)
            {
                Action   action       = () => Array.ForEach(randoms, i => func(i));
                TimeSpan timeSpan     = stopwatch.Time(action);
                int      milliSeconds = Convert.ToInt32(timeSpan.TotalMilliseconds);
                Console.WriteLine($"{func.Method.Name,-27} : {milliSeconds,4} ms");
            }
        }
示例#8
0
        public void TimingTest()
        {
            Stopwatch timer      = new Stopwatch();
            int       iterations = 10;
            long      ms         = timer.Time(() => ReadFromTestProvider(1024 * 1024, 4096), iterations);

            Console.WriteLine("{0} ms", ms);
        }
示例#9
0
        static double Time(int n, int tokens, long max)
        {
            Stopwatch stopwatch = new Stopwatch();

            string[] expressions = RandomExpressions(n, tokens, max);

            TimeSpan timeSpan = stopwatch.Time(() => Array.ForEach(expressions, e => new BinaryTree(e).TryEvaluate()));

            return(timeSpan.TotalMilliseconds);
        }
        public static UIElement CreateTimedButton(MemberInfo methodToLink, object bindingObject, Dictionary <string, object> options = null)
        {
            var            e   = new System.Windows.Controls.Button();
            AbstractModule m   = options["currentModule"] as AbstractModule;
            Action         del = (Action)Delegate.CreateDelegate(typeof(Action), bindingObject, methodToLink as MethodInfo);

            e.Click  += (o, i) => TimedAction(del, m, m.Iterations).Invoke();
            e.Content = methodToLink.Name;
            return(e);

            // Lokale Methode: "Wrappt" die zu ausführende Methode mit einer Stopwatch und erlaubt es X iterationen durchzuführen
            Action TimedAction(Action a, AbstractModule target, object iter)
            {
                var s = new Stopwatch();

                return(new Action(() => s.Time(() => a(), iter, target)));
            }
        }
示例#11
0
 public static long Time(this Stopwatch sw, Action action)
 {
     return(sw.Time(action, 1));
 }
        public void PerformanceTest()
        {
            var input1 = new TestSampleProvider(32000, 1);
            var input2 = new TestSampleProvider(32000, 1);
            var input3 = new TestSampleProvider(32000, 1);
            var input4 = new TestSampleProvider(32000, 1);
            var mp = new MultiplexingSampleProvider(new ISampleProvider[] { input1, input2, input3, input4 }, 4);
            mp.ConnectInputToOutput(0, 3);
            mp.ConnectInputToOutput(1, 2);
            mp.ConnectInputToOutput(2, 1);
            mp.ConnectInputToOutput(3, 0);

            float[] buffer = new float[input1.WaveFormat.AverageBytesPerSecond / 4];
            Stopwatch s = new Stopwatch();
            var duration = s.Time(() =>
            {
                // read one hour worth of audio
                for (int n = 0; n < 60 * 60; n++)
                {
                    mp.Read(buffer, 0, buffer.Length);
                }
            });
            Console.WriteLine("Performance test took {0}ms", duration);
        }
示例#13
0
        public static long TimeMillis(this Stopwatch sw, Action action, int iterations = 1)
        {
            var elapsed = sw.Time(p => { action(); return(true); }, true, iterations);

            return(elapsed.Item1.Milliseconds);
        }
示例#14
0
 public static Tuple <TimeSpan, T> Time <T>(this Stopwatch sw, Func <T> func, int iterations = 1)
 {
     return(sw.Time(f => func(), true, iterations));
 }
示例#15
0
 public void ShortTest()
 {
     Stopwatch timer = new Stopwatch();
     long ms = timer.Time(() => ReadFromTestProvider(16 * 1024, 4096));
     Console.WriteLine("{0} ms", ms);
 }
示例#16
0
        private async Task PruneBlock(PruningMode mode, Chain chain, ChainedHeader pruneBlock)
        {
            //TODO the replay information about blocks that have been rolled back also needs to be pruned (UnmintedTx)

            var txCount = 0;
            var totalStopwatch = Stopwatch.StartNew();
            var pruneBlockTxesStopwatch = new Stopwatch();
            var pruneTxIndexStopwatch = new Stopwatch();
            var pruneSpentTxesStopwatch = new Stopwatch();

            // retrieve the spent txes for this block
            BlockSpentTxes spentTxes;
            using (var handle = this.storageManager.OpenChainStateCursor())
            {
                var chainStateCursor = handle.Item;

                chainStateCursor.BeginTransaction(readOnly: true);
                chainStateCursor.TryGetBlockSpentTxes(pruneBlock.Height, out spentTxes);
            }

            if (spentTxes != null)
            {
                txCount = spentTxes.Count;

                pruneBlockTxesStopwatch.Start();
                pruneTxIndexStopwatch.Start();

                await Task.WhenAll(
                    // prune block txes (either merkle prune or delete)
                    PruneBlockTxesAsync(mode, chain, pruneBlock, spentTxes)
                        .ContinueWith(task => { pruneBlockTxesStopwatch.Stop(); task.Wait(); }),
                    // prune tx index
                    PruneTxIndexAsync(mode, chain, pruneBlock, spentTxes)
                        .ContinueWith(task => { pruneTxIndexStopwatch.Stop(); task.Wait(); })
                    );

                // remove block spent txes information
                //TODO should have a buffer on removing this, block txes pruning may need it again if flush doesn't happen
                pruneSpentTxesStopwatch.Time(() =>
                    PruneBlockSpentTxes(mode, chain, pruneBlock));
            }
            else //if (pruneBlock.Height > 0)
            {
                //TODO can't throw an exception unless the pruned chain is persisted
                //logger.Info("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX: {0:N0}".Format2(pruneBlock.Height));
                //throw new InvalidOperationException();
                txCount = 0;
            }

            // track stats
            txCountMeasure.Tick(txCount);
            txRateMeasure.Tick((float)(txCount / totalStopwatch.Elapsed.TotalSeconds));
            pruneBlockTxesDurationMeasure.Tick(pruneBlockTxesStopwatch.Elapsed);
            pruneTxIndexDurationMeasure.Tick(pruneTxIndexStopwatch.Elapsed);
            pruneSpentTxesDurationMeasure.Tick(pruneSpentTxesStopwatch.Elapsed);
            totalDurationMeasure.Tick(totalStopwatch.Elapsed);
        }
示例#17
0
        public static void LogResult(this Stopwatch sw, Action action, int iterations, string message)
        {
            var time = sw.Time(action, iterations);

            Console.WriteLine(message + ": " + time.ToString());
        }
示例#18
0
        private static void Execute()
        {
            var random = RandomArray();

            Console.WriteLine("Today's Randomly selected integers are:");
            foreach (var r in random)
            {
                Console.WriteLine(r);
            }

            Seperator();

            var selectionSort = new SelectionSort().Sort(random);

            Console.WriteLine("Utilising Selection Sort:");
            foreach (var selection in selectionSort)
            {
                Console.WriteLine(selection);
            }

            Seperator();

            var insertionSort = new InsertionSort().Sort(random);

            Console.WriteLine("Utilising Insertion Sort:");
            foreach (var insertion in insertionSort)
            {
                Console.WriteLine(insertion);
            }

            Seperator();

            var mergeSort = new MergeSort().Sort(random, 0, random.Length - 1);

            Console.WriteLine("Utilising Merge Sort:");
            var fullMergeWatch = Stopwatch.StartNew();

            foreach (var merge in mergeSort)
            {
                Console.WriteLine(merge);
            }
            fullMergeWatch.Stop();
            var elapsedTime = fullMergeWatch.Elapsed;

            Seperator();

            Console.WriteLine($"A Full cycle of a merge sort takes {elapsedTime} milliseconds");

            Console.WriteLine("One iteration of Selection Sort takes");
            var s = new Stopwatch();

            Console.WriteLine(s.Time(() => new SelectionSort().Sort(random), 1));

            Console.WriteLine("One iteration of Insertion Sort takes");
            var i = new Stopwatch();

            Console.WriteLine(i.Time(() => new InsertionSort().Sort(random), 1));

            Console.WriteLine("One iteration of Merge Sort takes");
            var m = new Stopwatch();

            Console.WriteLine(m.Time(() => new MergeSort().Sort(random, 0, random.Length - 1), 1));

            Seperator();
            TimeTheMethods(random);
        }