Author: manik.surtani(a)jboss.com
Date: 2008-10-13 12:59:48 -0400 (Mon, 13 Oct 2008)
New Revision: 6917
Added:
core/branches/flat/src/test/java/org/jboss/starobrno/lock/
core/branches/flat/src/test/java/org/jboss/starobrno/lock/LockContainerHashingTest.java
Log:
hash distribution test
Added:
core/branches/flat/src/test/java/org/jboss/starobrno/lock/LockContainerHashingTest.java
===================================================================
---
core/branches/flat/src/test/java/org/jboss/starobrno/lock/LockContainerHashingTest.java
(rev 0)
+++
core/branches/flat/src/test/java/org/jboss/starobrno/lock/LockContainerHashingTest.java 2008-10-13
16:59:48 UTC (rev 6917)
@@ -0,0 +1,92 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.starobrno.lock;
+
+import org.jboss.cache.util.concurrent.locks.LockContainer;
+import org.jboss.cache.util.concurrent.locks.ReentrantLockContainer;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.locks.Lock;
+
+@Test(groups = "unit")
+public class LockContainerHashingTest
+{
+ private LockContainer<String> stripedLock;
+
+ @BeforeMethod(alwaysRun = true)
+ public void setUp()
+ {
+ stripedLock = new ReentrantLockContainer<String>(500);
+ }
+
+ public void testHashingDistribution()
+ {
+ // ensure even bucket distribution of lock stripes
+ List<String> keys = createRandomKeys(1000);
+
+ Map<Lock, Integer> distribution = new HashMap<Lock, Integer>();
+
+ for (String s : keys)
+ {
+ Lock lock = stripedLock.getLock(s);
+ if (distribution.containsKey(lock))
+ {
+ int count = distribution.get(lock) + 1;
+ distribution.put(lock, count);
+ }
+ else
+ {
+ distribution.put(lock, 1);
+ }
+ }
+
+ System.out.println(distribution);
+
+ // cannot be larger than the number of locks
+ System.out.println("dist size: " + distribution.size());
+ System.out.println("num shared locks: " + stripedLock.getSize());
+ assert distribution.size() <= stripedLock.getSize();
+ // assume at least a 2/3rd spread
+ assert distribution.size() * 1.5 >= stripedLock.getSize();
+ }
+
+ private List<String> createRandomKeys(int number)
+ {
+
+ List<String> f = new ArrayList<String>(number);
+ Random r = new Random();
+ int i = number;
+ while (f.size() < number)
+ {
+ String s = i + "baseKey" + (10000 + i++);
+ f.add(s);
+ }
+
+ return f;
+ }
+}