Created
January 26, 2012 02:05
-
-
Save alphazero/1680466 to your computer and use it in GitHub Desktop.
micro benchmark for testing the concurrent queues
This file contains 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
/* | |
* Copyright 2012 Joubin Houshyar | |
* | |
* 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 oss.alphazero.util.concurrent; | |
import java.util.Queue; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.locks.LockSupport; | |
import oss.alphazero.util.Log; | |
public class AdHocTestConcurrentQueue { | |
public static void main(String[] args) { | |
new AdHocTestConcurrentQueue().run(); | |
} | |
private final void run () { | |
Queue<byte[]> q = new ConsumerProducerQueue<byte[]>(); | |
// Queue<byte[]> q = new LinkedBlockingQueue<byte[]>(); | |
final Thread tproducer = new Thread(newProducerTask(q), "producer"); | |
final Thread tconsumer = new Thread(newConsumerTask(q), "consumer"); | |
try { | |
tconsumer.start(); | |
tproducer.start(); | |
} catch (Throwable e) { e.printStackTrace(); System.exit(1); } | |
try { | |
tproducer.join(); | |
tconsumer.join(); | |
} catch (Throwable e) { e.printStackTrace(); System.exit(1); } | |
} | |
private final Runnable newProducerTask (final Queue<byte[]> q) { | |
return new Runnable() { | |
@Override final public void run() { | |
byte[] data = {100,100,100,100,100,100, 10, 13}; | |
final int iters = 4000; | |
for(;;){ | |
for(int i=0; i<iters; i++){ | |
q.offer(data); | |
} | |
LockSupport.parkNanos(10L); | |
} | |
} | |
}; | |
} | |
private final Runnable newConsumerTask (final Queue<byte[]> q) { | |
return new Runnable() { | |
@Override final public void run() { | |
int n = 0; | |
for(;;){ | |
long start = System.nanoTime(); | |
long rlen = 0; | |
while(rlen < 1250000){ // 10Mbits | |
final byte[] data = q.poll(); | |
if(data == null){ | |
LockSupport.parkNanos(10L); | |
} | |
else { | |
rlen += data.length; | |
} | |
} | |
long delta = System.nanoTime() - start; | |
n++; | |
try { | |
long bps = rlen*8*1000000000 / delta; | |
long wps = (rlen*1000000000/8) / delta; | |
System.out.format("[%05d] bytes:%d - delta:%4d msec - bps:%9d - wps:%8d\n", n, rlen, TimeUnit.NANOSECONDS.toMillis(delta), bps, wps); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
Log.error("divide by zero - bytes:%d - delta: %d",rlen, delta); | |
} | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment