public Task Run(IWorkload workload, IBucket bucket, Action <WorkloadResult> onWorkloadCompleted) { Ensure.That(workload, "workload").IsNotNull(); Ensure.That(onWorkloadCompleted, "onWorkloadCompleted").IsNotNull(); try { return(Task.WhenAll( Enumerable.Range(1, _numOfClients) .Select(index => { return workload.Execute(bucket, index) .ContinueWith(task => { onWorkloadCompleted(task.Result); }); }) )); } catch (Exception ex) { throw new Exception($"Exception while running workload tasks for \"{workload.GetType().Name}\".", ex); } }
public void Start(string[] args) { Logger.Debug("OnStart"); items = new WorkloadCollection(); ServiceStopping += items.StopEventHandler; Logger.Debug("Reading configuration data"); WorkloadConfigurationSection config = null; try { config = (WorkloadConfigurationSection)ConfigurationManager.GetSection( "WorkloadConfiguration"); if (config.Workloads.Count == 0) { Logger.Error("No workloads configured."); } } catch (Exception e) { Logger.Error(e.Message); throw; } foreach (WorkloadConfiguration wcfg in config.Workloads) { Logger.Debug($"Creating workload {wcfg.WorkloadName}."); try { Logger.Debug($"Creating instance of {wcfg.WorkloadType}"); IWorkload w = null; if (wcfg.WorkloadAssembly == "") { Logger.Debug("Loading from main assembly"); w = (IWorkload)Assembly.GetExecutingAssembly().CreateInstance(wcfg.WorkloadType); } else { Logger.Debug($"Loading from assembly '{wcfg.WorkloadAssembly}"); Assembly a = Assembly.Load(wcfg.WorkloadAssembly); w = (IWorkload)a.CreateInstance(wcfg.WorkloadType); } if (w == null) { Logger.Error($"Unable to create instance of {wcfg.WorkloadType}"); continue; } foreach (WorkloadProperty wProp in wcfg.WorkloadProperties) { Logger.Debug($"Setting property [{wProp.PropertyName}] to '{wProp.PropertyValue}'."); PropertyInfo p = w.GetType().GetProperty(wProp.PropertyName); if (p != null) { if (p.PropertyType == typeof(string)) { p.SetValue(w, wProp.PropertyValue); } else if (p.PropertyType == typeof(int?)) { p.SetValue(w, int.Parse(wProp.PropertyValue)); } else if (p.PropertyType == typeof(int)) { p.SetValue(w, int.Parse(wProp.PropertyValue)); } else if (p.PropertyType == typeof(bool)) { p.SetValue(w, bool.Parse(wProp.PropertyValue)); } else { Logger.Error($"Unsupported configuration property type {p.PropertyType}"); } } } items.Add(w); } catch (Exception e) { Logger.Error(e.Message, e); } } Logger.Info($"{ServiceName} started."); items.Start(); }