Exemplo n.º 1
0
        public void RunSerialize <TSymbols, TObject>(BaseSerializer <TSymbols> serializer, BaseTest <TObject> test)
        {
            object result = null;

            var dataRow = results.Rows.Cast <DataRow>().First(dataRow => Equals(dataRow[TestName], test.TestName) && Equals(dataRow[MethodName], SerValue));

            Dispatcher.InvokeAsync(() =>
            {
                dataRow[serializer.Name] = RuningValue;
            });

            try
            {
                TObject  obj     = default;
                TSymbols symbols = default;

                double ns = default;

                var isTimeout = !Task.Run(() =>
                {
                    obj     = test.GetObject();
                    symbols = serializer.Serialize(obj);

                    var stopwatch = Stopwatch.StartNew();

                    serializer.Serialize(obj);

                    ns = stopwatch.ElapsedNanoseconds();
                }).Wait(MillisecondsTimeout);

                if (isTimeout)
                {
                    throw new TimeoutException();
                }

                long   count = 0;
                double total = 0;
                double min   = double.MaxValue;
                double max   = double.MinValue;

                for (int i = 0; i < 10; i++)
                {
                    Run(1);

                    Run((int)(10 / GetAvg()));
                    Run((int)(100 / GetAvg()));
                    Run((int)(1000 / GetAvg()));
                    Run((int)(10000 / GetAvg()));
                    Run((int)(100000 / GetAvg()));
                    Run((int)(1000000 / GetAvg()));
                    Run((int)(10000000 / GetAvg()));
                    Run((int)(100000000 / GetAvg()));
                    Run((int)(500000000 / GetAvg()));
                }

                double GetAvg()
                {
                    return(total / count);
                }

                void Run(int times)
                {
                    if (!(times > 0))
                    {
                        return;
                    }

                    TSymbols tSym = default;

                    var stopwatch = Stopwatch.StartNew();

                    for (int i = 0; i < times; i++)
                    {
                        tSym = serializer.Serialize(obj);
                    }

                    var ns = stopwatch.ElapsedNanoseconds();

                    total += ns;
                    count += times;

                    min = Math.Min(min, ns / times);
                    max = Math.Max(min, ns / times);

                    var tObj = serializer.Deserialize <TObject>(tSym);

                    if (!test.Equals(tObj, obj))
                    {
                        throw new IncorrectException();
                    }
                }

                result = new Result(GetAvg());
            }
            catch (Exception e)
            {
                result = new ExceptionResult(e);
            }
            finally
            {
                Dispatcher.InvokeAsync(() =>
                {
                    dataRow[serializer.Name] = result;
                });
            }
        }