public Serialize() { _jsonSerializer = new CustomBonsaiSerializer(); _binarySerializer = new ExpressionSerializer(new BinaryObjectSerializer(), ExpressionFactory.Instance); _expr = (Expression <Func <IEnumerable <int> > >)(() => Enumerable.Range(0, 10).Where(x => x > 0).Select(x => x * x)); }
private static void Json() { GC.Collect(); Console.WriteLine("JSON"); var ser = new CustomBonsaiSerializer(); var N = 10000; var M = 10000; var e = (Expression <Func <IEnumerable <int> > >)(() => Enumerable.Range(0, 10).Where(x => x > 0).Select(x => x * x)); Console.WriteLine(" Serialize"); var sw = Stopwatch.StartNew(); var gc = GarbageCollectionWatch.StartNew(); var res = default(string); for (var i = 0; i < N; i++) { res = ser.Serialize(e.ToExpressionSlim()); } Console.WriteLine(" Elapsed (ms): " + sw.Elapsed.TotalMilliseconds / M); Console.WriteLine(" " + gc.Elapsed); Console.WriteLine(" Length (bytes): " + res.Length * 2); Console.WriteLine(" Deserialize"); sw.Restart(); gc.Restart(); for (var i = 0; i < M; i++) { e = (Expression <Func <IEnumerable <int> > >)ser.Deserialize(res).ToExpression(); } Console.WriteLine(" Elapsed (ms): " + sw.Elapsed.TotalMilliseconds / M); Console.WriteLine(" " + gc.Elapsed); Console.WriteLine(); }
private static void Size() { var serj = new CustomBonsaiSerializer(); var serb = new ExpressionSerializer(new BinaryObjectSerializer(), ExpressionFactory.Instance); var es = new Expression[] { Expression.Default(typeof(int)), Expression.Empty(), Expression.Parameter(typeof(int)), Expression.Parameter(typeof(int), "a"), Expression.Parameter(typeof(int), "ab"), Expression.Parameter(typeof(int), "abc"), ((Expression <Func <DateTime> >)(() => DateTime.Now)).Body, ((Expression <Func <string, int> >)(s => s.Length)).Body, ((Expression <Func <TimeSpan> >)(() => new TimeSpan())).Body, ((Expression <Func <long, DateTime> >)(t => new DateTime(t))).Body, ((Expression <Func <List <int> > >)(() => new List <int>())).Body, ((Expression <Func <int, int[]> >)(x => new int[x])).Body, ((Expression <Func <int, int, int[, ]> >)((x, y) => new int[x, y])).Body, #pragma warning disable IDE0079 // Next supression flagged as redundant on .NET SDK 6 #pragma warning disable CA1825 // Avoid zero-length array allocations (use in expression tree) ((Expression <Func <int[]> >)(() => new int[] {})).Body, #pragma warning restore CA1825 // Avoid zero-length array allocations #pragma warning restore IDE0079 ((Expression <Func <int, int[]> >)(x => new int[] { x })).Body, ((Expression <Func <int, int, int[]> >)((x, y) => new int[] { x, y })).Body, ((Expression <Func <string> >)(() => Console.ReadLine())).Body, ((Expression <Func <string, string> >)(s => s.ToUpper())).Body, #pragma warning disable IDE0057 // Use range operator (https://github.com/dotnet/roslyn/issues/49347) ((Expression <Func <string, int, string> >)((s, i) => s.Substring(i))).Body, #pragma warning restore IDE0057 // Use range operator ((Expression <Func <string, int, int, string> >)((s, i, j) => s.Substring(i, j))).Body, ((Expression <Func <int, int> >)(x => - x)).Body, ((Expression <Func <int, int> >)(x => ~x)).Body, ((Expression <Func <bool, bool> >)(x => !x)).Body, ((Expression <Func <TimeSpan, TimeSpan> >)(t => - t)).Body, ((Expression <Func <int, int, int> >)((x, y) => x + y)).Body, ((Expression <Func <bool, bool, bool> >)((x, y) => x && y)).Body, ((Expression <Func <string, string, string> >)((x, y) => x ?? y)).Body, ((Expression <Func <bool, string, string, string> >)((b, x, y) => b ? x : y)).Body, ((Expression <Func <int, List <int> > >)(x => new List <int> { x })).Body, ((Expression <Func <int, int, List <int> > >)((x, y) => new List <int> { x, y })).Body, //(Expression<Func<AppDomainSetup>>)(() => new AppDomainSetup { }), //(Expression<Func<string, AppDomainSetup>>)(s => new AppDomainSetup { ApplicationName = s }), (Expression <Func <int, int, int, int, Bar> >)((x, y, z, a) => new Bar { X = x, Y = { A = a }, Zs = { y, z } }), (Expression <Func <Func <int, int>, int, int> >)((f, x) => f(x)), (Expression <Func <int, int> >)(x => x), ((Expression <Func <int, int, IEnumerable <int> > >)((x, y) => Enumerable.Range(x, y))).Body, ((Expression <Func <int, int, IEnumerable <int> > >)((x, y) => Enumerable.Range(x, y).Where(i => true))).Body, ((Expression <Func <int, int, IEnumerable <int> > >)((x, y) => Enumerable.Range(x, y).Where(i => true).Select(i => i))).Body, ((Expression <Func <int, int, IQueryable <int> > >)((x, y) => Enumerable.Range(x, y).AsQueryable())).Body, ((Expression <Func <int, int, IQueryable <int> > >)((x, y) => Enumerable.Range(x, y).AsQueryable().Where(i => true))).Body, ((Expression <Func <int, int, IQueryable <int> > >)((x, y) => Enumerable.Range(x, y).AsQueryable().Where(i => true).Select(i => i))).Body, }; foreach (var e in es) { var slim = e.ToExpressionSlim(); var bj = Encoding.UTF8.GetBytes(serj.Serialize(slim)); #if USE_SLIM var bb = serb.Serialize(slim); serb.Deserialize(bb); #else var bb = serb.Serialize(e); #endif Console.WriteLine(bj.Length + "\t" + bb.Length + "\t" + (double)bb.Length / bj.Length + "\t" + e.ToString()); } }