func domainsPagination(domainDAO dao.DomainDAO) { numberOfItems := 1000 for i := 0; i < numberOfItems; i++ { domain := model.Domain{ FQDN: fmt.Sprintf("example%d.com.br", i), } if err := domainDAO.Save(&domain); err != nil { utils.Fatalln("Error saving domain in database", err) } } pagination := dao.DomainDAOPagination{ PageSize: 10, Page: 5, OrderBy: []dao.DomainDAOSort{ { Field: dao.DomainDAOOrderByFieldFQDN, Direction: dao.DAOOrderByDirectionAscending, }, }, } domains, err := domainDAO.FindAll(&pagination, true, "") if err != nil { utils.Fatalln("Error retrieving domains", err) } if pagination.NumberOfItems != numberOfItems { utils.Errorln("Number of items not calculated correctly", nil) } if pagination.NumberOfPages != numberOfItems/pagination.PageSize { utils.Errorln("Number of pages not calculated correctly", nil) } if len(domains) != pagination.PageSize { utils.Errorln("Number of domains not following page size", nil) } pagination = dao.DomainDAOPagination{ PageSize: 10000, Page: 1, OrderBy: []dao.DomainDAOSort{ { Field: dao.DomainDAOOrderByFieldFQDN, Direction: dao.DAOOrderByDirectionAscending, }, }, } domains, err = domainDAO.FindAll(&pagination, true, "") if err != nil { utils.Fatalln("Error retrieving domains", err) } if pagination.NumberOfPages != 1 { utils.Fatalln("Calculating wrong number of pages when there's only one page", nil) } for i := 0; i < numberOfItems; i++ { fqdn := fmt.Sprintf("example%d.com.br", i) if err := domainDAO.RemoveByFQDN(fqdn); err != nil { utils.Fatalln("Error removing domain from database", err) } } }
func calculateDomainDAODurations(domainDAO dao.DomainDAO, numberOfItems int) (totalDuration, insertDuration, queryDuration, removeDuration time.Duration) { beginTimer := time.Now() sectionTimer := beginTimer // Build array to create many at once var domains []*model.Domain for i := 0; i < numberOfItems; i++ { domain := model.Domain{ FQDN: fmt.Sprintf("test%d.com.br", i), } domains = append(domains, &domain) } errorInDomainsCreation := false domainResults := domainDAO.SaveMany(domains) // Check if there was any error while creating them for _, domainResult := range domainResults { if domainResult.Error != nil { errorInDomainsCreation = true utils.Errorln(fmt.Sprintf("Couldn't save domain %s in database during the performance test", domainResult.Domain.FQDN), domainResult.Error) } } if errorInDomainsCreation { utils.Fatalln("Due to errors in domain creation, the performance test will be aborted", nil) } insertDuration = time.Since(sectionTimer) sectionTimer = time.Now() // Try to find domains from different parts of the whole range to check indexes queryRanges := numberOfItems / 4 fqdn1 := fmt.Sprintf("test%d.com.br", queryRanges) fqdn2 := fmt.Sprintf("test%d.com.br", queryRanges*2) fqdn3 := fmt.Sprintf("test%d.com.br", queryRanges*3) if _, err := domainDAO.FindByFQDN(fqdn1); err != nil { utils.Fatalln(fmt.Sprintf("Couldn't find domain %s in database during "+ "the performance test", fqdn1), err) } if _, err := domainDAO.FindByFQDN(fqdn2); err != nil { utils.Fatalln(fmt.Sprintf("Couldn't find domain %s in database during "+ "the performance test", fqdn2), err) } if _, err := domainDAO.FindByFQDN(fqdn3); err != nil { utils.Fatalln(fmt.Sprintf("Couldn't find domain %s in database during "+ "the performance test", fqdn3), err) } queryDuration = time.Since(sectionTimer) sectionTimer = time.Now() errorInDomainsRemoval := false domainResults = domainDAO.RemoveMany(domains) // Check if there was any error while removing them for _, domainResult := range domainResults { if domainResult.Error != nil { errorInDomainsRemoval = true utils.Errorln(fmt.Sprintf("Error while trying to remove a domain %s during the performance test", domainResult.Domain.FQDN), domainResult.Error) } } if errorInDomainsRemoval { utils.Fatalln("Due to errors in domain removal, the performance test will be aborted", nil) } removeDuration = time.Since(sectionTimer) totalDuration = time.Since(beginTimer) return }
func scansPagination(scanDAO dao.ScanDAO) { numberOfItems := 1000 for i := 0; i < numberOfItems; i++ { scan := model.Scan{ StartedAt: time.Now().Add(time.Duration(-i) * time.Minute), } if err := scanDAO.Save(&scan); err != nil { utils.Fatalln("Error saving scan in database", err) } } pagination := dao.ScanDAOPagination{ PageSize: 10, Page: 5, OrderBy: []dao.ScanDAOSort{ { Field: dao.ScanDAOOrderByFieldStartedAt, Direction: dao.DAOOrderByDirectionAscending, }, }, } scans, err := scanDAO.FindAll(&pagination, true) if err != nil { utils.Fatalln("Error retrieving scans", err) } if pagination.NumberOfItems != numberOfItems { utils.Errorln("Number of items not calculated correctly", nil) } if pagination.NumberOfPages != numberOfItems/pagination.PageSize { utils.Errorln("Number of pages not calculated correctly", nil) } if len(scans) != pagination.PageSize { utils.Errorln("Number of scans not following page size", nil) } pagination = dao.ScanDAOPagination{ PageSize: 10000, Page: 1, OrderBy: []dao.ScanDAOSort{ { Field: dao.ScanDAOOrderByFieldStartedAt, Direction: dao.DAOOrderByDirectionAscending, }, }, } scans, err = scanDAO.FindAll(&pagination, true) if err != nil { utils.Fatalln("Error retrieving scans", err) } if pagination.NumberOfPages != 1 { utils.Fatalln("Calculating wrong number of pages when there's only one page", nil) } if err := scanDAO.RemoveAll(); err != nil { utils.Fatalln("Error removing scans from database", err) } }