Skip to content

Commit b8aa00d

Browse files
author
Sergey
committed
Work with LiteDB has been improved
1 parent 409a353 commit b8aa00d

File tree

12 files changed

+66
-57
lines changed

12 files changed

+66
-57
lines changed

JMeterTests/StressTest_PollCounters_TestService.jmx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
<collectionProp name="Arguments.arguments">
1616
<elementProp name="host_url" elementType="Argument">
1717
<stringProp name="Argument.name">host_url</stringProp>
18-
<stringProp name="Argument.value">perfon.1gb.ru</stringProp>
18+
<stringProp name="Argument.value">localhost</stringProp>
1919
<stringProp name="Argument.metadata">=</stringProp>
2020
</elementProp>
2121
<elementProp name="host_port" elementType="Argument">
2222
<stringProp name="Argument.name">host_port</stringProp>
23-
<stringProp name="Argument.value">80</stringProp>
23+
<stringProp name="Argument.value">8010</stringProp>
2424
<stringProp name="Argument.metadata">=</stringProp>
2525
</elementProp>
2626
<elementProp name="api_prefix" elementType="Argument">

Perfon.Core/PerfCounterStorages/IPerfomanceCountersStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IPerfomanceCountersStorage
2525
/// <param name="counterName"></param>
2626
/// <param name="date"></param>
2727
/// <returns></returns>
28-
Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null);
28+
Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null, int skip = 0);
2929

3030
/// <summary>
3131
/// Get list of names of all perf counters in the storage

Perfon.Core/PerfCounterStorages/PerfCounterCSVFileStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task StorePerfCounters(IEnumerable<IPerfCounterData> counters)
7272
}
7373
}
7474

75-
public Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null)
75+
public Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null, int skip=0)
7676
{
7777
var list = new List<PerfCounterValue>();
7878

Perfon.Core/PerfCounterStorages/PerfCounterInMemoryCacheStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public async Task StorePerfCounters(IEnumerable<IPerfCounterData> counters)
101101
}
102102
}
103103

104-
public Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null)
104+
public Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null, int skip = 0)
105105
{
106106
if (!date.HasValue)
107107
{

Perfon.Core/PerfCounterStorages/PerfCounterLiteDbStorage.cs

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,37 @@ public Task StorePerfCounters(IEnumerable<IPerfCounterData> counters)
4545

4646
using (var db = new LiteDatabase(PathToDbFolder + dbName))
4747
{
48-
// Get customer collection
49-
foreach (var counter in counters)
48+
using (var trans = db.BeginTrans())
5049
{
51-
try
50+
// Get customer collection
51+
foreach (var counter in counters)
5252
{
53-
var countersColl = db.GetCollection<PerfCounterValue>(counter.Name.GetHashCode().ToString());
54-
var names = db.GetCollection("CounterNames");
55-
56-
// Index document using a document property
57-
//countersColl.EnsureIndex("Timestamp", true);
58-
59-
var id = names.Find(Query.EQ("Name", counter.Name)).FirstOrDefault();
60-
if (id == null)
53+
try
6154
{
62-
var doc = new BsonDocument();
63-
doc.Add("Name", counter.Name);
64-
names.Insert(doc);
65-
}
55+
var countersColl = db.GetCollection<PerfCounterValue>(counter.Name.GetHashCode().ToString());
56+
var names = db.GetCollection("CounterNames");
6657

67-
var item = new PerfCounterValue(now, counter.Value);
58+
var id = names.Find(Query.EQ("Name", counter.Name)).FirstOrDefault();
59+
if (id == null)
60+
{
61+
var doc = new BsonDocument();
62+
doc.Add("Name", counter.Name);
63+
names.Insert(doc);
64+
}
6865

69-
countersColl.Insert(item);
70-
}
71-
catch (Exception exc)
72-
{
73-
if (OnError != null)
66+
var item = new PerfCounterValue(now, counter.Value);
67+
68+
countersColl.Insert(item);
69+
}
70+
catch (Exception exc)
7471
{
75-
OnError(new object(), new ErrorEventArgs(exc.ToString()));
72+
if (OnError != null)
73+
{
74+
OnError(new object(), new ErrorEventArgs(exc.ToString()));
75+
}
7676
}
7777
}
78+
trans.Commit();
7879
}
7980
}
8081
}
@@ -89,7 +90,7 @@ public Task StorePerfCounters(IEnumerable<IPerfCounterData> counters)
8990
return Task.Delay(0);
9091
}
9192

