1 4 package com.tc.object.lockmanager.api; 5 6 import java.util.Random ; 7 8 import junit.framework.TestCase; 9 10 public class LockRequestTest extends TestCase { 11 public void testEqualsAndHashCode() throws Exception { 12 Random random = new Random (); 13 int instanceCount = 1000000; 14 15 LockRequest notEqual = new LockRequest(new LockID("nothing like me"), new ThreadID(Integer.MAX_VALUE), 16 LockLevel.WRITE); 17 long[] samples = new long[instanceCount]; 18 for (int i = 0; i < instanceCount; i++) { 19 LockID lid = new LockID(random.nextInt(Integer.MAX_VALUE - 1) + ""); 20 ThreadID tid = new ThreadID(random.nextInt(Integer.MAX_VALUE - 1)); 21 int lockType = (i % 2 == 0) ? LockLevel.READ : LockLevel.WRITE; 22 23 long t0 = System.currentTimeMillis(); 24 LockRequest a = new LockRequest(lid, tid, lockType); 25 samples[i] = System.currentTimeMillis() - t0; 26 27 LockRequest b = new LockRequest(new LockID(lid.asString()), new ThreadID(tid.toLong()), lockType); 28 assertEquals(a, b); 29 assertEquals(a.hashCode(), b.hashCode()); 30 assertFalse(a.equals(notEqual)); 31 assertFalse(b.equals(notEqual)); 32 } 33 long min = 0, max = 0, sum = 0; 34 for (int i = 0; i < samples.length; i++) { 35 if (samples[i] < min) min = samples[i]; 36 if (samples[i] > max) max = samples[i]; 37 sum += samples[i]; 38 } 39 double mean = sum / samples.length; 40 System.out.println("min: " + min + ", max: " + max + ", mean: " + mean); 41 } 42 } 43 | Popular Tags |