-
-
Notifications
You must be signed in to change notification settings - Fork 110
Using ThreadLocal Digester for NameBasedGenerator to avoid synchronized #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
magnoyu
wants to merge
2
commits into
cowtowncoder:master
Choose a base branch
from
magnoyu:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,25 @@ public abstract class StringArgGenerator extends UUIDGenerator | |
*/ | ||
public abstract UUID generate(String name); | ||
|
||
/** | ||
* Method for generating name-based UUIDs using specified name (serialized to | ||
* bytes using UTF-8 encoding). No synchronization is performed on digester. | ||
* Digester is assumed to be created with ThreadLocal. | ||
*/ | ||
public abstract UUID concurrentGenerate(String name); | ||
/** | ||
* Method for generating name-based UUIDs using specified byte-serialization | ||
* of name. | ||
* | ||
* @since 3.1 | ||
*/ | ||
public abstract UUID generate(byte[] nameBytes); | ||
|
||
/** | ||
* Method for generating name-based UUIDs using specified byte-serialization | ||
* of name. No synchronization is performed on digester. Digester is assumed | ||
* to be created with ThreadLocal. | ||
Comment on lines
+33
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above, can we put an example when to use this method |
||
*/ | ||
public abstract UUID concurrentGenerate(byte[] nameBytes); | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
package perf; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.UUID; | ||
import java.util.concurrent.BrokenBarrierException; | ||
import java.util.concurrent.CyclicBarrier; | ||
|
||
import com.fasterxml.uuid.*; | ||
import com.fasterxml.uuid.impl.RandomBasedGenerator; | ||
|
@@ -51,11 +55,14 @@ public void test() throws Exception | |
final TimeBasedGenerator timeGenPlain = Generators.timeBasedGenerator(nic); | ||
final TimeBasedGenerator timeGenSynced = Generators.timeBasedGenerator(nic, | ||
new com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer()); | ||
final StringArgGenerator nameGen = Generators.nameBasedGenerator(namespaceForNamed); | ||
|
||
|
||
final MessageDigest digester = MessageDigest.getInstance("SHA-1"); | ||
final StringArgGenerator nameGen = Generators.nameBasedGenerator(namespaceForNamed, digester); | ||
final StringArgGenerator nameGenConcurrent = Generators.nameBasedGenerator(namespaceForNamed); | ||
|
||
while (true) { | ||
try { Thread.sleep(100L); } catch (InterruptedException ie) { } | ||
int round = (i++ % 7); | ||
int round = (i++ % 10); | ||
|
||
long curr = System.currentTimeMillis(); | ||
String msg; | ||
|
@@ -93,19 +100,32 @@ public void test() throws Exception | |
testRandom(uuids, ROUNDS, utilRandomGen); | ||
break; | ||
|
||
|
||
case 6: | ||
msg = "Jug, name-based"; | ||
testNameBased(uuids, ROUNDS, nameGen); | ||
break; | ||
|
||
/* | ||
case 7: | ||
msg = "Jug, name-based, concurrent"; | ||
testNameBasedConcurrent(uuids, ROUNDS, nameGenConcurrent); | ||
break; | ||
|
||
case 8: | ||
msg = "Jug, name-based, ten threads"; | ||
testNameBasedTenThreads(uuids, ROUNDS, namespaceForNamed); | ||
break; | ||
|
||
case 9: | ||
msg = "Jug, name-based, concurrent, ten threads"; | ||
testNameBasedConcurrentTenThreads(uuids, ROUNDS, namespaceForNamed); | ||
break; | ||
/* | ||
case 8: | ||
msg = "http://johannburkard.de/software/uuid/"; | ||
testUUID32(uuids, ROUNDS); | ||
break; | ||
*/ | ||
|
||
default: | ||
throw new Error("Internal error"); | ||
} | ||
|
@@ -175,7 +195,67 @@ private final void testNameBased(Object[] uuids, int rounds, StringArgGenerator | |
} | ||
} | ||
} | ||
|
||
|
||
private final void testNameBasedConcurrent(Object[] uuids, int rounds, StringArgGenerator uuidGen) | ||
{ | ||
while (--rounds >= 0) { | ||
for (int i = 0, len = uuids.length; i < len; ++i) { | ||
uuids[i] = uuidGen.concurrentGenerate(NAME); | ||
} | ||
} | ||
} | ||
|
||
private final void testNameBasedTenThreads(Object[] uuids, int rounds, final UUID namespaceForNamed) throws InterruptedException, BrokenBarrierException, NoSuchAlgorithmException | ||
{ | ||
while (--rounds >= 0) { | ||
final CyclicBarrier gate = new CyclicBarrier(11); | ||
final MessageDigest digester = MessageDigest.getInstance("SHA-1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
final StringArgGenerator nameGen = Generators.nameBasedGenerator(namespaceForNamed, digester); | ||
for (int j = 0; j < 10; j ++) { | ||
final Object[] fuuids = uuids; | ||
Thread t = new Thread(new Runnable() { | ||
public void run() { | ||
try { | ||
gate.await(); | ||
for (int i = 0, len = fuuids.length; i < len; ++i) { | ||
fuuids[i] = nameGen.generate(NAME); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
t.start(); | ||
} | ||
gate.await(); | ||
} | ||
} | ||
|
||
private final void testNameBasedConcurrentTenThreads(Object[] uuids, int rounds, final UUID namespaceForNamed) throws InterruptedException, BrokenBarrierException, NoSuchAlgorithmException | ||
{ | ||
while (--rounds >= 0) { | ||
final CyclicBarrier gate = new CyclicBarrier(11); | ||
final Object[] fuuids = uuids; | ||
for (int j = 0; j < 10; j ++) { | ||
Thread t = new Thread(new Runnable() { | ||
public void run() { | ||
final StringArgGenerator nameGen = Generators.nameBasedGenerator(namespaceForNamed); | ||
try { | ||
gate.await(); | ||
for (int i = 0, len = fuuids.length; i < len; ++i) { | ||
fuuids[i] = nameGen.concurrentGenerate(NAME); | ||
} | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
t.start(); | ||
} | ||
gate.await(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception | ||
{ | ||
new MeasurePerformance().test(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: It will be better if we can provide an use-case example when we should use this method.