92-
public Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null)
93+
public Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName, DateTime? date = null, int skip=0)
9394
{
9495
var list = new List<PerfCounterValue>();
9596

@@ -102,14 +103,15 @@ public Task<IEnumerable<PerfCounterValue>> QueryCounterValues(string counterName
102103

103104
try
104105
{
105-
var dbName = GetDbName(date.Value);
106-
107-
using (var db = new LiteDatabase(PathToDbFolder+dbName))
106+
using (var db = new LiteDatabase(GetDbReadOnlyName(date.Value)))
108107
{
109-
var countersColl = db.GetCollection<PerfCounterValue>(counterName.GetHashCode().ToString());
110-
111-
list = countersColl.FindAll().Where(a => a.Timestamp.Date == date).ToList();
108+
list = db.Engine.Find(counterName.GetHashCode().ToString(), Query.GTE("Timestamp", new BsonValue(date)), skip).Select(a =>
109+
{
110+
return new PerfCounterValue(a["Timestamp"].AsDateTime, (float)a["Value"].AsDouble);
111+
}).ToList();
112112

113+
//var countersColl = db.GetCollection<PerfCounterValue>(counterName.GetHashCode().ToString());
114+
//list = countersColl.FindAll().Where(a => a.Timestamp.Date == date).ToList();
113115
}
114116
}
115117
catch (Exception exc)
@@ -135,14 +137,10 @@ public Task<IEnumerable<string>> GetCountersList()
135137

136138
using (var db = new LiteDatabase(PathToDbFolder + dbName))
137139
{
138-
// Get customer collection
139-
var names = db.GetCollection("CounterNames");
140-
141-
// Index document using a document property
142-
//countersColl.EnsureIndex("Timestamp", true);
143-
144-
res = names.FindAll().Select(a => a["Name"].AsString).ToList();
140+
//var names = db.GetCollection("CounterNames");
141+
//res = names.FindAll().Select(a => a["Name"].AsString).ToList();
145142

143+
res = db.Engine.FindAll("CounterNames").Select(a => a["Name"].AsString).ToList();
146144
}
147145
}
148146
catch (Exception exc)
@@ -164,7 +162,12 @@ private static string GetDbName(DateTime now)
164162
{
165163
return "perfCounters_" + now.ToString("yyyy-MM-dd") + ".litedb";
166164
}
165+
private string GetDbReadOnlyName(DateTime date)
166+
{
167+
return "Filename=" + PathToDbFolder + GetDbName(date) + ";Mode=ReadOnly;Timeout=" + TimeSpan.FromSeconds(30);
168+
}
167169

168170

171+
169172
}
170173
}

Perfon.Core/Perfon.Core.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<projectUrl>http://perfon.1gb.ru/</projectUrl>
1010
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1111
<description>$description$</description>
12-
<releaseNotes>First stable release</releaseNotes>
12+
<releaseNotes>Dashboard bug fix</releaseNotes>
1313
<copyright>$copyright$</copyright>
1414
<tags>Performance Monitoring .Net </tags>
1515
</metadata>

Perfon.Core/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.1.0")]
36-
[assembly: AssemblyFileVersion("1.0.1.0")]
35+
[assembly: AssemblyVersion("1.0.2.0")]
36+
[assembly: AssemblyFileVersion("1.0.w.0")]

Perfon.WebApi/Controllers/PerfCountersController.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Perfon.WebApi
2020
public class PerfCountersController : ApiController
2121
{
2222
readonly string keyList = "PerfListCounters";
23-
23+
2424

2525
public IPerfomanceCountersStorage Db
2626
{
@@ -36,16 +36,16 @@ public IPerfomanceCountersStorage Db
3636
/// Get counters list
3737
/// </summary>
3838
/// <returns></returns>
39-
[Route("api/perfcounters")]
39+
[Route("api/perfcounters")]
4040
public async Task<IHttpActionResult> Get()
4141
{
4242
var res = MemoryCache.Default.Get(keyList) as IEnumerable<string>;
4343

44-
if (res == null)
44+
if (res == null || res.Count() <= 0)
4545
{
4646
// Not the best solution. Use Lazy here??
4747
res = await Db.GetCountersList();
48-
MemoryCache.Default.Add(keyList, res, new DateTimeOffset(DateTime.Now.AddSeconds(10)));
48+
MemoryCache.Default.Add(keyList, res, new DateTimeOffset(DateTime.Now.AddSeconds(60)));
4949
}
5050

5151
return Ok(res);
@@ -59,19 +59,25 @@ public async Task<IHttpActionResult> Get()
5959
[Route("api/perfcounters")] ///{*name}
6060
public async Task<IHttpActionResult> Get([FromUri]string name, [FromUri]DateTime? date = null, [FromUri]int? skip = null)
6161
{
62-
string key = name+date.GetHashCode();
62+
int skip2 = 0;
63+
if (skip.HasValue)
64+
{
65+
skip2 = skip.Value;
66+
}
67+
68+
string key = name + date.GetHashCode();
6369
var res = MemoryCache.Default.Get(key) as IEnumerable<PerfCounterValue>;
6470

6571
if (res == null)
6672
{
6773
// Not the best solution. Use Lazy here??
6874
res = await Db.QueryCounterValues(name, date);
69-
MemoryCache.Default.Add(key, res, new DateTimeOffset(DateTime.Now.AddSeconds(2)));
75+
MemoryCache.Default.Add(key, res, new DateTimeOffset(DateTime.Now.AddSeconds(3)));
7076
}
7177

72-
if (res != null && skip.HasValue)
78+
if (res != null && skip2 > 0)
7379
{
74-
res = res.Skip(skip.Value);
80+
res = res.Skip(skip2).ToList();
7581
}
7682

7783
return Ok(res);

Perfon.WebApi/Perfon.WebApi.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<projectUrl>http://perfon.1gb.ru/</projectUrl>
1010
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1111
<description>$description$</description>
12-
<releaseNotes>First stable release</releaseNotes>
12+
<releaseNotes>Dashboard bug fix</releaseNotes>
1313
<copyright>$copyright$</copyright>
1414
<tags>Performance Monitoring Web Api .Net </tags>
1515
<dependencies>

Perfon.WebApi/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.1.0")]
36-
[assembly: AssemblyFileVersion("1.0.1.0")]
35+
[assembly: AssemblyVersion("1.0.2.0")]
36+
[assembly: AssemblyFileVersion("1.0.2.0")]

0 commit comments

Comments
 (0)