private async Task BulkInsert(Pipeline.Admin.IDataStreamConnector connector, DataStream stream, string deviceId, QueryRangeType rangeType) { var records = base.GetRecordsToInsert(stream, deviceId, rangeType); var insertCommand = "insert into [unittest] (value1,value2,value3,customid,location,deviceId,timeStamp,pointindex) values (@value1,@value2,@value3,@customid,@location,@deviceId,@timeStamp,@pointindex)"; using (var cn = new System.Data.SqlClient.SqlConnection(GetConnectionString(stream))) using (var cmd = new System.Data.SqlClient.SqlCommand(insertCommand, cn)) { cmd.Parameters.AddWithValue("value1", 100); cmd.Parameters.AddWithValue("value2", 100); cmd.Parameters.AddWithValue("value3", 100); cmd.Parameters.AddWithValue("pointindex", 0); cmd.Parameters.AddWithValue("customid", Guid.NewGuid().ToId()); cmd.Parameters.AddWithValue("deviceid", deviceId); cmd.Parameters.AddWithValue("location", $"POINT(23.4 -85.5)"); cmd.Parameters.AddWithValue("timestamp", string.Empty); cn.Open(); var idx = -0; foreach (var record in records) { if (rangeType == QueryRangeType.Records_100) { cmd.Parameters["timestamp"].Value = DateTime.Now.AddDays(idx - 50); } else { cmd.Parameters["timestamp"].Value = record.Timestamp.ToDateTime(); } cmd.Parameters["pointindex"].Value = idx++; await cmd.ExecuteNonQueryAsync(); } cmd.Parameters.Clear(); cmd.CommandText = $"select count(*) from {stream.DBTableName}"; Assert.AreEqual(records.Count, Convert.ToInt32(cmd.ExecuteScalar())); } }
private async Task BulkInsert(IDataStreamConnector connector, DataStream stream, string deviceType, QueryRangeType rangeType) { var batchOper = new TableBatchOperation(); var cloudTable = GetCloudTable(stream); var records = GetRecordsToInsert(stream, "dev123", rangeType); foreach (var record in records) { var tsRecord = DataStreamTSEntity.FromDeviceStreamRecord(stream, record); batchOper.Add(TableOperation.Insert(tsRecord)); Console.WriteLine(tsRecord.RowKey); } var results = await cloudTable.ExecuteBatchAsync(batchOper); Assert.AreEqual(records.Count, results.Count, "Batch result size should match insert size"); foreach (var result in results) { Assert.AreEqual(204, result.HttpStatusCode); } // Give it just a little time to insert the rest of the records await Task.Delay(1000); }
protected List <DataStreamRecord> GetRecordsToInsert(DataStream stream, string deviceId, QueryRangeType rangeType) { var rnd = new Random(); var records = new List <DataStreamRecord>(); switch (rangeType) { case QueryRangeType.Records_100: for (var idx = 0; idx < 100; ++idx) { records.Add(GetRecord(stream, "dev123", null, new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}"))); } break; case QueryRangeType.ForBeforeQuery: for (var idx = 0; idx < 10; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(5).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", false), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } for (var idx = 10; idx < 20; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(0).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", false), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } for (var idx = 20; idx < 30; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(-5).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", true), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } break; case QueryRangeType.ForInRangeQuery: for (var idx = 0; idx < 10; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(5).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", false), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } for (var idx = 10; idx < 20; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(0).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", true), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } for (var idx = 20; idx < 30; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(-5).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", false), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } break; case QueryRangeType.ForAfterQuery: for (var idx = 0; idx < 10; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(-5).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", false), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } for (var idx = 10; idx < 20; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(0).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", false), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } for (var idx = 20; idx < 30; ++idx) { records.Add((GetRecord(stream, deviceId, DateTime.Now.AddDays(5).ToJSONString(), new KeyValuePair <string, object>("pointIndex", idx), new KeyValuePair <string, object>("pointOn", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("inrange", true), new KeyValuePair <string, object>("pointTwo", 50 - rnd.NextDouble() * 100), new KeyValuePair <string, object>("pointThree", $"testing-{rnd.Next(100, 10000)}")))); } break; } return(records); }
private async Task BulkInsert(IDataStreamConnector connector, DataStream stream, string deviceType, QueryRangeType rangeType) { var records = GetRecordsToInsert(stream, "dev123", rangeType); foreach (var record in records) { Assert.IsTrue((await connector.AddItemAsync(record)).Successful, "Did not insert bulk item"); } // Give it just a little time to insert the rest of the records await Task.Delay(1000); }