SHARE
TWEET

Untitled

a guest Mar 20th, 2016 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sun Mar 20 18:40:11 IST 2016::Starting
  2. Sun Mar 20 18:43:03 IST 2016::Generated test data, count 40000000
  3. Doing 20000000 validations.
  4. Sun Mar 20 18:43:30 IST 2016 Done in 26860 msecs.
  5. 0.001343 milliseconds per validation
  6.    
  7. import java.util.Date;
  8. import java.util.HashSet;
  9. import java.util.Random;
  10. import java.util.Set;
  11.  
  12. public class UserValidator {
  13.  
  14.     Set<String> validCredentials=new HashSet<>();
  15.     long numCredentials = 1000*1000*60;
  16.     Random rnd = new Random();
  17.     char[] validChars=null;
  18.  
  19.     public UserValidator(){
  20.         String chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*~*()";
  21.         validChars = chars.toCharArray();
  22.     }
  23.  
  24.     String generateRandomString(int len){
  25.            StringBuilder sb = new StringBuilder( len );
  26.            for( int i = 0; i < len; i++ )
  27.               sb.append( validChars[ rnd.nextInt(validChars.length) ] );
  28.            return sb.toString();
  29.     }
  30.  
  31.     String getRansomString(){
  32.         int len = 4;
  33.         len = len+rnd.nextInt(20);
  34.         return generateRandomString(len);
  35.     }
  36.  
  37.  
  38.     void generateTestData(){
  39.         for(long i=0;i<numCredentials;i++){
  40.             String username=getRansomString();
  41.             String password=getRansomString();
  42.             String credentials=username+":::"+new String(username+":"+password).hashCode();
  43.             validCredentials.add(credentials);
  44.         }
  45.     }
  46.  
  47.     boolean isValidUser(String user, String password){
  48.         String credentials=user+":::"+new String(user+":"+password).hashCode();
  49.         return validCredentials.contains(credentials);
  50.     }
  51.  
  52.     public static void main(String[] args) {
  53.         UserValidator u = new UserValidator();
  54.         System.out.println(new Date().toString()+"::Starting");
  55.         u.generateTestData();
  56.         System.out.println(new Date().toString()+"::Generated test data, count "+u.validCredentials.size());
  57.         int lookups=20*1000*1000;
  58.         System.out.println("Doing "+lookups+" validations.");
  59.         long timeStart = new Date().getTime();
  60.         for(int i=0;i<lookups;i++){
  61.             u.isValidUser(u.getRansomString(), u.getRansomString());
  62.         }
  63.         long timeEnd = new Date().getTime();
  64.         long timeSpent = (timeEnd-timeStart);
  65.         System.out.println(new Date().toString()+" Done in "+timeSpent+" msecs.");
  66.         System.out.println((double)timeSpent/(double)lookups +" milliseconds per validation");
  67.     }
  68.  
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top