source: trunk/grails-app/services/CreateBulkDataService.groovy @ 891

Last change on this file since 891 was 751, checked in by gav, 14 years ago

Domain change: remove redundant LifePlan? domain.

File size: 11.1 KB
RevLine 
[258]1import grails.util.GrailsUtil
2
3/**
4* Provides a data service to create a large volume of test data for load testing.
5*/
6class  CreateBulkDataService {
7
8    boolean transactional = false
9
[291]10    def authService
[258]11    def taskService
12    def dateUtilService
13    def appConfigService
[622]14    def createDataService
[572]15    def searchableService
[258]16    def assignedGroupService
17    def assignedPersonService
[548]18    def inventoryItemService
[258]19
20    def sessionFactory
[548]21    def grailsApplication
[258]22    def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP
23
[261]24    def startTime
25    def lastBatchStarted
[258]26
27/*******************************************
28Start of Group methods.
29Generally use these methods to create data.
30*******************************************/
31
32    /**
33    * Make a run of data creation.
34    */
[548]35    def createAll() {
36        def result = [:]
37
38        def fail = { Map m ->
39            result.error = [ code: m.code, args: m.args ]
40            return result
[258]41        }
42
[548]43        if(GrailsUtil.environment != "development")
44            return fail(code: 'default.not.development.environment.failure')
45
[622]46        createDataService.stopSearchableIndex()
[572]47
[258]48        log.info "Creating BULK data..."
49
50        // Person and Utils
51        log.info "Creating persons..."
52        createBulkTestPersons()
53//         createBulkTestSites()
54//         createBulkTestDepartments()
55//         createBulkTestSuppliers()
56
[548]57        // Assets
58//         createBulkTestTaskProcedure()
59//         createBulkTestMaintenanceActions()
60//         createBulkTestSections()
61//         createBulkTestAssets()
62//         createBulkTestAssetExtenedAttributes()
63//         createBulkTestAssetSubItems()
64//         createBulkTestAssetSubItemExtenedAttributes()
65
66        // Inventory
67        log.info "Creating inventory..."
68//         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
69        createBulkTestInventoryLocations()
70//         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
71        createBulkTestInventoryItems()
72
[258]73        // Tasks
74        log.info "Creating tasks..."
75        createBulkTestTasks()
76//         createBulkTestEntries()
77//         createBulkTestAssignedGroups()
78//         createBulkTestAssignedPersons()
79//         createBulkTestTaskRecurringSchedules()
80
[548]81        log.info "Creating BULK data...complete."
[572]82
[622]83        createDataService.startSearchableIndex()
[572]84
[548]85        return result
86
87    } // create()
88
89    /**
90    * Make a run of inventory data creation.
91    */
92    def createBulkInventoryTestData() {
93        def result = [:]
94
95        def fail = { Map m ->
96            result.error = [ code: m.code, args: m.args ]
97            return result
98        }
99
100        if(GrailsUtil.environment != "development")
101            return fail(code: 'default.not.development.environment.failure')
102
[622]103        createDataService.stopSearchableIndex()
[572]104
[548]105        log.info "Creating BULK data..."
106
[258]107        // Inventory
[548]108        log.info "Creating inventory..."
[258]109//         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
[548]110        createBulkTestInventoryLocations()
[258]111//         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
[548]112        createBulkTestInventoryItems()
[258]113
114        log.info "Creating BULK data...complete."
[572]115
[622]116        createDataService.startSearchableIndex()
[572]117
[548]118        return result
[258]119
[548]120    } // createBulkInventoryTestData()
[258]121
122/******************
123Start of Person
124*******************/
125
126    def createBulkTestPersons() {
127        //Person
128        def passClearText = "pass"
[291]129        def passwordEncoded = authService.encodePassword(passClearText)
[258]130        def personInstance
131
[261]132        def start = Person.count() + 1
133        def end = start + 100
[258]134
[261]135        def range = start..end
136
[258]137        def loginName = "BtLoginName"
138        String btLoginName
139        def firstName = "BtFirstName"
140        String btFirstName
141        def lastName = "BtLastName"
142
143        def authority2 = Authority.get(2)
144        def authority3 = Authority.get(3)
145        def personGroup1 = PersonGroup.get(1)
146        def personGroup2 = PersonGroup.get(2)
147        def personGroup3 = PersonGroup.get(3)
148        def personGroup4 = PersonGroup.get(4)
149        def personGroup5 = PersonGroup.get(5)
150
151        range.each() {
152
153            btLoginName = loginName + it
154            btFirstName = firstName + it
155
156            personInstance = new Person(loginName: btLoginName,
157                                        firstName: btFirstName,
158                                        lastName: lastName,
159                                        pass: passClearText,
[399]160                                        password: passwordEncoded)
[258]161            saveAndTest(personInstance)
162            personInstance.addToAuthorities(authority2)
163            personInstance.addToAuthorities(authority3)
164            personInstance.addToPersonGroups(personGroup1)
165            personInstance.addToPersonGroups(personGroup2)
166            personInstance.addToPersonGroups(personGroup3)
167            personInstance.addToPersonGroups(personGroup4)
168            personInstance.addToPersonGroups(personGroup5)
169
170        }
171
172    } // createBulkTestPersons()
173
174/*********************
175START OF TASK
176*********************/
177
178    def createBulkTestTasks() {
179
180        def taskResult
181        def p = [:]
182
[261]183        def start = Task.count() + 1
184        def end = start + 10000
[258]185
[261]186        def range = start..end
[258]187
[261]188
[258]189        def taskGroup1 = TaskGroup.get(1)
190        def taskPriority2 = TaskPriority.get(2)
[418]191        def taskType3 = TaskType.get(3)
[258]192        def leadPerson2 = Person.get(2)
193
194        def description = "Bulk test data "
195        String btDescription
196        def comment1 = "Has been noted as problematic, try recalibrating."
197        def today = dateUtilService.today
198
[261]199        startTime = System.currentTimeMillis()
200        lastBatchStarted = startTime
201
[258]202        range.each() {
203
[261]204            if(it % 100 == 0) {
[258]205                logStatus("Creating task #" + it)
206                cleanUpGorm()
207            }
208
209            btDescription = description + it
210
211            //Task #1
212            p = [taskGroup: taskGroup1,
213                    taskPriority: taskPriority2,
[418]214                    taskType: taskType3,
[258]215                    leadPerson: leadPerson2,
216                    description: btDescription,
217                    comment: comment1,
218                    targetStartDate: today]
219
[394]220            taskResult = taskService.save(p)
[258]221        }
222
[548]223    } // createBulkTestTasks()
[258]224
225    def createBulkTestEntries() {
226
227        def entryResult
228        def p = [:]
229
230        def range = 1..10
231        def task1 = Task.get(1)
232        def entryType1 = EntryType.get(1)
233        def comment1 = "This is a bulk test entry."
234        def durationMinute1 = 20
235
236        range.each() {
237
238            p = [task: task1,
239                    entryType: entryType1,
240                    comment: comment1,
241                    durationMinute: durationMinute1]
242
[394]243            entryResult = taskService.saveEntry(p)
[258]244
245        }
246
247    } // createBulkTestEntries()
248
[548]249
250/**************************
251START OF INVENTORY
252**************************/
253
254    def createBulkTestInventoryLocations() {
255
256        def inventoryLocationResult
257        def p = [:]
258
259        def start = InventoryLocation.count() + 1
260        def end = start + 50
261
262        def range = start..end
263
264
265        def inventoryStore1 = InventoryStore.read(1)
266
267        def name = "Bulk test location "
268        def btName = ''
269
270        startTime = System.currentTimeMillis()
271        lastBatchStarted = startTime
272
273        range.each() {
274
[572]275            if(it % 25 == 0) {
[548]276                logStatus("Creating inventory location #" + it)
277                cleanUpGorm()
278            }
279
280            btName = name + it
281
282            p = [inventoryStore: inventoryStore1,
283                    name: btName]
284
285            inventoryLocationResult = new InventoryLocation(p).save()
286        } // each()
287
288    } // createBulkTestInventoryLocations()
289
290    def createBulkTestInventoryItems() {
291
292        def inventoryItemInstance
293        def p = [:]
294
295        def pictureResource = grailsApplication.mainContext.getResource('images/logo.png')
296
297        def start = InventoryItem.count() + 1
298        def end = start + 250
299
300        def range = start..end
301
302        def inventoryLocation
303        def inventoryLocationIndex = 0
304        def inventoryLocationList = InventoryLocation.findAll()
305        def unitOfMeasure2 = UnitOfMeasure.read(2)
306        def inventoryType1 = InventoryType.read(1)
307        def inventoryGroup1 = InventoryGroup.read(1)
308
309        def name = "Bulk test inventory item "
310        def btName = ''
311
312        startTime = System.currentTimeMillis()
313        lastBatchStarted = startTime
314
315        range.each() {
316
[572]317            if(it % 50 == 0) {
[548]318                logStatus("Creating inventory item #" + it)
319                cleanUpGorm()
320            }
321
322            // Spread the inventoryItems across all available locations.
323            if(inventoryLocationIndex < inventoryLocationList.size()) {
324                inventoryLocation = inventoryLocationList[inventoryLocationIndex]
325            }
326            else {
327                inventoryLocationIndex = 0
328                inventoryLocation = inventoryLocationList[inventoryLocationIndex]
329            }
330            inventoryLocationIndex++
331
332            // Change the name for each inventoryItem.
333            btName = name + it
334
335            p = [inventoryGroup: inventoryGroup1,
336                    inventoryType: inventoryType1,
337                    unitOfMeasure: unitOfMeasure2,
338                    inventoryLocation: inventoryLocation,
339                    name: btName,
340                    description: "Bulk test data",
341                    unitsInStock: 2,
342                    reorderPoint: 0]
343
344            inventoryItemInstance = new InventoryItem(p)
345            saveAndTest(inventoryItemInstance)
346
347            def pictureResult = inventoryItemService.savePicture(inventoryItemInstance, pictureResource)
348
349            if(pictureResult.error)
350                log.error pictureResult.error
351        } // each()
352
353    } // createBulkTestInventoryItems()
354
[258]355    /**
356    * This cleans up the hibernate session and a grails map.
357    * For more info see: http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/
358    * The hibernate session flush is normal for hibernate.
359    * The map is apparently used by grails for domain object validation errors.
360    * A starting point for clean up is every 100 objects.
361    */
362    def cleanUpGorm() {
363        def session = sessionFactory.currentSession
364        session.flush()
365        session.clear()
366        propertyInstanceMap.get().clear()
367    }
368
369    def logStatus(String message) {
370        def batchEnded = System.currentTimeMillis()
371        def seconds = (batchEnded-lastBatchStarted)/1000
372        def total = (batchEnded-startTime)/1000
373        log.info "${message}, last: ${seconds}s, total: ${total}s"
374        lastBatchStarted = batchEnded
375    }
376
377
378/****************************************
379Call this function instead of .save()
380*****************************************/
381    private boolean saveAndTest(object) {
382        if(!object.save()) {
383//             BulkTestDataSuccessful = false
384            log.error "'${object}' failed to save!"
385            log.error object.errors
386            return false
387        }
388        return true
389    }
390
391} // end class.
Note: See TracBrowser for help on using the repository browser.