- Timestamp:
- 01/30/13 21:08:08 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Tests/JAVA/test/src/main/java/test/threads/experiment2/Main.java
r573 r574 12 12 13 13 /** 14 * IDEA: 15 * use common cyclic buffer with small capacity 16 * in single-threaded case just go around, 17 * in multithreaded use lzaySet cursors 18 * 14 19 * @author ruslan 15 20 * created 27.01.13 at 23:00 … … 21 26 private static final int TURNS = 1000; 22 27 private static final int MESSAGES = 1 << 16; 23 private static final int REPOSITORY_SIZE = 1 << 20;28 private static final int REPOSITORY_SIZE = 1 << 16; 24 29 25 30 public static void main( String[] args ) { … … 30 35 Arrays.fill( repositoryB, 1.0 ); 31 36 32 final BenchmarkResult[] results = benchmarkSync(33 messages,34 repositoryA, 35 repositoryB,36 TURNS, 37 RUNS38 ); 37 final TaskA a = new TaskA( repositoryA ); 38 final TaskB b = new TaskB( repositoryB ); 39 40 final Function<Message, Message> syncF = composeSync( a, b ); 41 42 final BenchmarkResult[] results = benchmark( syncF, messages, TURNS, RUNS ); 43 39 44 for( final BenchmarkResult result : results ) { 40 45 System.out.printf( … … 45 50 ); 46 51 } 52 } 53 54 private static Function<Message, Message> composeSync( final TaskA a, 55 final TaskB b ) { 56 return Functions.compose( b, a ); 47 57 } 48 58 … … 59 69 } 60 70 61 private static BenchmarkResult[] benchmarkSync( final Message[] messages, 62 final double[] repositoryA, 63 final double[] repositoryB, 64 final int turns, 65 final int runs ) { 66 final TaskA a = new TaskA( repositoryA ); 67 final TaskB b = new TaskB( repositoryB ); 68 final Function<Message, Message> ba = Functions.compose( b, a ); 71 72 private static BenchmarkResult[] benchmark( final Function<Message, Message> function, 73 final Message[] messages, 74 final int turns, 75 final int runs ) { 69 76 final BenchmarkResult[] results = new BenchmarkResult[runs]; 70 77 for( int j = 0; j < runs; j++ ) { 71 78 final long startedAt = System.nanoTime(); 72 79 for( int i = 0; i < turns; i++ ) { 73 benchmark SyncTurn( messages, ba);80 benchmarkTurn( messages, function ); 74 81 } 75 82 final long finishedAt = System.nanoTime(); … … 84 91 } 85 92 86 private static void benchmark SyncTurn( final Message[] messages,87 93 private static void benchmarkTurn( final Message[] messages, 94 final Function<Message, Message> f ) { 88 95 for( final Message message : messages ) { 89 96 f.apply( message ); 90 97 } 91 }92 93 private static BenchmarkResult[] benchmarkAsync( final Message[] messages,94 final double[] repositoryA,95 final double[] repositoryB,96 final int turns,97 final int runs ) {98 final TaskA a = new TaskA( repositoryA );99 final TaskB b = new TaskB( repositoryB );100 final Function<Message, Message> ba = composeAsync( b, a );101 final BenchmarkResult[] results = new BenchmarkResult[runs];102 for( int j = 0; j < runs; j++ ) {103 final long startedAt = System.nanoTime();104 for( int i = 0; i < turns; i++ ) {105 benchmarkSyncTurn( messages, ba );106 }107 final long finishedAt = System.nanoTime();108 109 results[j] = new BenchmarkResult(110 -1,111 finishedAt - startedAt,112 messages.length * turns113 );114 }115 return results;116 98 } 117 99 … … 127 109 }, 128 110 Executors.newSingleThreadExecutor(), 129 new SingleThreadedClaimStrategy( 128),111 new SingleThreadedClaimStrategy( 32 ), 130 112 new BusySpinWaitStrategy() 131 113 ); … … 141 123 142 124 return new Function<Message, Message>() { 125 private final Translator translator = new Translator(); 126 143 127 @Override 144 128 public Message apply( final Message in ) { 145 final Message inA = a.apply( in ); 146 disruptor.publishEvent( new EventTranslator<Message>() { 147 @Override 148 public Message translateTo( final Message event, 149 final long sequence ) { 150 event.from = inA.from; 151 event.to = inA.to; 152 event.amount = inA.amount; 153 return event; 154 } 155 } ); 129 a.apply( in ); 130 translator.with( in ); 131 disruptor.publishEvent( translator ); 156 132 return null; 157 133 } … … 194 170 } 195 171 172 private static class Translator implements EventTranslator<Message> { 173 private Message message; 174 175 public void with( final Message message ) { 176 this.message = message; 177 } 178 179 @Override 180 public Message translateTo( final Message event, 181 final long sequence ) { 182 event.from = message.from; 183 event.to = message.to; 184 event.amount = message.amount; 185 return event; 186 } 187 } 196 188 }
Note: See TracChangeset
for help on using the changeset viewer.