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

Last change on this file since 561 was 548, checked in by gav, 15 years ago

Add InventoryItemService savePicture().
Add bulk tests for inventory items.

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