Skip to content

Commit

Permalink
Client module; replaced System.currentTimeMillis with Clock.currentT…
Browse files Browse the repository at this point in the history
…imeMillis
  • Loading branch information
mdogan committed Apr 11, 2012
1 parent 82fcc4c commit 2cc2344
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.nio.SocketInterceptor;
import com.hazelcast.util.Clock;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -67,7 +68,7 @@ void scheduleHeartbeatTimerTask() {
heartbeatTimer.schedule(new TimerTask() {
@Override
public void run() {
long diff = client.getInRunnable().lastReceived - System.currentTimeMillis();
long diff = client.getInRunnable().lastReceived - Clock.currentTimeMillis();
try {
if (diff >= TIMEOUT / 5 && diff < TIMEOUT) {
logger.log(Level.FINEST,
Expand Down Expand Up @@ -118,7 +119,7 @@ private Connection lookForLiveConnection(final int attemptsLimit,
boolean restored = false;
int attempt = 0;
while (currentConnection == null && running && !Thread.interrupted()) {
final long next = System.currentTimeMillis() + reconnectionTimeout;
final long next = Clock.currentTimeMillis() + reconnectionTimeout;
synchronized (this) {
if (currentConnection == null) {
final Connection connection = searchForAvailableConnection();
Expand Down Expand Up @@ -149,7 +150,7 @@ private Connection lookForLiveConnection(final int attemptsLimit,
break;
}
attempt++;
final long t = next - System.currentTimeMillis();
final long t = next - Clock.currentTimeMillis();
logger.log(Level.INFO, format("Unable to get alive cluster connection," +
" try in {0} ms later, attempt {1} of {2}.",
Math.max(0, t), attempt, attemptsLimit));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hazelcast.client;

import com.hazelcast.impl.ClusterOperation;
import com.hazelcast.util.Clock;

import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -59,7 +60,7 @@ protected void customRun() throws InterruptedException {
} else {
packet = reader.readPacket(connection);
// logger.log(Level.FINEST, "Reading " + packet.getOperation() + " Call id: " + packet.getCallId());
this.lastReceived = System.currentTimeMillis();
this.lastReceived = Clock.currentTimeMillis();
Call call = callMap.remove(packet.getCallId());
if (call != null) {
call.received = System.nanoTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
import com.hazelcast.core.LifecycleService;
import com.hazelcast.logging.ILogger;
import com.hazelcast.logging.Logger;
import com.hazelcast.util.Clock;

import java.util.concurrent.*;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;

Expand Down Expand Up @@ -110,11 +114,11 @@ public void shutdown() {
Callable<Boolean> callable = new Callable<Boolean>() {
public Boolean call() {
synchronized (lifecycleLock) {
long begin = System.currentTimeMillis();
long begin = Clock.currentTimeMillis();
fireLifecycleEvent(SHUTTING_DOWN);
hazelcastClient.doShutdown();
running.set(false);
long time = System.currentTimeMillis() - begin;
long time = Clock.currentTimeMillis() - begin;
logger.log(Level.FINE, "HazelcastClient shutdown completed in " + time + " ms.");
fireLifecycleEvent(SHUTDOWN);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.hazelcast.query.Predicate;
import com.hazelcast.query.PredicateBuilder;
import com.hazelcast.query.SqlPredicate;
import com.hazelcast.util.Clock;
import org.junit.AfterClass;
import org.junit.Test;

Expand Down Expand Up @@ -234,9 +235,9 @@ public void addIndex() {
}
EntryObject e = new PredicateBuilder().getEntryObject();
Predicate predicate = e.get("age").equal(23);
long begin = System.currentTimeMillis();
long begin = Clock.currentTimeMillis();
Set<Entry<Object, Object>> set = map.entrySet(predicate);
long timeWithoutIndex = System.currentTimeMillis() - begin;
long timeWithoutIndex = Clock.currentTimeMillis() - begin;
assertEquals(1, set.size());
assertEquals(size, map.size());
map.destroy();
Expand All @@ -245,9 +246,9 @@ public void addIndex() {
for (int i = 0; i < size; i++) {
map.put(String.valueOf(i), new Employee("name" + i, i, true, 0));
}
begin = System.currentTimeMillis();
begin = Clock.currentTimeMillis();
set = map.entrySet(predicate);
long timeWithIndex = System.currentTimeMillis() - begin;
long timeWithIndex = Clock.currentTimeMillis() - begin;
assertEquals(1, set.size());
assertEquals(size, map.size());
assertTrue(timeWithoutIndex > 2 * timeWithIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.hazelcast.core.ITopic;
import com.hazelcast.core.Message;
import com.hazelcast.core.MessageListener;
import com.hazelcast.util.Clock;
import org.junit.AfterClass;
import org.junit.Test;

Expand Down Expand Up @@ -135,7 +136,7 @@ public void run() {
@Test
public void testPerformance() throws InterruptedException {
HazelcastClient hClient = getHazelcastClient();
long begin = System.currentTimeMillis();
long begin = Clock.currentTimeMillis();
int count = 10000;
final ITopic topic = hClient.getTopic("perf");
ExecutorService ex = Executors.newFixedThreadPool(10);
Expand All @@ -149,7 +150,7 @@ public void run() {
});
}
assertTrue(l.await(20, TimeUnit.SECONDS));
long time = System.currentTimeMillis() - begin;
long time = Clock.currentTimeMillis() - begin;
System.out.println("per second: " + count * 1000 / time);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.hazelcast.impl.GroupProperties;
import com.hazelcast.impl.SleepCallable;
import com.hazelcast.monitor.DistributedMapStatsCallable;
import com.hazelcast.util.Clock;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -918,7 +919,7 @@ public void performanceWithLotsOfExecutingTasks() throws InterruptedException, E
HazelcastClient client = TestUtility.newHazelcastClient(h2);
ExecutorService executor = client.getExecutorService("esname");
Map<Integer, FutureTask> taskMap = new ConcurrentHashMap<Integer, FutureTask>();
long start = System.currentTimeMillis();
long start = Clock.currentTimeMillis();
CountDownLatch latch = new CountDownLatch(100);
for (int i = 0; i < 1000; i++) {
FutureTask<Integer> task = new DistributedTask<Integer>(
Expand Down Expand Up @@ -1114,9 +1115,9 @@ boolean waitFor(LifecycleEvent.LifecycleState state, int seconds) {
while (remainingMillis >= 0) {
LifecycleEvent.LifecycleState received = null;
try {
long now = System.currentTimeMillis();
long now = Clock.currentTimeMillis();
received = eventQueue.poll(remainingMillis, TimeUnit.MILLISECONDS);
remainingMillis -= (System.currentTimeMillis() - now);
remainingMillis -= (Clock.currentTimeMillis() - now);
} catch (InterruptedException e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.impl.ClusterOperation;
import com.hazelcast.util.Clock;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
Expand Down Expand Up @@ -48,17 +49,17 @@ public void putAndget100000RecordsWith1ClusterMember() {
}

private void putAndGet(Map<String, String> map, int counter) {
long beginTime = System.currentTimeMillis();
long beginTime = Clock.currentTimeMillis();
for (int i = 1; i <= counter; i++) {
if (i % (counter / 10) == 0) {
System.out.println(i + ": " + (System.currentTimeMillis() - beginTime) + " ms");
System.out.println(i + ": " + (Clock.currentTimeMillis() - beginTime) + " ms");
}
map.put("key_" + i, String.valueOf(i));
}
beginTime = System.currentTimeMillis();
beginTime = Clock.currentTimeMillis();
for (int i = 1; i <= counter; i++) {
if (i % (counter / 10) == 0) {
System.out.println(i + ": " + (System.currentTimeMillis() - beginTime) + " ms");
System.out.println(i + ": " + (Clock.currentTimeMillis() - beginTime) + " ms");
}
assertEquals(String.valueOf(i), map.get("key_" + i));
}
Expand All @@ -73,7 +74,7 @@ public void putAndget100000RecordsWith1ClusterMemberFrom10Threads() throws Inter
final AtomicInteger getCounter = new AtomicInteger(count);
final AtomicInteger putCounter = new AtomicInteger(count);
ExecutorService executorService = Executors.newFixedThreadPool(threads);
final long beginTime = System.currentTimeMillis();
final long beginTime = Clock.currentTimeMillis();
final CountDownLatch latch = new CountDownLatch(threads);
for (int i = 0; i < threads; i++) {
executorService.execute(new Runnable() {
Expand All @@ -91,7 +92,7 @@ public void run() {
}
latch.await();
System.out.println(threads + " Threads made in total " + count +
" puts and gets in " + (System.currentTimeMillis() - beginTime) + " ms");
" puts and gets in " + (Clock.currentTimeMillis() - beginTime) + " ms");
}

@Test
Expand Down Expand Up @@ -175,7 +176,7 @@ public void run() {
final OutRunnable outRunnable = new OutRunnable(client, new HashMap<Long, Call>(), packetWriter);
new Thread(outRunnable).start();
final AtomicLong callCounter = new AtomicLong();
final long start = System.currentTimeMillis();
final long start = Clock.currentTimeMillis();
ExecutorService executorService = Executors.newFixedThreadPool(20);
final BlockingQueue<Object> queue = new LinkedBlockingQueue<Object>();
final Object object = new Object();
Expand Down Expand Up @@ -208,7 +209,7 @@ public void run() {
}
// numberOfTasks = numberOfTasks + numberOfTasks/10;
Thread.sleep(1 * 1000);
System.out.println("Operations per millisecond : " + callCounter.get() / (System.currentTimeMillis() - start));
System.out.println("Operations per millisecond : " + callCounter.get() / (Clock.currentTimeMillis() - start));
System.out.println("out runnable Queue size: " + outRunnable.getQueueSize());
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public void run() {
int key = (int) (Math.random() * ENTRY_COUNT);
int operation = ((int) (Math.random() * 100));
if (operation < GET_PERCENTAGE) {
// long start = System.currentTimeMillis();
// long start = Clock.currentTimeMillis();
map.get(String.valueOf(key));
// System.out.println("Get takes " + (System.currentTimeMillis() - start) + " ms" );
// System.out.println("Get takes " + (Clock.currentTimeMillis() - start) + " ms" );
stats.gets.incrementAndGet();
} else if (operation < GET_PERCENTAGE + PUT_PERCENTAGE) {
map.put(String.valueOf(key), new byte[VALUE_SIZE]);
Expand Down

0 comments on commit 2cc2344

Please sign in to comment.