[258] | 1 | import grails.util.GrailsUtil |
---|
| 2 | |
---|
| 3 | /** |
---|
| 4 | * Provides a data service to create a large volume of test data for load testing. |
---|
| 5 | */ |
---|
| 6 | class CreateBulkDataService { |
---|
| 7 | |
---|
| 8 | boolean transactional = false |
---|
| 9 | |
---|
| 10 | def personService |
---|
| 11 | def taskService |
---|
| 12 | def dateUtilService |
---|
| 13 | def appConfigService |
---|
| 14 | def assignedGroupService |
---|
| 15 | def assignedPersonService |
---|
| 16 | |
---|
| 17 | def sessionFactory |
---|
| 18 | def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP |
---|
| 19 | |
---|
[261] | 20 | def startTime |
---|
| 21 | def lastBatchStarted |
---|
[258] | 22 | |
---|
| 23 | /******************************************* |
---|
| 24 | Start of Group methods. |
---|
| 25 | Generally use these methods to create data. |
---|
| 26 | *******************************************/ |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Make a run of data creation. |
---|
| 30 | */ |
---|
| 31 | def create() { |
---|
| 32 | if(!GrailsUtil.environment == "development") { |
---|
| 33 | log.error "Dev environment not detected, will NOT create bulk data." |
---|
| 34 | return false |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | log.info "Creating BULK data..." |
---|
| 38 | |
---|
| 39 | // Person and Utils |
---|
| 40 | log.info "Creating persons..." |
---|
| 41 | createBulkTestPersons() |
---|
| 42 | |
---|
| 43 | // createBulkTestSites() |
---|
| 44 | // createBulkTestDepartments() |
---|
| 45 | // createBulkTestSuppliers() |
---|
| 46 | // createBulkTestManufacturers() |
---|
| 47 | |
---|
| 48 | // Tasks |
---|
| 49 | log.info "Creating tasks..." |
---|
| 50 | createBulkTestTasks() |
---|
| 51 | |
---|
| 52 | // createBulkTestEntries() |
---|
| 53 | // createBulkTestAssignedGroups() |
---|
| 54 | // createBulkTestAssignedPersons() |
---|
| 55 | // createBulkTestTaskRecurringSchedules() |
---|
| 56 | |
---|
| 57 | // Inventory |
---|
| 58 | // createBulkTestInventoryStores() /// @todo: Perhaps a 'createQuickStartData' method? |
---|
| 59 | // createBulkTestInventoryLocations() |
---|
| 60 | // createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method? |
---|
| 61 | // createBulkTestInventoryItems() |
---|
| 62 | |
---|
| 63 | // Assets |
---|
| 64 | // createBulkTestLifePlan() |
---|
| 65 | // createBulkTestTaskProcedure() |
---|
| 66 | // createBulkTestMaintenanceActions() |
---|
| 67 | // createBulkTestSystemSections() |
---|
| 68 | // createBulkTestAssetTypes() |
---|
| 69 | // createBulkTestAssemblies() |
---|
| 70 | // createBulkTestSubAssemblies() |
---|
| 71 | // createBulkTestComponentItems() |
---|
| 72 | // createBulkTestAssets() |
---|
| 73 | // createBulkTestAssetExtenedAttributes() |
---|
| 74 | |
---|
| 75 | log.info "Creating BULK data...complete." |
---|
| 76 | return true |
---|
| 77 | |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | /****************** |
---|
| 81 | Start of Person |
---|
| 82 | *******************/ |
---|
| 83 | |
---|
| 84 | def createBulkTestPersons() { |
---|
| 85 | //Person |
---|
| 86 | def passClearText = "pass" |
---|
| 87 | def passwordEncoded = personService.encodePassword(passClearText) |
---|
| 88 | def personInstance |
---|
| 89 | |
---|
[261] | 90 | def start = Person.count() + 1 |
---|
| 91 | def end = start + 100 |
---|
[258] | 92 | |
---|
[261] | 93 | def range = start..end |
---|
| 94 | |
---|
[258] | 95 | def loginName = "BtLoginName" |
---|
| 96 | String btLoginName |
---|
| 97 | def firstName = "BtFirstName" |
---|
| 98 | String btFirstName |
---|
| 99 | def lastName = "BtLastName" |
---|
| 100 | |
---|
| 101 | def authority2 = Authority.get(2) |
---|
| 102 | def authority3 = Authority.get(3) |
---|
| 103 | def personGroup1 = PersonGroup.get(1) |
---|
| 104 | def personGroup2 = PersonGroup.get(2) |
---|
| 105 | def personGroup3 = PersonGroup.get(3) |
---|
| 106 | def personGroup4 = PersonGroup.get(4) |
---|
| 107 | def personGroup5 = PersonGroup.get(5) |
---|
| 108 | |
---|
| 109 | range.each() { |
---|
| 110 | |
---|
| 111 | btLoginName = loginName + it |
---|
| 112 | btFirstName = firstName + it |
---|
| 113 | |
---|
| 114 | personInstance = new Person(loginName: btLoginName, |
---|
| 115 | firstName: btFirstName, |
---|
| 116 | lastName: lastName, |
---|
| 117 | pass: passClearText, |
---|
| 118 | password: passwordEncoded, |
---|
| 119 | email: "bulkTest@example.com") |
---|
| 120 | saveAndTest(personInstance) |
---|
| 121 | personInstance.addToAuthorities(authority2) |
---|
| 122 | personInstance.addToAuthorities(authority3) |
---|
| 123 | personInstance.addToPersonGroups(personGroup1) |
---|
| 124 | personInstance.addToPersonGroups(personGroup2) |
---|
| 125 | personInstance.addToPersonGroups(personGroup3) |
---|
| 126 | personInstance.addToPersonGroups(personGroup4) |
---|
| 127 | personInstance.addToPersonGroups(personGroup5) |
---|
| 128 | |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | } // createBulkTestPersons() |
---|
| 132 | |
---|
| 133 | /********************* |
---|
| 134 | START OF TASK |
---|
| 135 | *********************/ |
---|
| 136 | |
---|
| 137 | def createBulkTestTasks() { |
---|
| 138 | |
---|
| 139 | def taskResult |
---|
| 140 | def p = [:] |
---|
| 141 | |
---|
[261] | 142 | def start = Task.count() + 1 |
---|
| 143 | def end = start + 10000 |
---|
[258] | 144 | |
---|
[261] | 145 | def range = start..end |
---|
[258] | 146 | |
---|
[261] | 147 | |
---|
[258] | 148 | def taskGroup1 = TaskGroup.get(1) |
---|
| 149 | def taskPriority2 = TaskPriority.get(2) |
---|
| 150 | def taskType1 = TaskType.get(1) |
---|
| 151 | def leadPerson2 = Person.get(2) |
---|
| 152 | |
---|
| 153 | def description = "Bulk test data " |
---|
| 154 | String btDescription |
---|
| 155 | def comment1 = "Has been noted as problematic, try recalibrating." |
---|
| 156 | def today = dateUtilService.today |
---|
| 157 | |
---|
[261] | 158 | startTime = System.currentTimeMillis() |
---|
| 159 | lastBatchStarted = startTime |
---|
| 160 | |
---|
[258] | 161 | range.each() { |
---|
| 162 | |
---|
[261] | 163 | if(it % 100 == 0) { |
---|
[258] | 164 | logStatus("Creating task #" + it) |
---|
| 165 | cleanUpGorm() |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | btDescription = description + it |
---|
| 169 | |
---|
| 170 | //Task #1 |
---|
| 171 | p = [taskGroup: taskGroup1, |
---|
| 172 | taskPriority: taskPriority2, |
---|
| 173 | taskType: taskType1, |
---|
| 174 | leadPerson: leadPerson2, |
---|
| 175 | description: btDescription, |
---|
| 176 | comment: comment1, |
---|
| 177 | targetStartDate: today] |
---|
| 178 | |
---|
| 179 | taskResult = taskService.create(p) |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | def createBulkTestEntries() { |
---|
| 185 | |
---|
| 186 | def entryResult |
---|
| 187 | def p = [:] |
---|
| 188 | |
---|
| 189 | def range = 1..10 |
---|
| 190 | def task1 = Task.get(1) |
---|
| 191 | def entryType1 = EntryType.get(1) |
---|
| 192 | def comment1 = "This is a bulk test entry." |
---|
| 193 | def durationMinute1 = 20 |
---|
| 194 | |
---|
| 195 | range.each() { |
---|
| 196 | |
---|
| 197 | p = [task: task1, |
---|
| 198 | entryType: entryType1, |
---|
| 199 | comment: comment1, |
---|
| 200 | durationMinute: durationMinute1] |
---|
| 201 | |
---|
| 202 | entryResult = taskService.createEntry(p) |
---|
| 203 | |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | } // createBulkTestEntries() |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | * This cleans up the hibernate session and a grails map. |
---|
| 210 | * For more info see: http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/ |
---|
| 211 | * The hibernate session flush is normal for hibernate. |
---|
| 212 | * The map is apparently used by grails for domain object validation errors. |
---|
| 213 | * A starting point for clean up is every 100 objects. |
---|
| 214 | */ |
---|
| 215 | def cleanUpGorm() { |
---|
| 216 | def session = sessionFactory.currentSession |
---|
| 217 | session.flush() |
---|
| 218 | session.clear() |
---|
| 219 | propertyInstanceMap.get().clear() |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | def logStatus(String message) { |
---|
| 223 | def batchEnded = System.currentTimeMillis() |
---|
| 224 | def seconds = (batchEnded-lastBatchStarted)/1000 |
---|
| 225 | def total = (batchEnded-startTime)/1000 |
---|
| 226 | log.info "${message}, last: ${seconds}s, total: ${total}s" |
---|
| 227 | lastBatchStarted = batchEnded |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | /**************************************** |
---|
| 232 | Call this function instead of .save() |
---|
| 233 | *****************************************/ |
---|
| 234 | private boolean saveAndTest(object) { |
---|
| 235 | if(!object.save()) { |
---|
| 236 | // BulkTestDataSuccessful = false |
---|
| 237 | log.error "'${object}' failed to save!" |
---|
| 238 | log.error object.errors |
---|
| 239 | return false |
---|
| 240 | } |
---|
| 241 | return true |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | } // end class. |
---|