INT-1188, moved to the sandbox for further investigation.

This commit is contained in:
Oleg Zhurakousky
2010-06-28 14:17:34 +00:00
parent eb3793fbbc
commit 234c44f335
2 changed files with 0 additions and 215 deletions

View File

@@ -1,109 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.core;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Will generate time-based UUID (version 1 UUID).
* This will allow Message ID to be unique but also contain an
* embedded timestamp which could be retrieved via UUID.timestamp()
*
* @author Oleg Zhurakousky
* @since 2.0
*/
class TimeBasedUUIDGenerator {
public static final Object lock = new Object();
private static boolean canNotDetermineMac = true;
private static long lastTime;
private static long processId = Long.valueOf(ManagementFactory.getRuntimeMXBean().getName().hashCode());
private static long pidMac;
private static final Log logger = LogFactory.getLog(TimeBasedUUIDGenerator.class);
private static long macAddressAsLong = 0;
static {
try {
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress(address);
if (ni != null) {
byte[] mac = ni.getHardwareAddress();
//Converts array of unsigned bytes to an long
if (mac != null) {
for (int i = 0; i < mac.length; i++) {
macAddressAsLong |= mac[i] & 0xFF;
macAddressAsLong <<= 8;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
pidMac = macAddressAsLong;
if (pidMac == 0){
pidMac = (processId << 32) | System.currentTimeMillis();
} else {
canNotDetermineMac = false;
pidMac = (processId << 32) | macAddressAsLong;
}
}
/**
* Will generate unique time based UUID where the next UUID is
* always greater then the previous.
*
* @return
*/
public final static UUID generateId() {
return generateIdFromTimestamp(System.currentTimeMillis());
}
/**
*
* @param currentTimeMillis
* @return
*/
public final static UUID generateIdFromTimestamp(long currentTimeMillis){
long time;
synchronized (lock) {
if (currentTimeMillis > lastTime) {
lastTime = currentTimeMillis;
} else {
currentTimeMillis = ++lastTime;
}
}
time = currentTimeMillis;
// low Time
time = currentTimeMillis << 32;
// mid Time
time |= (currentTimeMillis & 0xFFFF00000000L) >> 16;
// hi Time
time |= 0x1000 | ((currentTimeMillis >> 48) & 0x0FFF); // version 1
if (canNotDetermineMac){
logger.warn("UUID generation process was not able to determine your MAC address. There is a slight chance for this UUID not to be globally unique");
}
return new UUID(time, pidMac);
}
}

View File

@@ -1,106 +0,0 @@
/*
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.core;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.util.StopWatch;
/**
* @author Oleg Zhurakousky
*
*/
public class TimeBasedUUIDGeneratorTests {
@Test
public void testGreaterThen(){
UUID id = TimeBasedUUIDGenerator.generateId();
for (int i = 0; i < 1000; i++) {
UUID newId = TimeBasedUUIDGenerator.generateId();
//System.out.println(newId);
// tests, that newly created UUID is always greater then the previous one.
Assert.assertTrue(newId.compareTo(id) == 1);
id = newId;
}
}
@Test
public void testUniqueness(){
long timestamp = System.currentTimeMillis();
UUID id = TimeBasedUUIDGenerator.generateIdFromTimestamp(timestamp);
for (int i = 0; i < 1000; i++) {
UUID newId = TimeBasedUUIDGenerator.generateIdFromTimestamp(timestamp);
// tests, that newly created UUID is always greater then the previous one.
Assert.assertTrue(newId.compareTo(id) == 1);
id = newId;
}
}
@Test
public void performanceTestSynch(){
StopWatch stopWatch = new StopWatch();
stopWatch.start();
for (int i = 0; i < 10000; i++) {
TimeBasedUUIDGenerator.generateId();
}
stopWatch.stop();
System.out.println("Generated 10000 UUID (sync) via TimeBasedUUIDGenerator.generateId(): in " + stopWatch.getTotalTimeSeconds() + " seconds");
stopWatch = new StopWatch();
stopWatch.start();
for (int i = 0; i < 10000; i++) {
UUID.randomUUID();
}
stopWatch.stop();
System.out.println("Generated 10000 UUID (sync) via UUID.randomUUID(): in " + stopWatch.getTotalTimeSeconds() + " seconds");
}
@Test
public void performanceTestAsynch() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(1);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
for (int i = 0; i < 10000; i++) {
executor.execute(new Runnable() {
public void run() {
TimeBasedUUIDGenerator.generateId();
}
});
}
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
stopWatch.stop();
System.out.println("Generated 10000 UUID (async) via TimeBasedUUIDGenerator.generateId(): in " + stopWatch.getTotalTimeSeconds() + " seconds");
executor = Executors.newFixedThreadPool(1);
stopWatch = new StopWatch();
stopWatch.start();
for (int i = 0; i < 10000; i++) {
executor.execute(new Runnable() {
public void run() {
UUID.randomUUID();
}
});
}
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
stopWatch.stop();
System.out.println("Generated 10000 UUID (async) via UUID.randomUUID(): in " + stopWatch.getTotalTimeSeconds() + " seconds");
}
}