Example #1
0
        static void Test(String name, ObjectPool <T> pool, Int32 max, Int32 times)
        {
            var     list  = new List <T>(max);
            Boolean isPop = true;

            CodeTimer.TimeLine(name, times, index =>
            {
                if (isPop && pool.StockCount <= 0)
                {
                    isPop = false;
                }
                else if (!isPop && list.Count <= 0)
                {
                    isPop = true;
                }

                if (isPop)
                {
                    list.Add(pool.Pop());
                }
                else
                {
                    var p = list.Count - 1;
                    var e = list[list.Count - 1];
                    list.RemoveAt(p);
                    pool.Push(e);
                }
            });
            // 清账
            foreach (var item in list)
            {
                pool.Push(item);
            }
        }
Example #2
0
        static void Test2(String name, ObjectPool <T> pool, Int32 max, Int32 times, Boolean[] rs)
        {
            var   list    = new List <T>(max);
            Int32 pcount1 = 0;
            Int32 pcount2 = 0;
            var   ri      = 0;

            CodeTimer.TimeLine(name, times, index =>
            {
                if (ri >= rs.Length)
                {
                    ri = 0;
                }
                if (list.Count < 1 || list.Count < max && rs[ri++])
                {
                    list.Add(pool.Pop());
                    pcount1++;
                }
                else
                {
                    var p = list.Count - 1;
                    var e = list[list.Count - 1];
                    list.RemoveAt(p);
                    pool.Push(e);
                    pcount2++;
                }
            });
            foreach (var item in list)
            {
                pool.Push(item);
            }

            //Console.WriteLine("{2} 借:{0,8:n0} 还:{1,8:n0}", pcount1, pcount2, name);
        }
Example #3
0
        static void Test(IJsonProvider provider, object obj)
        {
            string json   = "";
            object newObj = null;

            CodeTimer.TimeLine(provider.GetType().Name + " - Serialize", 10 * 10000, i => json     = provider.Serialize(obj));
            CodeTimer.TimeLine(provider.GetType().Name + " - Deserialize", 10 * 10000, i => newObj = provider.Deserialize(json, obj.GetType()));
        }
Example #4
0
        static void Test4()
        {
            XTrace.WriteLine("111");

            var count = 10000000;

            CodeTimer.ShowHeader("上下文切换性能测试");
            CodeTimer.TimeLine("QueueUserWorkItem", count, n => ThreadPool.QueueUserWorkItem(s => Thread.Sleep(1000)));
            CodeTimer.TimeLine("UnsafeQueueUserWorkItem", count, n => ThreadPool.UnsafeQueueUserWorkItem(s => Thread.Sleep(1000), null));
        }
Example #5
0
        static void Main()
        {
            var       container = ObjectFactory.CreateContainer();
            var       bus       = new CommandBus(container);
            string    s         = null;
            const int testCount = 10 * 10000;

            for (int i = 0; i < 5; i++)
            {
                CodeTimer.TimeLine("Call", testCount, x => s    = bus.Call(new TestCommand()));
                CodeTimer.TimeLine("Execute", testCount, x => s = bus.Execute(new TestCommand()).Result);
                Console.WriteLine();
            }
            Console.ReadLine();
        }
Example #6
0
        static void Test7()
        {
            //Console.Write("请输入表达式:");
            //var code = Console.ReadLine();

            //var rs = ScriptEngine.Execute(code, new Dictionary<String, Object> { { "a", 222 }, { "b", 333 } });
            ////Console.WriteLine(rs);

            //var se = ScriptEngine.Create(code);
            //var fm = code.Replace("a", "{0}").Replace("b", "{1}");
            //for (int i = 1; i <= 9; i++)
            //{
            //    for (int j = 1; j <= i; j++)
            //    {
            //        Console.Write(fm + "={2}\t", j, i, se.Invoke(i, j));
            //    }
            //    Console.WriteLine();
            //}

            var se = ScriptEngine.Create("Test.Program.TestMath(k)");

            if (se.Method == null)
            {
                se.Parameters.Add("k", typeof(Double));
                se.Compile();
            }

            var fun = (DM)(Object)Delegate.CreateDelegate(typeof(DM), se.Method as MethodInfo);

            var timer = 1000000;
            var k     = 123;

            CodeTimer.ShowHeader();
            CodeTimer.TimeLine("原生", timer, n => TestMath(k));
            CodeTimer.TimeLine("动态", timer, n => se.Invoke(k));
            CodeTimer.TimeLine("动态2", timer, n => fun(k));
        }
