001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/course-management/tags/sakai_2-4-1/cm-impl/hibernate-impl/impl/src/test/org/sakaiproject/coursemanagement/test/performance/IndexPerformance.java $
003: * $Id: IndexPerformance.java 22560 2007-03-13 19:46:27Z jholtzman@berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.coursemanagement.test.performance;
021:
022: import java.util.HashSet;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.sakaiproject.coursemanagement.api.CourseManagementAdministration;
029: import org.sakaiproject.coursemanagement.api.CourseManagementService;
030: import org.sakaiproject.coursemanagement.api.Enrollment;
031: import org.sakaiproject.coursemanagement.api.Membership;
032: import org.sakaiproject.coursemanagement.api.Section;
033: import org.sakaiproject.coursemanagement.api.SectionCategory;
034: import org.springframework.context.ApplicationContext;
035: import org.springframework.context.support.ClassPathXmlApplicationContext;
036: import org.springframework.transaction.PlatformTransactionManager;
037: import org.springframework.transaction.TransactionDefinition;
038: import org.springframework.transaction.TransactionStatus;
039: import org.springframework.transaction.support.DefaultTransactionDefinition;
040:
041: public class IndexPerformance {
042: private static final Log log = LogFactory
043: .getLog(IndexPerformance.class);
044:
045: private static final int secCount = 10;
046: private static final int enrollmentsPerEnrollmentSet = 100;
047: private static final int membersPerSection = 100;
048:
049: private static final String esPrefix = "IndexPerf ES ";
050: private static final String secPrefix = "IndexPerf SEC ";
051:
052: protected ApplicationContext appContext;
053: protected CourseManagementAdministration cmAdmin;
054: protected CourseManagementService cmService;
055: protected PlatformTransactionManager tm;
056:
057: public IndexPerformance() {
058: init();
059: }
060:
061: public void init() {
062: appContext = new ClassPathXmlApplicationContext(new String[] {
063: "spring-test.xml", "spring-config-test.xml" });
064: cmAdmin = (CourseManagementAdministration) appContext
065: .getBean(CourseManagementAdministration.class.getName());
066: cmService = (CourseManagementService) appContext
067: .getBean(CourseManagementService.class.getName());
068: tm = (PlatformTransactionManager) appContext
069: .getBean("cmTransactionManager");
070: }
071:
072: public void loadLotsOfData() {
073: DefaultTransactionDefinition def = new DefaultTransactionDefinition();
074: def
075: .setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
076: TransactionStatus status = tm.getTransaction(def);
077:
078: try {
079: String asId = "IndexPerf AS";
080: cmAdmin.createAcademicSession(asId, asId, asId, null, null);
081:
082: String csId = "IndexPerf CS";
083: cmAdmin.createCourseSet(csId, csId, csId, "DEPT", null);
084:
085: String ccId = "IndexPerf CC";
086: cmAdmin.createCanonicalCourse(ccId, ccId, ccId);
087:
088: String coId = "IndecPerf CO";
089: cmAdmin.createCourseOffering(coId, csId, csId, "open",
090: asId, ccId, null, null);
091:
092: for (int i = 1; i <= secCount; i++) {
093: String esId = esPrefix + i;
094: Set<String> instructors = new HashSet<String>();
095: instructors.add("instructor_A_" + i);
096: instructors.add("instructor_B_" + i);
097: instructors.add("instructor_C_" + i);
098: cmAdmin.createEnrollmentSet(esId, esId, esId,
099: "lecture", "3", coId, instructors);
100: }
101:
102: for (int i = 1; i <= secCount; i++) {
103: String secId = secPrefix + i;
104: cmAdmin.createSection(secId, secId, secId, "lecture",
105: null, coId, (esPrefix + i));
106: }
107:
108: for (int i = 1; i <= secCount; i++) {
109: for (int j = 1; j <= enrollmentsPerEnrollmentSet; j++) {
110: cmAdmin.addOrUpdateEnrollment("student" + j,
111: esPrefix + i, "enrolled", "3",
112: "letter grade");
113: }
114: }
115:
116: for (int i = 1; i <= secCount; i++) {
117: for (int j = 1; j <= membersPerSection; j++) {
118: cmAdmin.addOrUpdateSectionMembership("student" + j,
119: "some role", secPrefix + i, "some status");
120: }
121: }
122: } catch (Exception e) {
123: tm.rollback(status);
124: } finally {
125: if (!status.isCompleted()) {
126: tm.commit(status);
127: }
128: }
129: }
130:
131: public long getTimeToSelectEnrollments(String studentEid,
132: String esEid) {
133: long start = System.currentTimeMillis();
134: Enrollment enr = cmService.findEnrollment(studentEid, esEid);
135: long end = System.currentTimeMillis();
136: return end - start;
137: }
138:
139: public long getTimeToSelectMembers(String studentEid) {
140: long start = System.currentTimeMillis();
141: Map<String, String> roleMap = cmService
142: .findSectionRoles(studentEid);
143: long end = System.currentTimeMillis();
144: return end - start;
145: }
146:
147: public long getTimeToSelectInstructors(String instructorEid) {
148: long start = System.currentTimeMillis();
149: Set<Section> sections = cmService
150: .findInstructingSections(instructorEid);
151: long end = System.currentTimeMillis();
152: return end - start;
153: }
154:
155: // Static methods
156:
157: public static void main(String[] args) {
158: IndexPerformance indexPerf = new IndexPerformance();
159: indexPerf.loadLotsOfData();
160: logEnrollmentSelects(indexPerf);
161: logMemberSelects(indexPerf);
162: logInstructorSelects(indexPerf);
163: }
164:
165: private static void logEnrollmentSelects(IndexPerformance indexPerf) {
166: long total = 0;
167: for (int i = 1; i <= enrollmentsPerEnrollmentSet; i++) {
168: long this Run = indexPerf.getTimeToSelectEnrollments(
169: "student" + i, esPrefix + 1);
170: total += this Run;
171: }
172: double esMean = (double) total
173: / (double) enrollmentsPerEnrollmentSet;
174: log.info("Mean time (in milliseconds) to select Enrollments:\t"
175: + esMean);
176: }
177:
178: private static void logMemberSelects(IndexPerformance indexPerf) {
179: long total = 0;
180: for (int i = 1; i <= membersPerSection; i++) {
181: long this Run = indexPerf.getTimeToSelectMembers("student"
182: + i);
183: total += this Run;
184: }
185: double secMean = (double) total / (double) membersPerSection;
186: log.info("Mean time (in milliseconds) to select Memberships:\t"
187: + secMean);
188: }
189:
190: private static void logInstructorSelects(IndexPerformance indexPerf) {
191: long total = 0;
192: for (int i = 1; i <= secCount; i++) {
193: long this Run = indexPerf
194: .getTimeToSelectInstructors("instructor_B_" + i);
195: total += this Run;
196: }
197: double secMean = (double) total / (double) secCount;
198: log.info("Mean time (in milliseconds) to select Instructors:\t"
199: + secMean);
200: }
201:
202: }
|