Replace all Stopwatch with Timeit

This commit is contained in:
bao-qian 2015-11-02 00:04:05 +00:00
parent 79fdba969b
commit bfa0db3ba3
3 changed files with 69 additions and 68 deletions

View file

@ -92,17 +92,16 @@ namespace Wox.Core.Plugin
PluginPair pair = pluginPair; PluginPair pair = pluginPair;
ThreadPool.QueueUserWorkItem(o => ThreadPool.QueueUserWorkItem(o =>
{ {
Stopwatch sw = new Stopwatch(); using (var time = new Timeit($"Plugin init: {pair.Metadata.Name}"))
sw.Start();
pair.Plugin.Init(new PluginInitContext
{ {
CurrentPluginMetadata = pair.Metadata, pair.Plugin.Init(new PluginInitContext
Proxy = HttpProxy.Instance, {
API = API CurrentPluginMetadata = pair.Metadata,
}); Proxy = HttpProxy.Instance,
sw.Stop(); API = API
Debug.WriteLine(string.Format("Plugin init:{0} - {1}", pair.Metadata.Name, sw.ElapsedMilliseconds)); });
pair.InitTime = sw.ElapsedMilliseconds; pair.InitTime = time.Current;
}
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair); InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
}); });
} }
@ -168,25 +167,15 @@ namespace Wox.Core.Plugin
{ {
try try
{ {
Stopwatch sw = new Stopwatch(); using (var time = new Timeit("Preload programs"))
sw.Start();
List<Result> results = pair.Plugin.Query(query) ?? new List<Result>();
results.ForEach(o =>
{ {
o.PluginID = pair.Metadata.ID; var results = pair.Plugin.Query(query) ?? new List<Result>();
}); results.ForEach(o => { o.PluginID = pair.Metadata.ID; });
sw.Stop(); var seconds = time.Current;
Debug.WriteLine(string.Format("Plugin query: {0} - {1}", pair.Metadata.Name, sw.ElapsedMilliseconds)); pair.QueryCount += 1;
pair.QueryCount += 1; pair.AvgQueryTime = pair.QueryCount == 1 ? seconds : (pair.AvgQueryTime + seconds) / 2;
if (pair.QueryCount == 1) API.PushResults(query, pair.Metadata, results);
{
pair.AvgQueryTime = sw.ElapsedMilliseconds;
} }
else
{
pair.AvgQueryTime = (pair.AvgQueryTime + sw.ElapsedMilliseconds) / 2;
}
API.PushResults(query, pair.Metadata, results);
} }
catch (System.Exception e) catch (System.Exception e)
{ {

View file

@ -1,23 +1,38 @@
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using Wox.Plugin;
namespace Wox.Infrastructure namespace Wox.Infrastructure
{ {
public class Timeit : IDisposable public class Timeit : IDisposable
{ {
private Stopwatch stopwatch = new Stopwatch(); private readonly Stopwatch _stopwatch = new Stopwatch();
private string name; private readonly string _name;
public Timeit(string name) public Timeit(string name)
{ {
this.name = name; _name = name;
stopwatch.Start(); _stopwatch.Start();
} }
public long Current
{
get
{
_stopwatch.Stop();
long seconds = _stopwatch.ElapsedMilliseconds;
_stopwatch.Start();
Debug.WriteLine(_name + ":" + _stopwatch.ElapsedMilliseconds + "ms");
return seconds;
}
}
public void Dispose() public void Dispose()
{ {
stopwatch.Stop(); _stopwatch.Stop();
Debug.WriteLine(name + ":" + stopwatch.ElapsedMilliseconds + "ms"); Debug.WriteLine(_name + ":" + _stopwatch.ElapsedMilliseconds + "ms");
} }
} }
} }

View file

@ -80,50 +80,47 @@ namespace Wox.ImageLoader
public static ImageSource Load(string path, bool addToCache = true) public static ImageSource Load(string path, bool addToCache = true)
{ {
Stopwatch sw = new Stopwatch(); using (new Timeit($"Loading image path: {path}"))
sw.Start();
if (string.IsNullOrEmpty(path)) return null;
if (addToCache)
{ {
ImageCacheStroage.Instance.Add(path); if (string.IsNullOrEmpty(path)) return null;
} if (addToCache)
ImageSource img = null;
if (imageCache.ContainsKey(path))
{
img = imageCache[path];
}
else
{
string ext = Path.GetExtension(path).ToLower();
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{ {
img = new BitmapImage(new Uri(path)); ImageCacheStroage.Instance.Add(path);
}
else if (selfExts.Contains(ext) && File.Exists(path))
{
img = GetIcon(path);
}
else if (!string.IsNullOrEmpty(path) && imageExts.Contains(ext) && File.Exists(path))
{
img = new BitmapImage(new Uri(path));
} }
ImageSource img = null;
if (img != null && addToCache) if (imageCache.ContainsKey(path))
{ {
if (!imageCache.ContainsKey(path)) img = imageCache[path];
}
else
{
string ext = Path.GetExtension(path).ToLower();
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{ {
imageCache.Add(path, img); img = new BitmapImage(new Uri(path));
}
else if (selfExts.Contains(ext) && File.Exists(path))
{
img = GetIcon(path);
}
else if (!string.IsNullOrEmpty(path) && imageExts.Contains(ext) && File.Exists(path))
{
img = new BitmapImage(new Uri(path));
}
if (img != null && addToCache)
{
if (!imageCache.ContainsKey(path))
{
imageCache.Add(path, img);
}
} }
} }
return img;
} }
sw.Stop();
Debug.WriteLine(string.Format("Loading image path: {0} - {1}ms",path,sw.ElapsedMilliseconds));
return img;
} }
// http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx // http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx