[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 | |
---|
| 20 | def startTime = System.currentTimeMillis() |
---|
| 21 | def lastBatchStarted = startTime |
---|
| 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 | |
---|
| 90 | def range = 1..1000 |
---|
| 91 | |
---|
| 92 | def loginName = "BtLoginName" |
---|
| 93 | String btLoginName |
---|
| 94 | def firstName = "BtFirstName" |
---|
| 95 | String btFirstName |
---|
| 96 | def lastName = "BtLastName" |
---|
| 97 | |
---|
| 98 | def authority2 = Authority.get(2) |
---|
| 99 | def authority3 = Authority.get(3) |
---|
| 100 | def personGroup1 = PersonGroup.get(1) |
---|
| 101 | def personGroup2 = PersonGroup.get(2) |
---|
| 102 | def personGroup3 = PersonGroup.get(3) |
---|
| 103 | def personGroup4 = PersonGroup.get(4) |
---|
| 104 | def personGroup5 = PersonGroup.get(5) |
---|
| 105 | |
---|
| 106 | range.each() { |
---|
| 107 | |
---|
| 108 | btLoginName = loginName + it |
---|
| 109 | btFirstName = firstName + it |
---|
| 110 | |
---|
| 111 | personInstance = new Person(loginName: btLoginName, |
---|
| 112 | firstName: btFirstName, |
---|
| 113 | lastName: lastName, |
---|
| 114 | pass: passClearText, |
---|
| 115 | password: passwordEncoded, |
---|
| 116 | email: "bulkTest@example.com") |
---|
| 117 | saveAndTest(personInstance) |
---|
| 118 | personInstance.addToAuthorities(authority2) |
---|
| 119 | personInstance.addToAuthorities(authority3) |
---|
| 120 | personInstance.addToPersonGroups(personGroup1) |
---|
| 121 | personInstance.addToPersonGroups(personGroup2) |
---|
| 122 | personInstance.addToPersonGroups(personGroup3) |
---|
| 123 | personInstance.addToPersonGroups(personGroup4) |
---|
| 124 | personInstance.addToPersonGroups(personGroup5) |
---|
| 125 | |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | } // createBulkTestPersons() |
---|
| 129 | |
---|
| 130 | /********************* |
---|
| 131 | START OF TASK |
---|
| 132 | *********************/ |
---|
| 133 | |
---|
| 134 | def createBulkTestTasks() { |
---|
| 135 | |
---|
| 136 | def taskResult |
---|
| 137 | def p = [:] |
---|
| 138 | |
---|
| 139 | def range = 0..40000 |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | def taskGroup1 = TaskGroup.get(1) |
---|
| 143 | def taskPriority2 = TaskPriority.get(2) |
---|
| 144 | def taskType1 = TaskType.get(1) |
---|
| 145 | def leadPerson2 = Person.get(2) |
---|
| 146 | |
---|
| 147 | def description = "Bulk test data " |
---|
| 148 | String btDescription |
---|
| 149 | def comment1 = "Has been noted as problematic, try recalibrating." |
---|
| 150 | def today = dateUtilService.today |
---|
| 151 | |
---|
| 152 | range.each() { |
---|
| 153 | |
---|
| 154 | if(it % 1000 == 0) { |
---|
| 155 | logStatus("Creating task #" + it) |
---|
| 156 | cleanUpGorm() |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | btDescription = description + it |
---|
| 160 | |
---|
| 161 | //Task #1 |
---|
| 162 | p = [taskGroup: taskGroup1, |
---|
| 163 | taskPriority: taskPriority2, |
---|
| 164 | taskType: taskType1, |
---|
| 165 | leadPerson: leadPerson2, |
---|
| 166 | description: btDescription, |
---|
| 167 | comment: comment1, |
---|
| 168 | targetStartDate: today] |
---|
| 169 | |
---|
| 170 | taskResult = taskService.create(p) |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | def createBulkTestEntries() { |
---|
| 176 | |
---|
| 177 | def entryResult |
---|
| 178 | def p = [:] |
---|
| 179 | |
---|
| 180 | def range = 1..10 |
---|
| 181 | def task1 = Task.get(1) |
---|
| 182 | def entryType1 = EntryType.get(1) |
---|
| 183 | def comment1 = "This is a bulk test entry." |
---|
| 184 | def durationMinute1 = 20 |
---|
| 185 | |
---|
| 186 | range.each() { |
---|
| 187 | |
---|
| 188 | p = [task: task1, |
---|
| 189 | entryType: entryType1, |
---|
| 190 | comment: comment1, |
---|
| 191 | durationMinute: durationMinute1] |
---|
| 192 | |
---|
| 193 | entryResult = taskService.createEntry(p) |
---|
| 194 | |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | } // createBulkTestEntries() |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * This cleans up the hibernate session and a grails map. |
---|
| 201 | * For more info see: http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/ |
---|
| 202 | * The hibernate session flush is normal for hibernate. |
---|
| 203 | * The map is apparently used by grails for domain object validation errors. |
---|
| 204 | * A starting point for clean up is every 100 objects. |
---|
| 205 | */ |
---|
| 206 | def cleanUpGorm() { |
---|
| 207 | def session = sessionFactory.currentSession |
---|
| 208 | session.flush() |
---|
| 209 | session.clear() |
---|
| 210 | propertyInstanceMap.get().clear() |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | def logStatus(String message) { |
---|
| 214 | def batchEnded = System.currentTimeMillis() |
---|
| 215 | def seconds = (batchEnded-lastBatchStarted)/1000 |
---|
| 216 | def total = (batchEnded-startTime)/1000 |
---|
| 217 | log.info "${message}, last: ${seconds}s, total: ${total}s" |
---|
| 218 | lastBatchStarted = batchEnded |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | |
---|
| 222 | /**************************************** |
---|
| 223 | Call this function instead of .save() |
---|
| 224 | *****************************************/ |
---|
| 225 | private boolean saveAndTest(object) { |
---|
| 226 | if(!object.save()) { |
---|
| 227 | // BulkTestDataSuccessful = false |
---|
| 228 | log.error "'${object}' failed to save!" |
---|
| 229 | log.error object.errors |
---|
| 230 | return false |
---|
| 231 | } |
---|
| 232 | return true |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | } // end class. |
---|