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

Last change on this file since 746 was 722, checked in by gav, 14 years ago

Domain change: as per ticket #97 - Drop the entire Manufacturer domain concept.

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