-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perf improvement: use a model that batch calls to RefreshObjects in t…
…he view, use FastObjectListView
- Loading branch information
Showing
6 changed files
with
116 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Nats.Services.KeyValueStoreDemo/StoreClient/StoreClientForm.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
Nats.Services.KeyValueStoreDemo/StoreClient/StoreClientModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using BrightIdeasSoftware; | ||
using Nats.Services.Core.KeyValueStoreService; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Windows.Forms; | ||
|
||
namespace StoreClient | ||
{ | ||
public class StoreClientModel<T_Key, T_Value, T_RowItem> | ||
where T_Value : IKeyIdentifiable<T_Key> | ||
where T_RowItem : class, StoreRowItem<T_Value>, new() | ||
{ | ||
IKeyValueStoreService<T_Key, T_Value> service; | ||
|
||
Dictionary<T_Key, T_RowItem> dicoKeyRows = new Dictionary<T_Key, T_RowItem>(); | ||
Dictionary<T_Key, T_RowItem> updatedRows = new Dictionary<T_Key, T_RowItem>(); | ||
Timer timer; | ||
ObjectListView view; | ||
|
||
public async Task InitAsync(IKeyValueStoreService<T_Key, T_Value> service, ObjectListView view) | ||
{ | ||
this.service = service; | ||
this.view = view; | ||
|
||
var items= await Task.Run(() => service.GetAllValues()); | ||
foreach(var item in items) | ||
{ | ||
var rowItem = new T_RowItem(); | ||
rowItem.Update(item); | ||
dicoKeyRows[item.Key] = rowItem; | ||
} | ||
Generator.GenerateColumns(view, typeof(T_RowItem)); | ||
|
||
view.Objects = dicoKeyRows.Values; | ||
timer = new Timer(); | ||
timer.Tick += UpdateBatch; | ||
timer.Start(); | ||
service.ValueUpdated += OnValueUpdated; | ||
} | ||
|
||
private void UpdateBatch(object sender, EventArgs evt) | ||
{ | ||
List<T_RowItem> values; | ||
lock (updatedRows) | ||
{ | ||
if(updatedRows.Count == 0) | ||
{ | ||
return; | ||
} | ||
values = updatedRows.Values.ToList(); | ||
updatedRows.Clear(); | ||
} | ||
view.Invoke( (Action)(() => RefreshRows(values))); | ||
} | ||
|
||
Stopwatch sw = new Stopwatch(); | ||
long n = 0; | ||
private void RefreshRows(List<T_RowItem> values) | ||
{ | ||
n++; | ||
sw.Start(); | ||
view.RefreshObjects(values); | ||
sw.Stop(); | ||
if( n % 10 == 0) | ||
{ | ||
Debug.WriteLine("n: " + n + ", t=" + sw.ElapsedMilliseconds + ", avg: " + (sw.ElapsedMilliseconds/ (double)n)); | ||
} | ||
} | ||
|
||
private void OnValueUpdated(T_Value obj) | ||
{ | ||
lock(updatedRows) | ||
{ | ||
T_RowItem rowItem; | ||
if(! dicoKeyRows.TryGetValue(obj.Key, out rowItem)) | ||
{ | ||
rowItem = new T_RowItem(); | ||
dicoKeyRows[obj.Key] = rowItem; | ||
} | ||
|
||
rowItem.Update(obj); | ||
updatedRows[obj.Key] = rowItem; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace StoreClient | ||
{ | ||
public interface StoreRowItem<T_Value> | ||
{ | ||
void Update(T_Value value); | ||
} | ||
} |