/// <summary> /// Hit benchmarking checkpoint /// </summary> /// <param name="checkpointName"></param> /// <param name="epicId"></param> /// <param name="processName"></param> /// <param name="callerProcessName"></param> /// <param name="description"></param> /// <param name="payload"></param> public void Bench(string checkpointName, Guid epicId, string processName, string callerProcessName = null, string description = null, object payload = null) { if (!Configuration.IsEnabled) { return; } var entry = new BenchmarkEntry(); entry.Checkpoint = checkpointName; entry.EpicId = epicId; entry.ProcessName = processName; entry.CallerProcessName = callerProcessName; entry.Description = description; entry.Payload = payload; entry.CreatedOn = DateTime.UtcNow; var bag = InMemoryEntiryCache.GetOrAdd(epicId, new ConcurrentBag <BenchmarkEntry>()); bag.Add(entry); }
/// <summary> /// Persist epic data /// </summary> /// <param name="epicId"></param> public void FlushEpic(Guid epicId) { if (!Configuration.IsEnabled) { return; } bool success = true; success = InMemoryNameCache.TryRemove(epicId, out string epicName); if (!success) { throw new KeyNotFoundException(); } success = InMemoryEntiryCache.TryRemove(epicId, out ConcurrentBag <BenchmarkEntry> values); if (!success) { throw new KeyNotFoundException(); } var entries = values.OrderBy(x => x.CreatedOn); DataTable table = new DataTable(); table.Columns.Add("Id"); table.Columns.Add("CreatedOn"); table.Columns.Add("EpicId"); table.Columns.Add("ProcessName"); table.Columns.Add("CallerProcessName"); table.Columns.Add("EpicName"); table.Columns.Add("Checkpoint"); table.Columns.Add("Description"); table.Columns.Add("Payload"); foreach (var entry in entries) { var row = table.NewRow(); row["Id"] = 0; row["CreatedOn"] = entry.CreatedOn.ToString("yyyy-MM-dd HH:mm:ss.fffffff"); row["EpicName"] = epicName; row["EpicId"] = entry.EpicId; row["ProcessName"] = entry.ProcessName; row["Checkpoint"] = entry.Checkpoint; row["CallerProcessName"] = string.IsNullOrEmpty(entry.CallerProcessName) ? DBNull.Value : (object)entry.CallerProcessName; row["Description"] = string.IsNullOrEmpty(entry.Description) ? DBNull.Value : (object)entry.Description; row["Payload"] = entry.Payload == null ? DBNull.Value : (object)JsonConvert.SerializeObject(entry.Payload); table.Rows.Add(row); } using (var connection = new SqlConnection(Configuration.ConnectionString)) { connection.Open(); SqlTransaction transaction = connection.BeginTransaction(); using (var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction)) { bulkCopy.BatchSize = 500; bulkCopy.DestinationTableName = Configuration.TableName; try { bulkCopy.WriteToServer(table); transaction.Commit(); } finally { connection.Close(); } } } }