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

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

Apply bug fix for ticket #76.
Rename all instances of Lucene to Searchable.
Improved global directory config.
Add log levels for searchable plugin and compass.

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