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

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

Domain change: remove redundant LifePlan? domain.

File size: 11.1 KB
Line 
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
10    def authService
11    def taskService
12    def dateUtilService
13    def appConfigService
14    def createDataService
15    def searchableService
16    def assignedGroupService
17    def assignedPersonService
18    def inventoryItemService
19
20    def sessionFactory
21    def grailsApplication
22    def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP
23
24    def startTime
25    def lastBatchStarted
26
27/*******************************************
28Start of Group methods.
29Generally use these methods to create data.
30*******************************************/
31
32    /**
33    * Make a run of data creation.
34    */
35    def createAll() {
36        def result = [:]
37
38        def fail = { Map m ->
39            result.error = [ code: m.code, args: m.args ]
40            return result
41        }
42
43        if(GrailsUtil.environment != "development")
44            return fail(code: 'default.not.development.environment.failure')
45
46        createDataService.stopSearchableIndex()
47
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
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
73        // Tasks
74        log.info "Creating tasks..."
75        createBulkTestTasks()
76//         createBulkTestEntries()
77//         createBulkTestAssignedGroups()
78//         createBulkTestAssignedPersons()
79//         createBulkTestTaskRecurringSchedules()
80
81        log.info "Creating BULK data...complete."
82
83        createDataService.startSearchableIndex()
84
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
103        createDataService.stopSearchableIndex()
104
105        log.info "Creating BULK data..."
106
107        // Inventory
108        log.info "Creating inventory..."
109//         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
110        createBulkTestInventoryLocations()
111//         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
112        createBulkTestInventoryItems()
113
114        log.info "Creating BULK data...complete."
115
116        createDataService.startSearchableIndex()
117
118        return result
119
120    } // createBulkInventoryTestData()
121
122/******************
123Start of Person
124*******************/
125
126    def createBulkTestPersons() {
127        //Person
128        def passClearText = "pass"
129        def passwordEncoded = authService.encodePassword(passClearText)
130        def personInstance
131
132        def start = Person.count() + 1
133        def end = start + 100
134
135        def range = start..end
136
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,
160                                        password: passwordEncoded)
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
183        def start = Task.count() + 1
184        def end = start + 10000
185
186        def range = start..end
187
188
189        def taskGroup1 = TaskGroup.get(1)
190        def taskPriority2 = TaskPriority.get(2)
191        def taskType3 = TaskType.get(3)
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
199        startTime = System.currentTimeMillis()
200        lastBatchStarted = startTime
201
202        range.each() {
203
204            if(it % 100 == 0) {
205                logStatus("Creating task #" + it)
206                cleanUpGorm()
207            }
208
209            btDescription = description + it
210
211            //Task #1
212            p = [taskGroup: taskGroup1,
213                    taskPriority: taskPriority2,
214                    taskType: taskType3,
215                    leadPerson: leadPerson2,
216                    description: btDescription,
217                    comment: comment1,
218                    targetStartDate: today]
219
220            taskResult = taskService.save(p)
221        }
222
223    } // createBulkTestTasks()
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
243            entryResult = taskService.saveEntry(p)
244
245        }
246
247    } // createBulkTestEntries()
248
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
275            if(it % 25 == 0) {
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
317            if(it % 50 == 0) {
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
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.