private string CreateReport(string ret, List <string> orderedKeys)
        {
            foreach (string key in orderedKeys)
            {
                BenchmarkProfile profile = Profiles[key];
                ret += $"\t{key}: {profile.GetElapsedTime()} ({profile.NumBenchmarks} benchmarks, avg. {(profile.NumBenchmarks <= 0 ? 0f : (float)profile.GetElapsedTime().TotalMilliseconds / (float)profile.NumBenchmarks)})\n";
            }

            return(ret);
        }
        public BenchmarkProfile GetProfile(string id)
        {
            BenchmarkProfile profile = null;

            Profiles.TryGetValue(id, out profile);
            if (profile == null)
            {
                profile = new BenchmarkProfile(id);
                Profiles.Add(id, profile);
            }
            return(profile);
        }
        public string CreateReport()
        {
            var reportSb = new StringBuilder();

            reportSb.Append("=== BENCHMARK REPORT ===\n");
            reportSb.Append("=== ORDERED BY TOTAL TIME ===\n");

            var orderedKeys = new List <string>(Profiles.Keys);

            orderedKeys.Sort(
                (x, y) => {
                long xTicks = Profiles[x].GetElapsedTime().Ticks;
                long yTicks = Profiles[y].GetElapsedTime().Ticks;
                return(yTicks.CompareTo(xTicks));
            });

            CreateReport(reportSb, orderedKeys);
            reportSb.Append("\n=== ORDERED BY AVG. TIME ===\n");

            orderedKeys = new List <string>(Profiles.Keys);
            orderedKeys.Sort(
                (x, y) => {
                BenchmarkProfile xProfile = Profiles[x];
                BenchmarkProfile yProfile = Profiles[y];
                if (xProfile.NumBenchmarks <= 0 && yProfile.NumBenchmarks <= 0)
                {
                    return(0);
                }

                if (xProfile.NumBenchmarks > 0 && yProfile.NumBenchmarks <= 0)
                {
                    return(-1);
                }

                if (xProfile.NumBenchmarks <= 0 && yProfile.NumBenchmarks > 0)
                {
                    return(1);
                }

                float xAvg = (float)xProfile.GetElapsedTime().TotalMilliseconds /
                             xProfile.NumBenchmarks;
                float yAvg = (float)yProfile.GetElapsedTime().TotalMilliseconds /
                             yProfile.NumBenchmarks;
                return(yAvg.CompareTo(xAvg));
            });

            CreateReport(reportSb, orderedKeys);
            return(reportSb.ToString());
        }
 /// <summary>
 /// Adds ordered report lines to the stringbuilder
 /// </summary>
 /// <param name="reportSb">StringBuilder where report lines are added</param>
 /// <param name="orderedKeys">The data</param>
 private void CreateReport(StringBuilder reportSb, List <string> orderedKeys)
 {
     foreach (string key in orderedKeys)
     {
         BenchmarkProfile profile = Profiles[key];
         reportSb.AppendFormat(
             "\t{0}: {1} ({2} benchmarks, avg. {3})\n",
             key,
             profile.GetElapsedTime(),
             profile.NumBenchmarks,
             profile.NumBenchmarks <= 0
                     ? 0f
                     : (float)profile.GetElapsedTime().TotalMilliseconds /
             (float)profile.NumBenchmarks);
     }
 }
Ejemplo n.º 5
0
        public Benchmark(string id = null, string postfix = null)
        {
            if (id == null)
            {
                StackFrame frame  = new StackFrame(1);
                MethodBase method = frame.GetMethod();
                id = method.DeclaringType.Name + "#" + method.Name;
            }

            if (postfix != null)
            {
                id += "#" + postfix;
            }

            profile = BenchmarkProfileProvider.Instance.GetProfile(id);
            profile.Start();
        }
        public string CreateReport()
        {
            string ret = "=== BENCHMARK REPORT ===\n";

            ret += "=== ORDERED BY TOTAL TIME ===\n";
            List <string> orderedKeys = new List <string>(Profiles.Keys);

            orderedKeys.Sort(delegate(string x, string y) {
                long xTicks = Profiles[x].GetElapsedTime().Ticks;
                long yTicks = Profiles[y].GetElapsedTime().Ticks;
                return(yTicks.CompareTo(xTicks));
            });
            ret = CreateReport(ret, orderedKeys);

            ret        += "\n=== ORDERED BY AVG. TIME ===\n";
            orderedKeys = new List <string>(Profiles.Keys);
            orderedKeys.Sort(delegate(string x, string y) {
                BenchmarkProfile xProfile = Profiles[x];
                BenchmarkProfile yProfile = Profiles[y];
                if (xProfile.NumBenchmarks <= 0 && yProfile.NumBenchmarks <= 0)
                {
                    return(0);
                }
                else if (xProfile.NumBenchmarks > 0 && yProfile.NumBenchmarks <= 0)
                {
                    return(-1);
                }
                else if (xProfile.NumBenchmarks <= 0 && yProfile.NumBenchmarks > 0)
                {
                    return(1);
                }
                else
                {
                    float xAvg = (float)xProfile.GetElapsedTime().TotalMilliseconds / (float)xProfile.NumBenchmarks;
                    float yAvg = (float)yProfile.GetElapsedTime().TotalMilliseconds / (float)yProfile.NumBenchmarks;
                    return(yAvg.CompareTo(xAvg));
                }
            });

            ret = CreateReport(ret, orderedKeys);
            return(ret);
        }