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

Last change on this file since 378 was 291, checked in by gav, 15 years ago

Svn move PersonService to AuthService?.

File size: 6.9 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
16
17    def sessionFactory
18    def propertyInstanceMap = org.codehaus.groovy.grails.plugins.DomainClassGrailsPlugin.PROPERTY_INSTANCE_MAP
19
[261]20    def startTime
21    def lastBatchStarted
[258]22
23/*******************************************
24Start of Group methods.
25Generally use these methods to create data.
26*******************************************/
27
28    /**
29    * Make a run of data creation.
30    */
31    def create() {
32        if(!GrailsUtil.environment == "development") {
33            log.error "Dev environment not detected, will NOT create bulk data."
34            return false
35        }
36
37        log.info "Creating BULK data..."
38
39        // Person and Utils
40        log.info "Creating persons..."
41        createBulkTestPersons()
42
43//         createBulkTestSites()
44//         createBulkTestDepartments()
45//         createBulkTestSuppliers()
46//         createBulkTestManufacturers()
47
48        // Tasks
49        log.info "Creating tasks..."
50        createBulkTestTasks()
51
52//         createBulkTestEntries()
53//         createBulkTestAssignedGroups()
54//         createBulkTestAssignedPersons()
55//         createBulkTestTaskRecurringSchedules()
56
57        // Inventory
58//         createBulkTestInventoryStores()  /// @todo: Perhaps a 'createQuickStartData' method?
59//         createBulkTestInventoryLocations()
60//         createBulkTestInventoryGroups() /// @todo: Perhaps a 'createQuickStartData' method?
61//         createBulkTestInventoryItems()
62
63        // Assets
64//         createBulkTestLifePlan()
65//         createBulkTestTaskProcedure()
66//         createBulkTestMaintenanceActions()
[269]67//         createBulkTestSections()
[258]68//         createBulkTestAssets()
69//         createBulkTestAssetExtenedAttributes()
[269]70//         createBulkTestAssetSubItems()
71//         createBulkTestAssetSubItemExtenedAttributes()
[258]72
73        log.info "Creating BULK data...complete."
74        return true
75
76    }
77
78/******************
79Start of Person
80*******************/
81
82    def createBulkTestPersons() {
83        //Person
84        def passClearText = "pass"
[291]85        def passwordEncoded = authService.encodePassword(passClearText)
[258]86        def personInstance
87
[261]88        def start = Person.count() + 1
89        def end = start + 100
[258]90
[261]91        def range = start..end
92
[258]93        def loginName = "BtLoginName"
94        String btLoginName
95        def firstName = "BtFirstName"
96        String btFirstName
97        def lastName = "BtLastName"
98
99        def authority2 = Authority.get(2)
100        def authority3 = Authority.get(3)
101        def personGroup1 = PersonGroup.get(1)
102        def personGroup2 = PersonGroup.get(2)
103        def personGroup3 = PersonGroup.get(3)
104        def personGroup4 = PersonGroup.get(4)
105        def personGroup5 = PersonGroup.get(5)
106
107        range.each() {
108
109            btLoginName = loginName + it
110            btFirstName = firstName + it
111
112            personInstance = new Person(loginName: btLoginName,
113                                        firstName: btFirstName,
114                                        lastName: lastName,
115                                        pass: passClearText,
116                                        password: passwordEncoded,
117                                        email: "bulkTest@example.com")
118            saveAndTest(personInstance)
119            personInstance.addToAuthorities(authority2)
120            personInstance.addToAuthorities(authority3)
121            personInstance.addToPersonGroups(personGroup1)
122            personInstance.addToPersonGroups(personGroup2)
123            personInstance.addToPersonGroups(personGroup3)
124            personInstance.addToPersonGroups(personGroup4)
125            personInstance.addToPersonGroups(personGroup5)
126
127        }
128
129    } // createBulkTestPersons()
130
131/*********************
132START OF TASK
133*********************/
134
135    def createBulkTestTasks() {
136
137        def taskResult
138        def p = [:]
139
[261]140        def start = Task.count() + 1
141        def end = start + 10000
[258]142
[261]143        def range = start..end
[258]144
[261]145
[258]146        def taskGroup1 = TaskGroup.get(1)
147        def taskPriority2 = TaskPriority.get(2)
148        def taskType1 = TaskType.get(1)
149        def leadPerson2 = Person.get(2)
150
151        def description = "Bulk test data "
152        String btDescription
153        def comment1 = "Has been noted as problematic, try recalibrating."
154        def today = dateUtilService.today
155
[261]156        startTime = System.currentTimeMillis()
157        lastBatchStarted = startTime
158
[258]159        range.each() {
160
[261]161            if(it % 100 == 0) {
[258]162                logStatus("Creating task #" + it)
163                cleanUpGorm()
164            }
165
166            btDescription = description + it
167
168            //Task #1
169            p = [taskGroup: taskGroup1,
170                    taskPriority: taskPriority2,
171                    taskType: taskType1,
172                    leadPerson: leadPerson2,
173                    description: btDescription,
174                    comment: comment1,
175                    targetStartDate: today]
176
177            taskResult = taskService.create(p)
178        }
179
180    }
181
182    def createBulkTestEntries() {
183
184        def entryResult
185        def p = [:]
186
187        def range = 1..10
188        def task1 = Task.get(1)
189        def entryType1 = EntryType.get(1)
190        def comment1 = "This is a bulk test entry."
191        def durationMinute1 = 20
192
193        range.each() {
194
195            p = [task: task1,
196                    entryType: entryType1,
197                    comment: comment1,
198                    durationMinute: durationMinute1]
199
200            entryResult = taskService.createEntry(p)
201
202        }
203
204    } // createBulkTestEntries()
205
206    /**
207    * This cleans up the hibernate session and a grails map.
208    * For more info see: http://naleid.com/blog/2009/10/01/batch-import-performance-with-grails-and-mysql/
209    * The hibernate session flush is normal for hibernate.
210    * The map is apparently used by grails for domain object validation errors.
211    * A starting point for clean up is every 100 objects.
212    */
213    def cleanUpGorm() {
214        def session = sessionFactory.currentSession
215        session.flush()
216        session.clear()
217        propertyInstanceMap.get().clear()
218    }
219
220    def logStatus(String message) {
221        def batchEnded = System.currentTimeMillis()
222        def seconds = (batchEnded-lastBatchStarted)/1000
223        def total = (batchEnded-startTime)/1000
224        log.info "${message}, last: ${seconds}s, total: ${total}s"
225        lastBatchStarted = batchEnded
226    }
227
228
229/****************************************
230Call this function instead of .save()
231*****************************************/
232    private boolean saveAndTest(object) {
233        if(!object.save()) {
234//             BulkTestDataSuccessful = false
235            log.error "'${object}' failed to save!"
236            log.error object.errors
237            return false
238        }
239        return true
240    }
241
242} // end class.
Note: See TracBrowser for help on using the repository browser.