[logback-dev] [GIT] Logback: the generic, reliable, fast and flexible logging framework. branch, master, updated. v_0.9.28-26-g5be4443
added by portage for gitosis-gentoo
git-noreply at pixie.qos.ch
Thu Mar 17 16:57:24 CET 2011
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "Logback: the generic, reliable, fast and flexible logging framework.".
The branch, master has been updated
via 5be4443a22e7f8b886b867a29c141432179c1d53 (commit)
from d88160e448083dcf5524bf926b45b91cf4ebb1a1 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://git.qos.ch/gitweb/?p=logback.git;a=commit;h=5be4443a22e7f8b886b867a29c141432179c1d53
http://github.com/ceki/logback/commit/5be4443a22e7f8b886b867a29c141432179c1d53
commit 5be4443a22e7f8b886b867a29c141432179c1d53
Author: Ceki Gulcu <ceki at qos.ch>
Date: Thu Mar 17 16:54:28 2011 +0100
working on LRUMessageCache
diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/turbo/LRUMessageCache.java b/logback-classic/src/main/java/ch/qos/logback/classic/turbo/LRUMessageCache.java
index 90cd274..cfa963f 100644
--- a/logback-classic/src/main/java/ch/qos/logback/classic/turbo/LRUMessageCache.java
+++ b/logback-classic/src/main/java/ch/qos/logback/classic/turbo/LRUMessageCache.java
@@ -27,7 +27,7 @@ class LRUMessageCache extends LinkedHashMap<String, Integer> {
LRUMessageCache(int cacheSize) {
super((int) (cacheSize * (4.0f / 3)), 0.75f, true);
if (cacheSize < 1) {
- throw new IllegalArgumentException("Cache size cannnot be smaller than 1");
+ throw new IllegalArgumentException("Cache size cannot be smaller than 1");
}
this.cacheSize = cacheSize;
}
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/turbo/LRUMessageCacheTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/LRUMessageCacheTest.java
new file mode 100644
index 0000000..5e2fed3
--- /dev/null
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/LRUMessageCacheTest.java
@@ -0,0 +1,80 @@
+package ch.qos.logback.classic.turbo;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import junit.framework.Assert;
+
+import org.junit.Test;
+
+public class LRUMessageCacheTest {
+ private static final int INVOCATIONS_PER_TASK = 500 * 1024;
+ private static final int THREADS_NUMBER = 16;
+
+ @Test
+ public void testEldestEntriesRemoval() {
+ final LRUMessageCache cache = new LRUMessageCache(2);
+ Assert.assertEquals(0, cache.getMessageCountAndThenIncrement("0"));
+ Assert.assertEquals(1, cache.getMessageCountAndThenIncrement("0"));
+ Assert.assertEquals(0, cache.getMessageCountAndThenIncrement("1"));
+ Assert.assertEquals(1, cache.getMessageCountAndThenIncrement("1"));
+ // 0 entry should have been removed.
+ Assert.assertEquals(0, cache.getMessageCountAndThenIncrement("2"));
+ // So it is expected a returned value of 0 instead of 2.
+ // 1 entry should have been removed.
+ Assert.assertEquals(0, cache.getMessageCountAndThenIncrement("0"));
+ // So it is expected a returned value of 0 instead of 2.
+ // 2 entry should have been removed.
+ Assert.assertEquals(0, cache.getMessageCountAndThenIncrement("1"));
+ // So it is expected a returned value of 0 instead of 2.
+ Assert.assertEquals(0, cache.getMessageCountAndThenIncrement("2"));
+ }
+
+ @Test
+ public void multiThreadsTest() throws InterruptedException, ExecutionException {
+ final LRUMessageCache cache = new LRUMessageCache(THREADS_NUMBER);
+
+ ArrayList<TestTask> tasks = new ArrayList<TestTask>(THREADS_NUMBER);
+ for (int i = 0; i < THREADS_NUMBER; i++) {
+ tasks.add(new TestTask(cache));
+ }
+
+ ExecutorService execSrv = Executors.newFixedThreadPool(THREADS_NUMBER);
+
+ List<Future<Boolean>> futures = execSrv.invokeAll(tasks);
+ for (Future<Boolean> future : futures) {
+ // Validate that task has successfully finished.
+ future.get();
+ }
+ }
+
+ /**
+ * Each thread is using always the same "Message" key.
+ */
+ private class TestTask implements Callable<Boolean> {
+ private int prevValue = -1;
+ private final LRUMessageCache cache;
+
+ public TestTask(LRUMessageCache cache) {
+ this.cache = cache;
+ }
+
+ public Boolean call() throws Exception {
+ String msg = Thread.currentThread().getName();
+
+ for (int i = 0; i < INVOCATIONS_PER_TASK; i++) {
+ int current = cache.getMessageCountAndThenIncrement(msg);
+ // Ensure that new count is greater than previous count.
+ Assert.assertEquals(prevValue + 1, current);
+ prevValue = current;
+ }
+
+ return true;
+ }
+ }
+}
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/Event.java b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/Event.java
similarity index 90%
rename from logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/Event.java
rename to logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/Event.java
index 6219c35..b9bb777 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/Event.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/Event.java
@@ -1,33 +1,33 @@
-/**
- * Logback: the reliable, generic, fast and flexible logging framework.
- * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
- *
- * This program and the accompanying materials are dual-licensed under
- * either the terms of the Eclipse Public License v1.0 as published by
- * the Eclipse Foundation
- *
- * or (per the licensee's choosing)
- *
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
- */
-package ch.qos.logback.classic.pattern.lru;
-
-public class Event<K> {
-
- final public boolean put;
- final public K k;
-
- public Event(boolean put, K k) {
- this.put = put;
- this.k = k;
- }
-
- public String toString() {
- if(put) {
- return "Event: put, "+k;
- } else {
- return "Event: get, "+k;
- }
- }
-}
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
+ *
+ * This program and the accompanying materials are dual-licensed under
+ * either the terms of the Eclipse Public License v1.0 as published by
+ * the Eclipse Foundation
+ *
+ * or (per the licensee's choosing)
+ *
+ * under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.turbo.lru;
+
+public class Event<K> {
+
+ final public boolean put;
+ final public K k;
+
+ public Event(boolean put, K k) {
+ this.put = put;
+ this.k = k;
+ }
+
+ public String toString() {
+ if(put) {
+ return "Event: put, "+k;
+ } else {
+ return "Event: get, "+k;
+ }
+ }
+}
diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/pattern/LRUCache.java b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/LRUCache.java
similarity index 93%
rename from logback-classic/src/main/java/ch/qos/logback/classic/pattern/LRUCache.java
rename to logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/LRUCache.java
index c2b426a..9bec9d8 100644
--- a/logback-classic/src/main/java/ch/qos/logback/classic/pattern/LRUCache.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/LRUCache.java
@@ -1,51 +1,51 @@
-/**
- * Logback: the reliable, generic, fast and flexible logging framework.
- * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
- *
- * This program and the accompanying materials are dual-licensed under
- * either the terms of the Eclipse Public License v1.0 as published by
- * the Eclipse Foundation
- *
- * or (per the licensee's choosing)
- *
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
- */
-package ch.qos.logback.classic.pattern;
-
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * An lru cache based on Java's LinkedHashMap.
- *
- * @author Ceki Gulcu
- *
- * @param <K>
- * @param <V>
- */
-public class LRUCache<K, V> extends LinkedHashMap<K, V> {
- private static final long serialVersionUID = -6592964689843698200L;
-
- final int cacheSize;
-
- public LRUCache(int cacheSize) {
- super((int) (cacheSize*(4.0f/3)), 0.75f, true);
- if(cacheSize < 1) {
- throw new IllegalArgumentException("Cache size cannnot be smaller than 1");
- }
- this.cacheSize = cacheSize;
- }
-
- protected boolean removeEldestEntry(Map.Entry eldest) {
- return (size() > cacheSize);
- }
-
- List<K> keyList() {
- ArrayList<K> al = new ArrayList<K>();
- al.addAll(keySet());
- return al;
- }
-}
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
+ *
+ * This program and the accompanying materials are dual-licensed under
+ * either the terms of the Eclipse Public License v1.0 as published by
+ * the Eclipse Foundation
+ *
+ * or (per the licensee's choosing)
+ *
+ * under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.turbo.lru;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * An lru cache based on Java's LinkedHashMap.
+ *
+ * @author Ceki Gulcu
+ *
+ * @param <K>
+ * @param <V>
+ */
+public class LRUCache<K, V> extends LinkedHashMap<K, V> {
+ private static final long serialVersionUID = -6592964689843698200L;
+
+ final int cacheSize;
+
+ public LRUCache(int cacheSize) {
+ super((int) (cacheSize*(4.0f/3)), 0.75f, true);
+ if(cacheSize < 1) {
+ throw new IllegalArgumentException("Cache size cannnot be smaller than 1");
+ }
+ this.cacheSize = cacheSize;
+ }
+
+ protected boolean removeEldestEntry(Map.Entry eldest) {
+ return (size() > cacheSize);
+ }
+
+ List<K> keyList() {
+ ArrayList<K> al = new ArrayList<K>();
+ al.addAll(keySet());
+ return al;
+ }
+}
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/LRUCacheTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/LRUCacheTest.java
similarity index 95%
rename from logback-classic/src/test/java/ch/qos/logback/classic/pattern/LRUCacheTest.java
rename to logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/LRUCacheTest.java
index c9628b6..96e04c0 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/LRUCacheTest.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/LRUCacheTest.java
@@ -1,125 +1,122 @@
-/**
- * Logback: the reliable, generic, fast and flexible logging framework.
- * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
- *
- * This program and the accompanying materials are dual-licensed under
- * either the terms of the Eclipse Public License v1.0 as published by
- * the Eclipse Foundation
- *
- * or (per the licensee's choosing)
- *
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
- */
-package ch.qos.logback.classic.pattern;
-
-import static org.junit.Assert.assertEquals;
-
-import java.util.LinkedList;
-import java.util.List;
-
-import org.junit.Ignore;
-import org.junit.Test;
-
-import ch.qos.logback.classic.pattern.lru.Event;
-import ch.qos.logback.classic.pattern.lru.T_LRUCache;
-
- at Ignore
-public class LRUCacheTest {
-
- @Test
- public void smoke() {
-
- LRUCache<String, String> cache = new LRUCache<String, String>(2);
- cache.put("a", "a");
- cache.put("b", "b");
- cache.put("c", "c");
- List<String> witness = new LinkedList<String>();
- witness.add("b");
- witness.add("c");
- assertEquals(witness, cache.keyList());
- }
-
- @Test
- public void typicalScenarioTest() {
- int simulationLen = 1000 * 10;
- int cacheSize = 100;
- int worldSize = 1000;
- doScenario(simulationLen, cacheSize, worldSize);
- }
-
- @Test
- public void scenarioCoverageTest() {
- int simulationLen = 1000 * 10;
-
- int[] cacheSizes = new int[] { 1, 10, 100};
- // tests with large worldSizes are slow because with a large
- // world size the probability of a cache miss is high.
- int[] worldSizes = new int[] { 1, 10, 100 };
-
- for (int i = 0; i < cacheSizes.length; i++) {
- for (int j = 0; j < worldSizes.length; j++) {
- doScenario(simulationLen, cacheSizes[i], worldSizes[j]);
- }
- }
- }
-
- void doScenario(int simulationLen, int cacheSize, int worldSize) {
- int get2PutRatio = 10;
- Simulator simulator = new Simulator(worldSize, get2PutRatio, false);
- List<Event> scenario = simulator.generateScenario(simulationLen);
- LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
- T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
- long start = System.nanoTime();
- simulator.simulate(scenario, lruCache, tlruCache);
- //assertEquals(tlruCache.keyList(), lruCache.keyList());
- long end = System.nanoTime();
- System.out.println("cacheSize=" + cacheSize + ", worldSize=" + worldSize
- + ", elapsed time=" + ((end - start) / (1000 * 1000)) + " in millis");
- }
-
-
-
- @Test
- @Ignore // slow test that is known to pass
- public void multiThreadedScenario() throws InterruptedException {
- int cacheSize = 100;
- int worldSize = cacheSize*2;
- LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
- T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
- SimulatorRunnable[] simulatorArray = new SimulatorRunnable[5];
- for(int i = 0; i < simulatorArray.length; i++) {
- simulatorArray[i] = new SimulatorRunnable(lruCache, tlruCache, worldSize);
- }
- for(int i = 0; i < simulatorArray.length; i++) {
- simulatorArray[i].start();
- }
- for(int i = 0; i < simulatorArray.length; i++) {
- simulatorArray[i].join();
- }
- assertEquals(tlruCache.keyList(), lruCache.keyList());
- }
-
- private class SimulatorRunnable extends Thread {
-
- LRUCache<String, String> lruCache;
- T_LRUCache<String> tlruCache;
- int worldSize;
-
- SimulatorRunnable(LRUCache<String, String> lruCache, T_LRUCache<String> tlruCache, int worldSize) {
- this.lruCache = lruCache;
- this.tlruCache = tlruCache;
- this.worldSize = worldSize;
- }
-
- public void run() {
- int get2PutRatio = 10;
- int simulationLen = 1000*50;
- Simulator simulator = new Simulator(worldSize, get2PutRatio, true);
- List<Event> scenario = simulator.generateScenario(simulationLen);
- simulator.simulate(scenario, lruCache, tlruCache);
- System.out.println("done");
- }
- }
-
-}
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
+ *
+ * This program and the accompanying materials are dual-licensed under
+ * either the terms of the Eclipse Public License v1.0 as published by
+ * the Eclipse Foundation
+ *
+ * or (per the licensee's choosing)
+ *
+ * under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.turbo.lru;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+ at Ignore
+public class LRUCacheTest {
+
+ @Test
+ public void smoke() {
+
+ LRUCache<String, String> cache = new LRUCache<String, String>(2);
+ cache.put("a", "a");
+ cache.put("b", "b");
+ cache.put("c", "c");
+ List<String> witness = new LinkedList<String>();
+ witness.add("b");
+ witness.add("c");
+ assertEquals(witness, cache.keyList());
+ }
+
+ @Test
+ public void typicalScenarioTest() {
+ int simulationLen = 1000 * 10;
+ int cacheSize = 100;
+ int worldSize = 1000;
+ doScenario(simulationLen, cacheSize, worldSize);
+ }
+
+ @Test
+ public void scenarioCoverageTest() {
+ int simulationLen = 1000 * 10;
+
+ int[] cacheSizes = new int[] { 1, 10, 100};
+ // tests with large worldSizes are slow because with a large
+ // world size the probability of a cache miss is high.
+ int[] worldSizes = new int[] { 1, 10, 100 };
+
+ for (int i = 0; i < cacheSizes.length; i++) {
+ for (int j = 0; j < worldSizes.length; j++) {
+ doScenario(simulationLen, cacheSizes[i], worldSizes[j]);
+ }
+ }
+ }
+
+ void doScenario(int simulationLen, int cacheSize, int worldSize) {
+ int get2PutRatio = 10;
+ Simulator simulator = new Simulator(worldSize, get2PutRatio, false);
+ List<Event> scenario = simulator.generateScenario(simulationLen);
+ LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
+ T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
+ long start = System.nanoTime();
+ simulator.simulate(scenario, lruCache, tlruCache);
+ //assertEquals(tlruCache.keyList(), lruCache.keyList());
+ long end = System.nanoTime();
+ System.out.println("cacheSize=" + cacheSize + ", worldSize=" + worldSize
+ + ", elapsed time=" + ((end - start) / (1000 * 1000)) + " in millis");
+ }
+
+
+
+ @Test
+ @Ignore // slow test that is known to pass
+ public void multiThreadedScenario() throws InterruptedException {
+ int cacheSize = 100;
+ int worldSize = cacheSize*2;
+ LRUCache<String, String> lruCache = new LRUCache<String, String>(cacheSize);
+ T_LRUCache<String> tlruCache = new T_LRUCache<String>(cacheSize);
+ SimulatorRunnable[] simulatorArray = new SimulatorRunnable[5];
+ for(int i = 0; i < simulatorArray.length; i++) {
+ simulatorArray[i] = new SimulatorRunnable(lruCache, tlruCache, worldSize);
+ }
+ for(int i = 0; i < simulatorArray.length; i++) {
+ simulatorArray[i].start();
+ }
+ for(int i = 0; i < simulatorArray.length; i++) {
+ simulatorArray[i].join();
+ }
+ assertEquals(tlruCache.keyList(), lruCache.keyList());
+ }
+
+ private class SimulatorRunnable extends Thread {
+
+ LRUCache<String, String> lruCache;
+ T_LRUCache<String> tlruCache;
+ int worldSize;
+
+ SimulatorRunnable(LRUCache<String, String> lruCache, T_LRUCache<String> tlruCache, int worldSize) {
+ this.lruCache = lruCache;
+ this.tlruCache = tlruCache;
+ this.worldSize = worldSize;
+ }
+
+ public void run() {
+ int get2PutRatio = 10;
+ int simulationLen = 1000*50;
+ Simulator simulator = new Simulator(worldSize, get2PutRatio, true);
+ List<Event> scenario = simulator.generateScenario(simulationLen);
+ simulator.simulate(scenario, lruCache, tlruCache);
+ System.out.println("done");
+ }
+ }
+
+}
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/Simulator.java b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/Simulator.java
similarity index 94%
rename from logback-classic/src/test/java/ch/qos/logback/classic/pattern/Simulator.java
rename to logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/Simulator.java
index d07fdc4..a1eea56 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/Simulator.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/Simulator.java
@@ -1,92 +1,89 @@
-/**
- * Logback: the reliable, generic, fast and flexible logging framework.
- * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
- *
- * This program and the accompanying materials are dual-licensed under
- * either the terms of the Eclipse Public License v1.0 as published by
- * the Eclipse Foundation
- *
- * or (per the licensee's choosing)
- *
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
- */
-package ch.qos.logback.classic.pattern;
-
-import static org.junit.Assert.assertEquals;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Random;
-
-import ch.qos.logback.classic.pattern.lru.Event;
-import ch.qos.logback.classic.pattern.lru.T_LRUCache;
-
-public class Simulator {
-
- Random random;
-
- int worldSize;
- int get2PutRatio;
- boolean multiThreaded;
-
- public Simulator(int worldSize, int get2PutRatio, boolean multiThreaded) {
- this.worldSize = worldSize;
- this.get2PutRatio = get2PutRatio;
- long seed = System.nanoTime();
- // System.out.println("seed is "+seed);
- random = new Random(seed);
- this.multiThreaded = multiThreaded;
- }
-
- public List<Event> generateScenario(int len) {
- List<Event> scenario = new ArrayList<Event>();
-
- for (int i = 0; i < len; i++) {
-
- int r = random.nextInt(get2PutRatio);
- boolean put = false;
- if (r == 0) {
- put = true;
- }
- r = random.nextInt(worldSize);
- Event<String> e = new Event<String>(put, String.valueOf(r));
- scenario.add(e);
- }
- return scenario;
- }
-
- public void simulate(List<Event> scenario, LRUCache<String, String> lruCache,
- T_LRUCache<String> tlruCache) {
- for (Event<String> e : scenario) {
- if (e.put) {
- lruCache.put(e.k, e.k);
- tlruCache.put(e.k);
- } else {
- String r0 = lruCache.get(e.k);
- String r1 = tlruCache.get(e.k);
- if (!multiThreaded) {
- // if the simulation is used in a multi-threaded
- // context, then the state of lruCache may be different than
- // that of tlruCache. In single threaded mode, they should
- // return the same values all the time
- if (r0 != null) {
- assertEquals(r0, e.k);
- }
- assertEquals(r0, r1);
- }
- }
- }
- }
-
- // void compareAndDumpIfDifferent(LRUCache<String, String> lruCache,
- // T_LRUCache<String> tlruCache) {
- // lruCache.dump();
- // tlruCache.dump();
- // if(!lruCache.keyList().equals(tlruCache.ketList())) {
- // lruCache.dump();
- // tlruCache.dump();
- // throw new AssertionFailedError("s");
- // }
- // }
-}
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
+ *
+ * This program and the accompanying materials are dual-licensed under
+ * either the terms of the Eclipse Public License v1.0 as published by
+ * the Eclipse Foundation
+ *
+ * or (per the licensee's choosing)
+ *
+ * under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.turbo.lru;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+public class Simulator {
+
+ Random random;
+
+ int worldSize;
+ int get2PutRatio;
+ boolean multiThreaded;
+
+ public Simulator(int worldSize, int get2PutRatio, boolean multiThreaded) {
+ this.worldSize = worldSize;
+ this.get2PutRatio = get2PutRatio;
+ long seed = System.nanoTime();
+ // System.out.println("seed is "+seed);
+ random = new Random(seed);
+ this.multiThreaded = multiThreaded;
+ }
+
+ public List<Event> generateScenario(int len) {
+ List<Event> scenario = new ArrayList<Event>();
+
+ for (int i = 0; i < len; i++) {
+
+ int r = random.nextInt(get2PutRatio);
+ boolean put = false;
+ if (r == 0) {
+ put = true;
+ }
+ r = random.nextInt(worldSize);
+ Event<String> e = new Event<String>(put, String.valueOf(r));
+ scenario.add(e);
+ }
+ return scenario;
+ }
+
+ public void simulate(List<Event> scenario, LRUCache<String, String> lruCache,
+ T_LRUCache<String> tlruCache) {
+ for (Event<String> e : scenario) {
+ if (e.put) {
+ lruCache.put(e.k, e.k);
+ tlruCache.put(e.k);
+ } else {
+ String r0 = lruCache.get(e.k);
+ String r1 = tlruCache.get(e.k);
+ if (!multiThreaded) {
+ // if the simulation is used in a multi-threaded
+ // context, then the state of lruCache may be different than
+ // that of tlruCache. In single threaded mode, they should
+ // return the same values all the time
+ if (r0 != null) {
+ assertEquals(r0, e.k);
+ }
+ assertEquals(r0, r1);
+ }
+ }
+ }
+ }
+
+ // void compareAndDumpIfDifferent(LRUCache<String, String> lruCache,
+ // T_LRUCache<String> tlruCache) {
+ // lruCache.dump();
+ // tlruCache.dump();
+ // if(!lruCache.keyList().equals(tlruCache.ketList())) {
+ // lruCache.dump();
+ // tlruCache.dump();
+ // throw new AssertionFailedError("s");
+ // }
+ // }
+}
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/T_Entry.java b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/T_Entry.java
similarity index 92%
rename from logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/T_Entry.java
rename to logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/T_Entry.java
index 59cff0a..980ab2e 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/T_Entry.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/T_Entry.java
@@ -1,45 +1,45 @@
-/**
- * Logback: the reliable, generic, fast and flexible logging framework.
- * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
- *
- * This program and the accompanying materials are dual-licensed under
- * either the terms of the Eclipse Public License v1.0 as published by
- * the Eclipse Foundation
- *
- * or (per the licensee's choosing)
- *
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
- */
-package ch.qos.logback.classic.pattern.lru;
-
-public class T_Entry<K> implements Comparable {
-
- K k;
- long sequenceNumber;
-
- T_Entry(K k, long sn) {
- this.k = k;
- this.sequenceNumber = sn;
- }
-
- public int compareTo(Object o) {
- if(!(o instanceof T_Entry)) {
- throw new IllegalArgumentException("arguments must be of type "+T_Entry.class);
- }
-
- T_Entry other = (T_Entry) o;
- if(sequenceNumber > other.sequenceNumber) {
- return 1;
- }
- if(sequenceNumber == other.sequenceNumber) {
- return 0;
- }
- return -1;
- }
- @Override
- public String toString() {
- return "("+k+","+sequenceNumber+")";
- //return "("+k+")";
- }
-}
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
+ *
+ * This program and the accompanying materials are dual-licensed under
+ * either the terms of the Eclipse Public License v1.0 as published by
+ * the Eclipse Foundation
+ *
+ * or (per the licensee's choosing)
+ *
+ * under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.turbo.lru;
+
+public class T_Entry<K> implements Comparable {
+
+ K k;
+ long sequenceNumber;
+
+ T_Entry(K k, long sn) {
+ this.k = k;
+ this.sequenceNumber = sn;
+ }
+
+ public int compareTo(Object o) {
+ if(!(o instanceof T_Entry)) {
+ throw new IllegalArgumentException("arguments must be of type "+T_Entry.class);
+ }
+
+ T_Entry other = (T_Entry) o;
+ if(sequenceNumber > other.sequenceNumber) {
+ return 1;
+ }
+ if(sequenceNumber == other.sequenceNumber) {
+ return 0;
+ }
+ return -1;
+ }
+ @Override
+ public String toString() {
+ return "("+k+","+sequenceNumber+")";
+ //return "("+k+")";
+ }
+}
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/T_LRUCache.java b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/T_LRUCache.java
similarity index 94%
rename from logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/T_LRUCache.java
rename to logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/T_LRUCache.java
index d132ee1..ebb7150 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/lru/T_LRUCache.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/turbo/lru/T_LRUCache.java
@@ -1,93 +1,93 @@
-/**
- * Logback: the reliable, generic, fast and flexible logging framework.
- * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
- *
- * This program and the accompanying materials are dual-licensed under
- * either the terms of the Eclipse Public License v1.0 as published by
- * the Eclipse Foundation
- *
- * or (per the licensee's choosing)
- *
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
- */
-package ch.qos.logback.classic.pattern.lru;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-
-/**
- * This is an alternative (slower) implementation of LRUCache for testing
- * purposes.
- *
- * @author Ceki Gulcu
- */
-public class T_LRUCache<K> {
-
- int sequenceNumber;
- final int cacheSize;
- List<T_Entry<K>> cacheList = new LinkedList<T_Entry<K>>();
-
- public T_LRUCache(int size) {
- this.cacheSize = size;
- }
-
- @SuppressWarnings("unchecked")
- synchronized public void put(K k) {
- sequenceNumber++;
- T_Entry<K> te = getEntry(k);
- if (te != null) {
- te.sequenceNumber = sequenceNumber;
- } else {
- te = new T_Entry<K>(k, sequenceNumber);
- cacheList.add(te);
- }
- Collections.sort(cacheList);
- while(cacheList.size() > cacheSize) {
- cacheList.remove(0);
- }
- }
-
- @SuppressWarnings("unchecked")
- synchronized public K get(K k) {
- T_Entry<K> te = getEntry(k);
- if (te == null) {
- return null;
- } else {
- te.sequenceNumber = ++sequenceNumber;
- Collections.sort(cacheList);
- return te.k;
- }
- }
-
- synchronized public List<K> keyList() {
- List<K> keyList = new ArrayList<K>();
- for (T_Entry<K> e : cacheList) {
- keyList.add(e.k);
- }
- return keyList;
- }
-
- private T_Entry<K> getEntry(K k) {
- for (int i = 0; i < cacheList.size(); i++) {
- T_Entry<K> te = cacheList.get(i);
- if (te.k.equals(k)) {
- return te;
- }
- }
- return null;
- }
-
- public void dump() {
- System.out.print("T:");
- for (T_Entry<K> te : cacheList) {
- //System.out.print(te.toString()+"->");
- System.out.print(te.k+", ");
- }
- System.out.println();
- }
-
-}
-
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2009, QOS.ch. All rights reserved.
+ *
+ * This program and the accompanying materials are dual-licensed under
+ * either the terms of the Eclipse Public License v1.0 as published by
+ * the Eclipse Foundation
+ *
+ * or (per the licensee's choosing)
+ *
+ * under the terms of the GNU Lesser General Public License version 2.1
+ * as published by the Free Software Foundation.
+ */
+package ch.qos.logback.classic.turbo.lru;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * This is an alternative (slower) implementation of LRUCache for testing
+ * purposes.
+ *
+ * @author Ceki Gulcu
+ */
+public class T_LRUCache<K> {
+
+ int sequenceNumber;
+ final int cacheSize;
+ List<T_Entry<K>> cacheList = new LinkedList<T_Entry<K>>();
+
+ public T_LRUCache(int size) {
+ this.cacheSize = size;
+ }
+
+ @SuppressWarnings("unchecked")
+ synchronized public void put(K k) {
+ sequenceNumber++;
+ T_Entry<K> te = getEntry(k);
+ if (te != null) {
+ te.sequenceNumber = sequenceNumber;
+ } else {
+ te = new T_Entry<K>(k, sequenceNumber);
+ cacheList.add(te);
+ }
+ Collections.sort(cacheList);
+ while(cacheList.size() > cacheSize) {
+ cacheList.remove(0);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ synchronized public K get(K k) {
+ T_Entry<K> te = getEntry(k);
+ if (te == null) {
+ return null;
+ } else {
+ te.sequenceNumber = ++sequenceNumber;
+ Collections.sort(cacheList);
+ return te.k;
+ }
+ }
+
+ synchronized public List<K> keyList() {
+ List<K> keyList = new ArrayList<K>();
+ for (T_Entry<K> e : cacheList) {
+ keyList.add(e.k);
+ }
+ return keyList;
+ }
+
+ private T_Entry<K> getEntry(K k) {
+ for (int i = 0; i < cacheList.size(); i++) {
+ T_Entry<K> te = cacheList.get(i);
+ if (te.k.equals(k)) {
+ return te;
+ }
+ }
+ return null;
+ }
+
+ public void dump() {
+ System.out.print("T:");
+ for (T_Entry<K> te : cacheList) {
+ //System.out.print(te.toString()+"->");
+ System.out.print(te.k+", ");
+ }
+ System.out.println();
+ }
+
+}
+
-----------------------------------------------------------------------
Summary of changes:
.../qos/logback/classic/turbo/LRUMessageCache.java | 2 +-
.../logback/classic/turbo/LRUMessageCacheTest.java | 80 +++++++
.../classic/{pattern => turbo}/lru/Event.java | 66 +++---
.../qos/logback/classic/turbo/lru}/LRUCache.java | 102 ++++----
.../{pattern => turbo/lru}/LRUCacheTest.java | 247 ++++++++++----------
.../classic/{pattern => turbo/lru}/Simulator.java | 181 +++++++--------
.../classic/{pattern => turbo}/lru/T_Entry.java | 90 ++++----
.../classic/{pattern => turbo}/lru/T_LRUCache.java | 186 ++++++++--------
8 files changed, 514 insertions(+), 440 deletions(-)
create mode 100644 logback-classic/src/test/java/ch/qos/logback/classic/turbo/LRUMessageCacheTest.java
rename logback-classic/src/test/java/ch/qos/logback/classic/{pattern => turbo}/lru/Event.java (90%)
rename logback-classic/src/{main/java/ch/qos/logback/classic/pattern => test/java/ch/qos/logback/classic/turbo/lru}/LRUCache.java (93%)
rename logback-classic/src/test/java/ch/qos/logback/classic/{pattern => turbo/lru}/LRUCacheTest.java (95%)
rename logback-classic/src/test/java/ch/qos/logback/classic/{pattern => turbo/lru}/Simulator.java (94%)
rename logback-classic/src/test/java/ch/qos/logback/classic/{pattern => turbo}/lru/T_Entry.java (92%)
rename logback-classic/src/test/java/ch/qos/logback/classic/{pattern => turbo}/lru/T_LRUCache.java (94%)
hooks/post-receive
--
Logback: the generic, reliable, fast and flexible logging framework.
More information about the logback-dev
mailing list