2014-12-15 22:58:49 +08:00
|
|
|
|
using System;
|
2016-04-24 07:37:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
2015-11-02 21:43:09 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.Infrastructure
|
|
|
|
|
{
|
2015-11-05 05:49:36 +08:00
|
|
|
|
public static class Stopwatch
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2016-04-24 07:37:25 +08:00
|
|
|
|
private static readonly Dictionary<string, long> Count = new Dictionary<string, long>();
|
|
|
|
|
private static readonly object Locker = new object();
|
2015-11-05 05:35:04 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This stopwatch will appear only in Debug mode
|
|
|
|
|
/// </summary>
|
2015-11-05 05:49:36 +08:00
|
|
|
|
public static void Debug(string name, Action action)
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2015-11-05 05:35:04 +08:00
|
|
|
|
#if DEBUG
|
2015-11-05 05:49:36 +08:00
|
|
|
|
Normal(name, action);
|
2015-11-05 05:35:04 +08:00
|
|
|
|
#else
|
|
|
|
|
action();
|
|
|
|
|
#endif
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-05 05:49:36 +08:00
|
|
|
|
public static long Normal(string name, Action action)
|
2014-12-15 22:58:49 +08:00
|
|
|
|
{
|
2015-11-05 05:49:36 +08:00
|
|
|
|
var stopWatch = new System.Diagnostics.Stopwatch();
|
2015-11-05 05:35:04 +08:00
|
|
|
|
stopWatch.Start();
|
|
|
|
|
action();
|
|
|
|
|
stopWatch.Stop();
|
|
|
|
|
var milliseconds = stopWatch.ElapsedMilliseconds;
|
2015-11-08 01:32:58 +08:00
|
|
|
|
string info = $"{name} : {milliseconds}ms";
|
|
|
|
|
Log.Debug(info);
|
2015-11-05 05:35:04 +08:00
|
|
|
|
return milliseconds;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
2015-11-05 05:35:04 +08:00
|
|
|
|
|
2016-04-24 07:37:25 +08:00
|
|
|
|
public static void StartCount(string name, Action action)
|
|
|
|
|
{
|
|
|
|
|
var stopWatch = new System.Diagnostics.Stopwatch();
|
|
|
|
|
stopWatch.Start();
|
|
|
|
|
action();
|
|
|
|
|
stopWatch.Stop();
|
|
|
|
|
var milliseconds = stopWatch.ElapsedMilliseconds;
|
|
|
|
|
lock (Locker)
|
|
|
|
|
{
|
|
|
|
|
if (Count.ContainsKey(name))
|
|
|
|
|
{
|
|
|
|
|
Count[name] += milliseconds;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Count[name] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void EndCount()
|
|
|
|
|
{
|
|
|
|
|
foreach (var key in Count.Keys)
|
|
|
|
|
{
|
|
|
|
|
string info = $"{key} already cost {Count[key]}ms";
|
|
|
|
|
Log.Debug(info);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|