From a22ca5b57ff30ae92350c84edb9d9034614e1a47 Mon Sep 17 00:00:00 2001 From: bao-qian Date: Wed, 30 Nov 2016 00:31:31 +0000 Subject: [PATCH] refactoring stopwatch --- Plugins/Wox.Plugin.Program/Main.cs | 4 ++-- Wox.Core/Plugin/PluginManager.cs | 2 +- Wox.Infrastructure/Image/ImageLoader.cs | 2 +- Wox.Infrastructure/Stopwatch.cs | 15 +++++++++------ 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index 045e42effb..fe420dc9f4 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -29,14 +29,14 @@ namespace Wox.Plugin.Program _settingsStorage = new PluginJsonStorage(); _settings = _settingsStorage.Load(); - Stopwatch.Debug("Preload programs", () => + Stopwatch.Normal("Preload programs", () => { _cacheStorage = new BinaryStorage(); _cache = _cacheStorage.Load(); _win32s = _cache.Programs; }); Log.Info($"Preload {_win32s.Length} programs from cache"); - Stopwatch.Debug("Program Index", IndexPrograms); + Stopwatch.Normal("Program Index", IndexPrograms); } public void Save() diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 579d972196..1050cf1157 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -176,7 +176,7 @@ namespace Wox.Core.Plugin try { var metadata = pair.Metadata; - var milliseconds = Stopwatch.Normal($"Plugin.Query cost for {metadata.Name}", () => + var milliseconds = Stopwatch.Debug($"Plugin.Query cost for {metadata.Name}", () => { results = pair.Plugin.Query(query) ?? results; UpdatePluginMetadata(results, metadata, query); diff --git a/Wox.Infrastructure/Image/ImageLoader.cs b/Wox.Infrastructure/Image/ImageLoader.cs index 0743b647d6..00e7044513 100644 --- a/Wox.Infrastructure/Image/ImageLoader.cs +++ b/Wox.Infrastructure/Image/ImageLoader.cs @@ -92,7 +92,7 @@ namespace Wox.Infrastructure.Image } Task.Run(() => { - Stopwatch.Debug("Preload images from cache", () => + Stopwatch.Normal("Preload images from cache", () => { _cache.TopUsedImages.AsParallel().Where(i => !ImageSources.ContainsKey(i.Key)).ForAll(i => { diff --git a/Wox.Infrastructure/Stopwatch.cs b/Wox.Infrastructure/Stopwatch.cs index a0009f8a65..8ce1ca36f0 100644 --- a/Wox.Infrastructure/Stopwatch.cs +++ b/Wox.Infrastructure/Stopwatch.cs @@ -11,13 +11,16 @@ namespace Wox.Infrastructure /// /// This stopwatch will appear only in Debug mode /// - public static void Debug(string name, Action action) + public static long Debug(string name, Action action) { -#if DEBUG - Normal(name, action); -#else + var stopWatch = new System.Diagnostics.Stopwatch(); + stopWatch.Start(); action(); -#endif + stopWatch.Stop(); + var milliseconds = stopWatch.ElapsedMilliseconds; + string info = $"{name} : {milliseconds}ms"; + Log.Debug(info); + return milliseconds; } public static long Normal(string name, Action action) @@ -28,7 +31,7 @@ namespace Wox.Infrastructure stopWatch.Stop(); var milliseconds = stopWatch.ElapsedMilliseconds; string info = $"{name} : {milliseconds}ms"; - Log.Debug(info); + Log.Info(info); return milliseconds; }