public IList <LoadtestViewModel> ConvertToViewModels(IEnumerable <Loadtest> domains) { List <LoadtestViewModel> viewModels = new List <LoadtestViewModel>(); LoadTestingContext context = LoadTestingContext.Create(base.ConnectionStringRepository); foreach (Loadtest lt in domains) { LoadtestViewModel vm = new LoadtestViewModel(); vm.Id = lt.Id; AgentMongoDb agentDb = context.Agents.Find <AgentMongoDb>(a => a.DomainId == lt.AgentId).SingleOrDefault(); if (agentDb == null) { throw new ArgumentException("There is no load test agent with the given ID."); } vm.AgentCountry = agentDb.Location.Country; vm.AgentCity = agentDb.Location.City; CustomerMongoDb custDb = context.Customers.Find <CustomerMongoDb>(c => c.DomainId == lt.CustomerId).SingleOrDefault(); if (custDb == null) { throw new ArgumentException("There is no customer with the given ID."); } vm.CustomerName = custDb.Name; if (lt.EngineerId.HasValue) { EngineerMongoDb engDb = context.Engineers.Find <EngineerMongoDb>(e => e.DomainId == lt.EngineerId.Value).SingleOrDefault(); if (engDb == null) { throw new ArgumentException("There is no engineer with the given ID."); } vm.EngineerName = engDb.Name; } LoadtestTypeMongoDb loadtestTypeDb = context.LoadtestTypes.Find(l => l.DomainId == lt.LoadtestTypeId).SingleOrDefault(); if (loadtestTypeDb == null) { throw new ArgumentException("There is no load test type with the given ID."); } vm.LoadtestTypeShortDescription = loadtestTypeDb.Description.ShortDescription; ProjectMongoDb projectDb = context.Projects.Find(p => p.DomainId == lt.ProjectId).SingleOrDefault(); if (projectDb == null) { throw new ArgumentException("There is no project with the given ID."); } vm.ProjectName = projectDb.Description.ShortDescription; ScenarioMongoDb scenarioDb = context.Scenarios.Find(s => s.DomainId == lt.ScenarioId).SingleOrDefault(); if (scenarioDb == null) { throw new ArgumentException("There is no scenario with the given ID."); } vm.ScenarioUriOne = scenarioDb.UriOne; vm.ScenarioUriTwo = scenarioDb.UriTwo; vm.ScenarioUriThree = scenarioDb.UriThree; vm.UserCount = lt.Parameters.UserCount; vm.StartDateUtc = lt.Parameters.StartDateUtc; vm.DurationSec = lt.Parameters.DurationSec; viewModels.Add(vm); } return(viewModels); }
public IList <Loadtest> ConvertToDomain(IEnumerable <LoadtestViewModel> viewModels) { List <Loadtest> loadtests = new List <Loadtest>(); LoadTestingContext context = LoadTestingContext.Create(base.ConnectionStringRepository); foreach (LoadtestViewModel vm in viewModels) { Guid id = vm.Id; LoadtestParameters ltParams = new LoadtestParameters(vm.StartDateUtc, vm.UserCount, vm.DurationSec); var agentQueryBuilder = Builders <AgentMongoDb> .Filter; var agentCityFilter = agentQueryBuilder.Eq <string>(a => a.Location.City.ToLower(), vm.AgentCity.ToLower()); var agentCountryFilter = agentQueryBuilder.Eq <string>(a => a.Location.Country.ToLower(), vm.AgentCountry.ToLower()); var agentCompleteFilter = agentQueryBuilder.And(agentCityFilter, agentCountryFilter); AgentMongoDb agentDb = context.Agents.Find(agentCompleteFilter).FirstOrDefault(); if (agentDb == null) { throw new ArgumentException("There is no agent with the given properties."); } var customerQueryBuilder = Builders <CustomerMongoDb> .Filter; var customerQuery = customerQueryBuilder.Eq <string>(c => c.Name.ToLower(), vm.CustomerName.ToLower()); CustomerMongoDb custDb = context.Customers.Find(customerQuery).SingleOrDefault(); if (custDb == null) { throw new ArgumentException("There is no customer with the given properties."); } Guid?engineerId = null; if (!string.IsNullOrEmpty(vm.EngineerName)) { EngineerMongoDb engDb = context.Engineers.Find(e => e.Name.ToLower() == vm.EngineerName.ToLower()).SingleOrDefault(); if (engDb == null) { throw new ArgumentException("There is no engineer with the given properties."); } engineerId = engDb.DomainId; } LoadtestTypeMongoDb ltTypeDb = context.LoadtestTypes.Find(t => t.Description.ShortDescription.ToLower() == vm.LoadtestTypeShortDescription.ToLower()).SingleOrDefault(); if (ltTypeDb == null) { throw new ArgumentException("There is no load test type with the given properties."); } ProjectMongoDb projectDb = context.Projects.Find(p => p.Description.ShortDescription.ToLower() == vm.ProjectName.ToLower()).SingleOrDefault(); if (projectDb == null) { throw new ArgumentException("There is no project with the given properties."); } var scenarioQueryBuilder = Builders <ScenarioMongoDb> .Filter; var scenarioUriOneFilter = scenarioQueryBuilder.Eq <string>(s => s.UriOne.ToLower(), vm.ScenarioUriOne.ToLower()); var scenarioUriTwoFilter = scenarioQueryBuilder.Eq <string>(s => s.UriTwo.ToLower(), vm.ScenarioUriTwo.ToLower()); var scenarioUriThreeFilter = scenarioQueryBuilder.Eq <string>(s => s.UriThree.ToLower(), vm.ScenarioUriThree.ToLower()); var scenarioCompleteFilter = scenarioQueryBuilder.And(scenarioUriOneFilter, scenarioUriTwoFilter, scenarioUriThreeFilter); ScenarioMongoDb scenarioDb = context.Scenarios.Find(scenarioCompleteFilter).SingleOrDefault(); if (scenarioDb == null) { scenarioDb = new ScenarioMongoDb() { DbObjectId = ObjectId.GenerateNewId(), DomainId = Guid.NewGuid() }; if (!string.IsNullOrEmpty(vm.ScenarioUriOne)) { scenarioDb.UriOne = vm.ScenarioUriOne; } if (!string.IsNullOrEmpty(vm.ScenarioUriTwo)) { scenarioDb.UriTwo = vm.ScenarioUriTwo; } if (!string.IsNullOrEmpty(vm.ScenarioUriThree)) { scenarioDb.UriThree = vm.ScenarioUriThree; } context.Scenarios.InsertOne(scenarioDb); } Loadtest converted = new Loadtest(id, ltParams, agentDb.DomainId, custDb.DomainId, engineerId, ltTypeDb.DomainId, projectDb.DomainId, scenarioDb.DomainId); loadtests.Add(converted); } return(loadtests); }
private static void Seed() { LoadTestingContext context = LoadTestingContext.Create(new WebConfigConnectionStringRepository()); List <AgentMongoDb> agents = new List <AgentMongoDb>(); AgentMongoDb amazon = new AgentMongoDb(); amazon.DomainId = Guid.NewGuid(); amazon.DbObjectId = ObjectId.GenerateNewId(); amazon.Location = new LocationMongoDb() { City = "Seattle", Country = "USA", DbObjectId = ObjectId.GenerateNewId() }; AgentMongoDb rackspace = new AgentMongoDb(); rackspace.DomainId = Guid.NewGuid(); rackspace.DbObjectId = ObjectId.GenerateNewId(); rackspace.Location = new LocationMongoDb() { City = "Frankfurt", Country = "Germany", DbObjectId = ObjectId.GenerateNewId() }; AgentMongoDb azure = new AgentMongoDb(); azure.DomainId = Guid.NewGuid(); azure.DbObjectId = ObjectId.GenerateNewId(); azure.Location = new LocationMongoDb() { City = "Tokyo", Country = "Japan", DbObjectId = ObjectId.GenerateNewId() }; agents.Add(amazon); agents.Add(rackspace); agents.Add(azure); Task addManyAgentsTask = context.Agents.InsertManyAsync(agents); List <CustomerMongoDb> customers = new List <CustomerMongoDb>(); CustomerMongoDb niceCustomer = new CustomerMongoDb(); niceCustomer.DomainId = Guid.NewGuid(); niceCustomer.DbObjectId = ObjectId.GenerateNewId(); niceCustomer.Name = "Nice customer"; CustomerMongoDb greatCustomer = new CustomerMongoDb(); greatCustomer.DomainId = Guid.NewGuid(); greatCustomer.DbObjectId = ObjectId.GenerateNewId(); greatCustomer.Name = "Great customer"; CustomerMongoDb okCustomer = new CustomerMongoDb(); okCustomer.DomainId = Guid.NewGuid(); okCustomer.DbObjectId = ObjectId.GenerateNewId(); okCustomer.Name = "OK Customer"; customers.Add(niceCustomer); customers.Add(greatCustomer); customers.Add(okCustomer); Task addManyCustomersTask = context.Customers.InsertManyAsync(customers); List <EngineerMongoDb> engineers = new List <EngineerMongoDb>(); EngineerMongoDb john = new EngineerMongoDb(); john.DomainId = Guid.NewGuid(); john.Name = "John"; john.DbObjectId = ObjectId.GenerateNewId(); EngineerMongoDb mary = new EngineerMongoDb(); mary.DomainId = Guid.NewGuid(); mary.Name = "Mary"; mary.DbObjectId = ObjectId.GenerateNewId(); EngineerMongoDb fred = new EngineerMongoDb(); fred.DomainId = Guid.NewGuid(); fred.Name = "Fred"; fred.DbObjectId = ObjectId.GenerateNewId(); engineers.Add(john); engineers.Add(mary); engineers.Add(fred); Task addManyEngineersTask = context.Engineers.InsertManyAsync(engineers); List <LoadtestTypeMongoDb> testTypes = new List <LoadtestTypeMongoDb>(); LoadtestTypeMongoDb stressTest = new LoadtestTypeMongoDb(); stressTest.DomainId = Guid.NewGuid(); stressTest.DbObjectId = ObjectId.GenerateNewId(); stressTest.Description = new DescriptionMongoDb() { DbObjectId = ObjectId.GenerateNewId(), ShortDescription = "Stress test", LongDescription = "To determine or validate an application’s behavior when it is pushed beyond normal or peak load conditions." }; LoadtestTypeMongoDb capacityTest = new LoadtestTypeMongoDb(); capacityTest.DomainId = Guid.NewGuid(); capacityTest.DbObjectId = ObjectId.GenerateNewId(); capacityTest.Description = new DescriptionMongoDb() { DbObjectId = ObjectId.GenerateNewId(), ShortDescription = "Capacity test", LongDescription = "To determine how many users and/or transactions a given system will support and still meet performance goals." }; testTypes.Add(stressTest); testTypes.Add(capacityTest); Task addManyLoadtestTypesTask = context.LoadtestTypes.InsertManyAsync(testTypes); List <ProjectMongoDb> projects = new List <ProjectMongoDb>(); ProjectMongoDb firstProject = new ProjectMongoDb(); firstProject.DomainId = Guid.NewGuid(); firstProject.DbObjectId = ObjectId.GenerateNewId(); firstProject.Description = new DescriptionMongoDb() { DbObjectId = ObjectId.GenerateNewId(), ShortDescription = "First project", LongDescription = "Long description of first project" }; ProjectMongoDb secondProject = new ProjectMongoDb(); secondProject.DomainId = Guid.NewGuid(); secondProject.DbObjectId = ObjectId.GenerateNewId(); secondProject.Description = new DescriptionMongoDb() { DbObjectId = ObjectId.GenerateNewId(), ShortDescription = "Second project", LongDescription = "Long description of second project" }; ProjectMongoDb thirdProject = new ProjectMongoDb(); thirdProject.DomainId = Guid.NewGuid(); thirdProject.DbObjectId = ObjectId.GenerateNewId(); thirdProject.Description = new DescriptionMongoDb() { DbObjectId = ObjectId.GenerateNewId(), ShortDescription = "Third project", LongDescription = "Long description of third project" }; projects.Add(firstProject); projects.Add(secondProject); projects.Add(thirdProject); Task addManyProjectsTask = context.Projects.InsertManyAsync(projects); List <ScenarioMongoDb> scenarios = new List <ScenarioMongoDb>(); ScenarioMongoDb scenarioOne = new ScenarioMongoDb(); scenarioOne.DomainId = Guid.NewGuid(); scenarioOne.DbObjectId = ObjectId.GenerateNewId(); scenarioOne.UriOne = "www.bbc.co.uk"; scenarioOne.UriTwo = "www.cnn.com"; ScenarioMongoDb scenarioTwo = new ScenarioMongoDb(); scenarioTwo.DomainId = Guid.NewGuid(); scenarioTwo.DbObjectId = ObjectId.GenerateNewId(); scenarioTwo.UriOne = "www.amazon.com"; scenarioTwo.UriTwo = "www.microsoft.com"; ScenarioMongoDb scenarioThree = new ScenarioMongoDb(); scenarioThree.DomainId = Guid.NewGuid(); scenarioThree.DbObjectId = ObjectId.GenerateNewId(); scenarioThree.UriOne = "www.greatsite.com"; scenarioThree.UriTwo = "www.nosuchsite.com"; scenarioThree.UriThree = "www.neverheardofsite.com"; scenarios.Add(scenarioOne); scenarios.Add(scenarioTwo); scenarios.Add(scenarioThree); Task addManyScenariosTask = context.Scenarios.InsertManyAsync(scenarios); List <LoadtestMongoDb> loadtests = new List <LoadtestMongoDb>(); LoadtestMongoDb ltOne = new LoadtestMongoDb(); ltOne.DomainId = Guid.NewGuid(); ltOne.DbObjectId = ObjectId.GenerateNewId(); ltOne.AgentId = amazon.DomainId; ltOne.CustomerId = niceCustomer.DomainId; ltOne.EngineerId = john.DomainId; ltOne.LoadtestTypeId = stressTest.DomainId; DateTime ltOneStart = DateTime.UtcNow; ltOne.Parameters = new LoadtestParametersMongoDb() { DbObjectId = ObjectId.GenerateNewId(), DurationSec = 60, StartDateUtc = ltOneStart, UserCount = 10, ExpectedEndDateUtc = ltOneStart.AddSeconds(60) }; ltOne.ProjectId = firstProject.DomainId; ltOne.ScenarioId = scenarioOne.DomainId; LoadtestMongoDb ltTwo = new LoadtestMongoDb(); ltTwo.DomainId = Guid.NewGuid(); ltTwo.DbObjectId = ObjectId.GenerateNewId(); ltTwo.AgentId = azure.DomainId; ltTwo.CustomerId = greatCustomer.DomainId; ltTwo.EngineerId = mary.DomainId; ltTwo.LoadtestTypeId = capacityTest.DomainId; DateTime ltTwoStart = DateTime.UtcNow.AddMinutes(20); ltTwo.Parameters = new LoadtestParametersMongoDb() { DbObjectId = ObjectId.GenerateNewId(), DurationSec = 120, StartDateUtc = ltTwoStart, UserCount = 40, ExpectedEndDateUtc = ltTwoStart.AddSeconds(120) }; ltTwo.ProjectId = secondProject.DomainId; ltTwo.ScenarioId = scenarioThree.DomainId; LoadtestMongoDb ltThree = new LoadtestMongoDb(); ltThree.DomainId = Guid.NewGuid(); ltThree.DbObjectId = ObjectId.GenerateNewId(); ltThree.AgentId = rackspace.DomainId; ltThree.CustomerId = okCustomer.DomainId; ltThree.EngineerId = fred.DomainId; ltThree.LoadtestTypeId = stressTest.DomainId; DateTime ltThreeStart = DateTime.UtcNow.AddMinutes(30); ltThree.Parameters = new LoadtestParametersMongoDb() { DbObjectId = ObjectId.GenerateNewId(), DurationSec = 180, StartDateUtc = ltThreeStart, UserCount = 50, ExpectedEndDateUtc = ltThreeStart.AddSeconds(180) }; ltThree.ProjectId = thirdProject.DomainId; ltThree.ScenarioId = scenarioTwo.DomainId; loadtests.Add(ltOne); loadtests.Add(ltTwo); loadtests.Add(ltThree); Task addManyLoadtestsTask = context.Loadtests.InsertManyAsync(loadtests); Task.WaitAll(addManyAgentsTask, addManyCustomersTask, addManyEngineersTask, addManyLoadtestTypesTask , addManyProjectsTask, addManyScenariosTask, addManyLoadtestsTask); }