- Timestamp:
- 02/03/13 20:29:16 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Tests/JAVA/test/src/main/java/test/threads/experiment2/Main.java
r576 r577 10 10 import com.lmax.disruptor.dsl.Disruptor; 11 11 12 import static com.google.common.base.Preconditions.checkElementIndex;13 12 import static test.threads.queue.stressed.ITask.BenchmarkResult; 14 13 … … 18 17 */ 19 18 public class Main { 19 private static final boolean ASYNC = Boolean.getBoolean( "async" ); 20 20 private static final boolean PREDICABLE_SCAN = Boolean.getBoolean( "predictable" ); 21 21 22 private static final int RUNS = Integer.getInteger( "runs", 16 ); 23 private static final int TURNS = Integer.getInteger( "turns", 512 ); 24 25 private static final int MESSAGES = 1 << 8; 26 27 private static final int REPOSITORY_SIZE = Integer.getInteger( "repository-size", 1 << 16 ); 22 private static final int RUNS = Integer.getInteger( "runs", 32 ); 23 private static final int TURNS = Integer.getInteger( "turns", 1 << 20 ); 24 25 private static final int REPOSITORY_SIZE_EXP = Integer.getInteger( "repository-size-exp", 15 ); 26 private static final int REPOSITORY_SIZE = 1 << REPOSITORY_SIZE_EXP; 28 27 private static final int READ_PER_MESSAGE = 128; 29 28 private static final int READ_STEP = 1025; … … 34 33 public static void main( final String[] args ) { 35 34 System.out.printf( 36 " Repo: %d, reads: %d/msg, step: %d%s\n" +37 " %d runs by %d turns by %d messages each\n",38 REPOSITORY_SIZE, READ_PER_MESSAGE, READ_STEP,39 PREDICABLE_SCAN ? "predictable" : "random",40 RUNS, TURNS, MESSAGES41 );42 43 44 final Message[] messages = messages( MESSAGES, REPOSITORY_SIZE ); 35 "%s, Repo: double[%d], %d reads/msg, %s\n" + 36 "\t%d runs by %d messages each\n", 37 ( ASYNC ? "ASYNC" : "SYNC" ), 38 REPOSITORY_SIZE, READ_PER_MESSAGE, 39 PREDICABLE_SCAN ? "step: " + READ_STEP : "random", 40 RUNS, TURNS 41 ); 42 43 45 44 final double[] repositoryA = new double[REPOSITORY_SIZE]; 46 45 Arrays.fill( repositoryA, 1.0 ); … … 52 51 final Phase b = new Phase( repositoryB ); 53 52 54 final Function<Message, Message> function = composeSync( a, b ); 53 final Function<Message, Message> function; 54 if( ASYNC ) { 55 function = composeAsync( a, b ); 56 } else { 57 function = composeSync( a, b ); 58 } 55 59 56 60 final BenchmarkResult[] results = benchmark( 57 61 function, 58 messages,59 62 TURNS, 60 63 RUNS 61 64 ); 62 65 66 double sum = 0; 67 double sum2 = 0; 63 68 for( final BenchmarkResult result : results ) { 69 final double pps = 1.0 * result.timeElapsedNanoseconds / result.packetsCompleted; 70 sum += pps; 71 sum2 += pps * pps; 64 72 System.out.printf( 65 73 "%d ns/%d packets -> %.1f ns/packet\n", 66 74 result.timeElapsedNanoseconds, 67 75 result.packetsCompleted, 68 1.0 * result.timeElapsedNanoseconds / result.packetsCompleted76 pps 69 77 ); 70 78 } 71 } 72 73 private static Message[] messages( final int size, 74 final int repositorySize ) { 75 final Message[] messages = new Message[size]; 76 final int step = REPOSITORY_SIZE / size; 77 for( int i = 0; i < messages.length; i++ ) { 78 final Message message = new Message(); 79 80 final int index = i * step % repositorySize; 81 message.index = index; 82 83 checkElementIndex( message.index, repositorySize ); 84 85 message.amount = i / 2 - size; 86 87 messages[i] = message; 88 } 89 return messages; 79 final double avg = sum / results.length; 80 final double std = Math.sqrt( sum2 / results.length - avg * avg ); 81 System.out.printf( 82 "\t\t\taverage \t\t %.1f ns/packet (+/- %.0f%%)\n", 83 avg, 84 std / avg * 100 85 ); 90 86 } 91 87 … … 95 91 if( PREDICABLE_SCAN ) { 96 92 for( int i = 0; i < READ_PER_MESSAGE; i++ ) { 97 final int index = ( i * READ_STEP + offset ) % repository.length;93 final int index = ( i * READ_STEP + offset ) % REPOSITORY_SIZE; 98 94 repository[index] += amount; 99 95 } … … 101 97 int index = offset; 102 98 for( int i = 0; i < READ_PER_MESSAGE; i++ ) { 103 index = Math.abs( index * offset ) % repository.length;99 index = Math.abs( index * offset ) % REPOSITORY_SIZE; 104 100 repository[index] += amount; 105 101 } … … 109 105 110 106 private static BenchmarkResult[] benchmark( final Function<Message, Message> function, 111 final Message[] messages,112 107 final int turns, 113 108 final int runs ) { 114 109 final BenchmarkResult[] results = new BenchmarkResult[runs]; 110 final Message message = new Message(); 115 111 for( int run = 0; run < runs; run++ ) { 116 112 final long startedAt = System.nanoTime(); 117 for( int turn = 0; turn < turns; turn++ ) { 118 benchmarkTurn( messages, function ); 119 } 113 benchmarkTurn( message, function, turns ); 120 114 final long finishedAt = System.nanoTime(); 121 115 … … 123 117 -1, 124 118 finishedAt - startedAt, 125 messages.length *turns119 turns 126 120 ); 127 121 } … … 129 123 } 130 124 131 private static void benchmarkTurn( final Message[] messages, 132 final Function<Message, Message> f ) { 133 for( final Message message : messages ) { 134 message.index++; 125 private static void benchmarkTurn( final Message message, 126 final Function<Message, Message> f, 127 final int turns ) { 128 final double amountStep = 1.0 / turns; 129 for( int turn = 0; turn < turns; turn++ ) { 130 message.index = ( message.index + 1 ) / REPOSITORY_SIZE; 131 message.amount += amountStep; 135 132 f.apply( message ); 136 133 }
Note: See TracChangeset
for help on using the changeset viewer.