Esempio n. 1
0
 public static double FinalMark(this Benchmarker self)
 {
     if (self == null)
     {
         return(0);
     }
     if (!self.EnabledInProduction && !FiTechCoreExtensions.EnableBenchMarkers)
     {
         return(0);
     }
     if (!self.Active)
     {
         return(0);
     }
     try {
         var initMark = self.marks[0];
         if (self.marks.Count < 2)
         {
             self.marks.Add(new TimeMark(initMark.Timestamp, self.myName));
         }
         var lastMark = self.marks[self.marks.Count - 1];
         lastMark.SetDuration(DateTime.UtcNow);
         var retv = (self.marks[self.marks.Count - 1].Timestamp - self.marks[0].Timestamp).TotalMilliseconds;
         if (self.WriteToStdout)
         {
             lock ("BENCHMARKER BM_WRITE") {
                 self.VerboseLog().ForEach(Console.WriteLine);
             }
         }
         return(retv);
     } catch (Exception) { }
     return(0);
 }
Esempio n. 2
0
 public static void Assert(this Benchmarker self, Expression <Func <bool> > expr)
 {
     if (self == null)
     {
         return;
     }
     if (!self.EnabledInProduction && !FiTechCoreExtensions.EnableBenchMarkers)
     {
         return;
     }
     try {
         bool result = expr.Compile().Invoke();
         if (!result && Debugger.IsAttached)
         {
             self.Mark($"Assert Failed {expr.ToString()} => {result}");
             Debugger.Break();
         }
     } catch (Exception x) {
         self.Mark($"Assert {expr.ToString()} => Exception", x);
         if (Debugger.IsAttached)
         {
             Debugger.Break();
         }
     }
 }
Esempio n. 3
0
 public static void Wrap(this Benchmarker self, string label, Action action)
 {
     try {
         self.Mark($"[RegionStart] {label}");
         action.Invoke();
         self.Mark($"[RegionEndOk] {label}");
     }
     catch (Exception x) {
         self.Mark($"[RegionException] {label}", x);
         throw new Exception($"Error in Region {label}", x);
     }
 }
Esempio n. 4
0
        public static IEnumerable <String> VerboseLog(this Benchmarker self)
        {
            if (self == null)
            {
                yield break;
            }
            if (!self.EnabledInProduction && !FiTechCoreExtensions.EnableBenchMarkers)
            {
                yield break;
            }

            var lines = new List <String>();

            var retv = TotalTime(self);

            yield return($"--------------------------");

            yield return($"{self.myName}");

            yield return($"--------------------------");

            yield return($" | 0ms ");

            for (int i = 0; i < self.marks.Count - 1; i++)
            {
                yield return($" | [{i}] {self.marks[i].Name} +[{self.marks[i].Duration.TotalMilliseconds}ms]");

                if (self.marks[i].Exception != null)
                {
                    var ex = self.marks[i].Exception;
                    do
                    {
                        yield return($" | [{i}] => Thrown {self.marks[i].Exception.Message}");

                        yield return($" | [{i}] => StackTrace {self.marks[i].Exception.StackTrace}");

                        ex = ex.InnerException;
                    } while (ex != null);
                }
            }
            yield return($" v {retv}ms");

            yield return($"--------------------------");

            yield return($"Total: {retv}ms");
        }
Esempio n. 5
0
        public static async Task <T> Wrap <T>(this Benchmarker self, string label, Func <Task <T> > action)
        {
            T retv;

            try {
                self.Mark($"[RegionStart] {label}");
                retv = await action.Invoke();

                self.Mark($"[RegionEndOk] {label}");
            }
            catch (Exception x) {
                self.Mark($"[RegionException] {label}", x);
                retv = default(T);
                throw x;
            }

            return(retv);
        }
Esempio n. 6
0
        public static T Wrap <T>(this Benchmarker self, string label, Func <T> action)
        {
            if (self == null)
            {
                return(default(T));
            }
            T retv;

            try {
                self.Mark($"[RegionStart] {label}");
                retv = action.Invoke();
                self.Mark($"[RegionEndOk] {label}");
            } catch (Exception x) {
                self.Mark($"[RegionException] {label}", x);
                throw new Exception($"Error in Region {label}", x);
            }

            return(retv);
        }
Esempio n. 7
0
        public static double Mark(this Benchmarker self, String txt, Exception ex = null)
        {
            if (self == null)
            {
                return(0);
            }
            if (!self.EnabledInProduction && !FiTechCoreExtensions.EnableBenchMarkers)
            {
                return(0);
            }
            if (!self.Active)
            {
                return(0);
            }
            try {
                if (self.marks.Count > 1)
                {
                    self.marks[self.marks.Count - 1].SetDuration(DateTime.UtcNow);
                }
                self.marks.Add(new TimeMark(self.marks[0].Timestamp, txt)
                {
                    Exception = ex
                });
                if (self.marks.Count < 2)
                {
                    return(0);
                }
                var retv = self.marks[self.marks.Count - 1]
                           .Timestamp.Subtract(self.marks[self.marks.Count - 2].Timestamp).TotalMilliseconds;

                if (FiTechCoreExtensions.EnableLiveBenchmarkerStdOut)
                {
                    var r2 = self.marks[self.marks.Count - 1]
                             .Timestamp.Subtract(self.marks[0].Timestamp).TotalMilliseconds;
                    Console.WriteLine($"{r2} {txt}");
                }
                return(retv);
            } catch (Exception) {
            }
            return(0);
        }
Esempio n. 8
0
 public static double TotalTime(this Benchmarker self)
 {
     return((self.marks[self.marks.Count - 1].Timestamp - self.marks[0].Timestamp).TotalMilliseconds);
 }