Package org.olat.user

Source Code of org.olat.user.EmailCheckPerformanceTest

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS,
* <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) 2007 frentix GmbH, Switzerland<br>
* <p>
*/
package org.olat.user;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;
import org.olat.basesecurity.AuthHelper;
import org.olat.basesecurity.Constants;
import org.olat.basesecurity.Manager;
import org.olat.basesecurity.ManagerFactory;
import org.olat.core.commons.persistence.DB;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.id.Identity;
import org.olat.core.id.User;
import org.olat.core.id.UserConstants;
import org.olat.core.logging.Tracing;
import org.olat.core.test.OlatTestCase;
import org.olat.core.util.StringHelper;
import org.olat.login.OLATAuthenticationController;

/**
* Performance test for check if email exist
*
* @author Christian Guretzki
*/
public class EmailCheckPerformanceTest extends OlatTestCase {
  private static boolean isInitialized = false;

  private static Logger log = Logger.getLogger(EmailCheckPerformanceTest.class.getName());

  private static long createUserTime;
  private static long testExistEmailAddressTime;
  /**
   * @param arg
   */
  public EmailCheckPerformanceTest(String arg) {
    super(arg);
  }

  /**
   * @see junit.framework.TestCase#setUp()
   */
  protected void setUp()throws Exception {
    super.setUp();
    System.out.println("V 0.1 23.04.08/10:00");
    if (isInitialized == false) {
      // start with fresh database
      DBFactory.getJunitInstance().clearDatabase();
      createUsers();
      isInitialized = true;
    }
  }

  /**
   * TearDown is called after each test
   */
  public void tearDown() {
    try {
      DB db = DBFactory.getInstance();
      db.closeSession();
    } catch (Exception e) {
      log.error("Exception in tearDown(): " + e);
    }
  }

 
 
//  public void testExistEmailAddress() throws Exception {
//    System.out.println("testExistEmailAddress...");
//    int MAX_LOOP = 100;
//    long startTime = System.currentTimeMillis();
//    for (int i = 0; i<MAX_LOOP; i++) {
//      boolean test = ManagerFactory.getManager().existEmailAddress("test_" + i + "@test.ti");
//      if (test == true) {
//        System.out.println("TEST EMAIL EXIST 1");
//      }
//    }
//    long endTime = System.currentTimeMillis();
//    testExistEmailAddressTime = (endTime - startTime);
//    log.info("testExistEmailAddress takes time=" + (endTime - startTime) ); 
//  }

  public void testUserManger() throws Exception {
    System.out.println("testUserManger start...");
    int MAX_LOOP = 100;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i<MAX_LOOP; i++) {
      Identity test = UserManager.getInstance().findIdentityByEmail("test_" + i + "@test.ti");
      if (test != null) {
        System.out.println("TEST EMAIL EXIST 2");
      }
    }
    long endTime = System.currentTimeMillis();
    log.info("testUserManger takes time=" + (endTime - startTime) + " ; testExistEmailAddressTime=" + testExistEmailAddressTime + " ; createUserTime=" + createUserTime)
  }

  /**
   * Export all test cases as suite. Make sure you add your testcase to
   * org.olat.test.AllTest.java
   * @return
   * @throws Exception
   */
  public static Test suite() throws Exception {
    return new TestSuite(EmailCheckPerformanceTest.class);
  }
 
 
  private void createUsers() {
    int numberUsers = 10000;
    String username;
    String institution;
    String gender;
   
    long startTime = System.currentTimeMillis();

    UserManager um = UserManager.getInstance();
    Manager sm = ManagerFactory.getManager();

    // create users group
    sm.createAndPersistNamedSecurityGroup(Constants.GROUP_OLATUSERS);
   
    System.out.println("TEST start creating " + numberUsers + " testusers");
    for (int i = 1; i < numberUsers+1; i++) {
      username = i + "email_test";
      if (i % 2 == 0) {
        institution = "myinst";
        gender = "m";
      } else {
        institution = "yourinst";
        gender = "f";
      }
      User user = UserManager.getInstance().createUser(username + "first", username + "last", username + "@test.test");
      user.setProperty(UserConstants.GENDER, gender);
      user.setProperty(UserConstants.BIRTHDAY, "24.07.3007");
      user.setProperty(UserConstants.STREET, "Zähringerstrasse 26");
      user.setProperty(UserConstants.EXTENDEDADDRESS, null);
      user.setProperty(UserConstants.POBOX, null);
      user.setProperty(UserConstants.CITY, "Zürich");
      user.setProperty(UserConstants.COUNTRY, "Switzerland");
      user.setProperty(UserConstants.TELMOBILE, "123456789");
      user.setProperty(UserConstants.TELOFFICE, "123456789");
      user.setProperty(UserConstants.TELPRIVATE, "123456789");
      user.setProperty(UserConstants.INSTITUTIONALEMAIL, username + "@" + institution);
      user.setProperty(UserConstants.INSTITUTIONALNAME, institution);
      user.setProperty(UserConstants.INSTITUTIONALUSERIDENTIFIER, username + "-" + institution);
      AuthHelper.createAndPersistIdentityAndUserWithUserGroup(username, "hokuspokus", user);

      if (i % 10 == 0) {
        // flush now to obtimize performance
        DBFactory.getInstance().closeSession();
        System.out.print(".");
      }
    }
    long endTime = System.currentTimeMillis();
    createUserTime = (endTime - startTime);
    System.out.println("TEST created " + numberUsers + " testusers in createUserTime=" + createUserTime);
   
  }
}
TOP

Related Classes of org.olat.user.EmailCheckPerformanceTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.