Example #7
0
        /// <summary>开始测试</summary>
        public static void Start()
        {
            // 提升进程优先级
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;

            // 为所有Json宿主创建实例
            var hosts = typeof(IJsonHost).GetAllSubclasses().Select(e => e.CreateInstance() as IJsonHost).ToArray();

            Console.Clear();

            for (int i = 0; i < 2; i++)
            {
                var obj = Create(i > 0);

                var json = "";
                Console.WriteLine("{0} Json序列化大小", i == 0 ? "普通" : "高级");
                foreach (var item in hosts)
                {
                    json = item.Write(obj, true);
                    Console.WriteLine("{0}\t大小:{1:n0}", item.GetType().Name, json.GetBytes().Length);
                    Console.WriteLine(json);

                    item.Read(json, obj.GetType());
                }
                {
                    var bn = new BinaryFormatter();
                    var ms = new MemoryStream();
                    bn.Serialize(ms, obj);
                    Console.WriteLine("{0}\t大小:{1:n0}", bn.GetType().Name, ms.Length);
                }
                {
                    var bn = new Binary();
                    bn.Write(obj);
                    Console.WriteLine("{0}\t大小:{1:n0}", bn.GetType().Name, bn.Stream.Length);
                }

                CodeTimer.ShowHeader("Json序列化性能测试");
                foreach (var item in hosts)
                {
                    CodeTimer.TimeLine(item.GetType().Name, 100000, n => { item.Write(obj); });
                }
                CodeTimer.TimeLine("BinaryFormatter", 100000, n =>
                {
                    var bn = new BinaryFormatter();
                    var ms = new MemoryStream();
                    bn.Serialize(ms, obj);
                });
                CodeTimer.TimeLine("Binary", 100000, n =>
                {
                    var bn = new Binary();
                    bn.Write(obj);
                });

                Console.WriteLine();
                CodeTimer.ShowHeader("Json反序列化性能测试");
                foreach (var item in hosts)
                {
                    CodeTimer.TimeLine(item.GetType().Name, 100000, n => { item.Read(json, obj.GetType()); });
                }
                CodeTimer.TimeLine("JsonParser", 100000, n =>
                {
                    var jp = new JsonParser(json);
                    jp.Decode();
                });
            }
        }
Example #8
0
        /// <summary>开始测试</summary>
        public static void Start()
        {
            // 提升进程优先级
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.Idle;

            // 为所有Binary宿主创建实例
            Console.Clear();

            var ks = new String[] { "普通", "高级", "名值普通", "名值高级" };

            for (int i = 0; i < 4; i++)
            {
                var ext = i == 1 || i == 3;
                var unm = i == 2 || i == 3;
                var obj = Create(ext);

                Byte[] buf = null;
                Byte[] bts = null;
                Console.WriteLine("{0} 序列化", ks[i]);
                {
                    var bn = new Binary();
                    //bn.Log = XTrace.Log;
                    //bn.EnableTrace();
                    if (!ext)
                    {
                        SetExt(bn);
                    }
                    if (unm)
                    {
                        bn.AddHandler <BinaryPair>();
                    }
                    bn.Write(obj);

                    buf = bn.GetBytes();
                    Console.Write("{0}\t大小:{1:n0}\t", bn.GetType().Name, bn.Stream.Length);
                    bn.Stream = new MemoryStream(buf);
                    //bn.EnableTrace();
                    bn.Read(obj.GetType());
                }
                {
                    var bn = new BinaryFormatter();
                    var ms = new MemoryStream();
                    bn.Serialize(ms, obj);

                    bts = ms.ToArray();
                    Console.WriteLine("{0}\t大小:{1:n0}", bn.GetType().Name, ms.Length);
                }

                CodeTimer.ShowHeader("序列化");
                CodeTimer.TimeLine("Binary", 100000, n =>
                {
                    var bn = new Binary();
                    if (!ext)
                    {
                        SetExt(bn);
                    }
                    if (unm)
                    {
                        bn.AddHandler <BinaryPair>();
                    }
                    bn.Write(obj);
                });
                CodeTimer.TimeLine("BinaryFormatter", 100000, n =>
                {
                    var bn = new BinaryFormatter();
                    var ms = new MemoryStream();
                    bn.Serialize(ms, obj);
                });

                Console.WriteLine();
                CodeTimer.ShowHeader("反序列化");
                CodeTimer.TimeLine("Binary", 100000, n =>
                {
                    var bn = new Binary();
                    if (!ext)
                    {
                        SetExt(bn);
                    }
                    if (unm)
                    {
                        bn.AddHandler <BinaryPair>();
                    }
                    bn.Stream = new MemoryStream(buf);
                    bn.Read(obj.GetType());
                });
                CodeTimer.TimeLine("BinaryFormatter", 100000, n =>
                {
                    var bn = new BinaryFormatter();
                    var ms = new MemoryStream(bts);
                    bn.Deserialize(ms);
                });

                Console.WriteLine();
            }
        }