Hi,
I tried to do a benchmarking read/write openhistorian based on examples of writing and reading. After writing several million points * 50000 tags, reading values by one tag is very slow - about hundreds points/second. Is it normally?
My code fragment:
public void ReadHistorianData(string historianServer, string instanceName, DateTime startTime, DateTime stopTime, string measurementIDs = null)
{
const int DefaultHistorianPort = 38402;
if (string.IsNullOrEmpty(historianServer))
throw new ArgumentNullException(" historianServer", " Missing historian server parameter");
if (string.IsNullOrEmpty(instanceName))
throw new ArgumentNullException(" instanceName", " Missing historian instance name parameter");
if (startTime > stopTime)
throw new ArgumentException(" Invalid time range specified", " startTime");
string[] parts = historianServer.Split(':');
string hostName = parts[0];
int port;
if (parts.Length < 2 || !int.TryParse(parts[1], out port))
port = DefaultHistorianPort;
// Open historian connection
using (HistorianClient client = new HistorianClient(hostName, port))
using (ClientDatabaseBase<HistorianKey, HistorianValue> reader = client.GetDatabase<HistorianKey, HistorianValue>(instanceName))
{
// Setup time-range and point ID selections
SeekFilterBase<HistorianKey> timeFilter = TimestampSeekFilter.CreateFromRange<HistorianKey>(startTime, stopTime);
MatchFilterBase<HistorianKey, HistorianValue> pointFilter = null;
HistorianKey key = new HistorianKey();
HistorianValue value = new HistorianValue();
if (!string.IsNullOrEmpty(measurementIDs))
pointFilter = PointIdMatchFilter.CreateFromList<HistorianKey, HistorianValue>(measurementIDs.Split(',').Select(ulong.Parse));
ulong iterationCount = 100000;
var sw = new System.Diagnostics.Stopwatch();
ulong count = 0;
sw.Start();
for (ulong j = 0; j < iterationCount; ++j)
{
// Start stream reader for the provided time window and selected points
TreeStream<HistorianKey, HistorianValue> stream = reader.Read(SortedTreeEngineReaderOptions.Default, timeFilter, pointFilter);
while (stream.Read(key, value))
{
if(key.PointID != 0)
throw new Exception("Wrong key ID!");
//yield return new HistorianMeasurement(key.PointID, key.TimestampAsDate, value.AsSingle);
++count;
}
if (j % 5 == 0)
{
float speed1 = 1000.0f * count / (float)sw.ElapsedMilliseconds;
m_labelRead.Text = string.Format("Read speed: {0} rec/sec, count: {1}", speed1, count);
Application.DoEvents();
}
}
m_labelRead.Text += " OK";
}
}
public void WriteHistorianData(string historianServer, string instanceName)
{
const int DefaultHistorianPort = 38402;
if (string.IsNullOrEmpty(historianServer))
throw new ArgumentNullException("historianServer", "Missing historian server parameter");
if (string.IsNullOrEmpty(instanceName))
throw new ArgumentNullException(" instanceName", "Missing historian instance name parameter");
string[] parts = historianServer.Split(':');
string hostName = parts[0];
int port;
if (parts.Length < 2 || !int.TryParse(parts[1], out port))
port = DefaultHistorianPort;
ulong count = 0;
DateTime startTime = DateTime.Now.AddHours(-240);
// Open historian connection
using (HistorianClient client = new HistorianClient(hostName, port))
using (ClientDatabaseBase<HistorianKey, HistorianValue> database = client.GetDatabase<HistorianKey, HistorianValue>(instanceName))
using (HistorianInputQueue queue = new HistorianInputQueue(() => database))
{
HistorianKey key = new HistorianKey();
HistorianValue value = new HistorianValue();
ulong tagCount = 50000;
ulong iterationCount = 100000;
var sw = new System.Diagnostics.Stopwatch();
sw.Start();
ulong local_count = 0;
for (ulong j = 0; j < iterationCount; ++j)
{
for (ulong i = 0; i < tagCount; ++i)
{
key.PointID = i;
key.TimestampAsDate = startTime;
value.AsSingle = 2.0f * i * j;
queue.Enqueue(key, value);
++count;
++local_count;
}
startTime = startTime.AddSeconds(1);
if (j % 5 == 0)
{
sw.Stop();
float speed = 1000.0f * local_count / (float)sw.ElapsedMilliseconds;
m_labelWrite.Text = string.Format("Speed: {0} rec/sec, count: {1}", speed, count);
Application.DoEvents();
while (queue.Size > 0)
Thread.Sleep(100);
sw.Start();
local_count = 0;
}
}
}
}
Thanks!
Nikolay