-
Notifications
You must be signed in to change notification settings - Fork 531
/
EventConsumerThreadTest.java
251 lines (217 loc) · 6.91 KB
/
EventConsumerThreadTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package org.apache.maven.plugin.surefire.extensions;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.apache.maven.surefire.api.event.Event;
import org.apache.maven.surefire.api.fork.ForkNodeArguments;
import org.apache.maven.surefire.extensions.EventHandler;
import org.apache.maven.surefire.extensions.util.CountdownCloseable;
import org.junit.Test;
import javax.annotation.Nonnull;
import java.io.Closeable;
import java.io.File;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.util.concurrent.atomic.AtomicInteger;
import static java.lang.Math.min;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.copyOfRange;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.fest.assertions.Assertions.assertThat;
/**
*
*/
public class EventConsumerThreadTest
{
@SuppressWarnings( "checkstyle:magicnumber" )
@Test( timeout = 60_000L )
public void performanceTest() throws Exception
{
final long[] staredAt = {0};
final long[] finishedAt = {0};
final AtomicInteger calls = new AtomicInteger();
final int totalCalls = 1_000_000; // 400_000; // 1_000_000; // 10_000_000;
EventHandler<Event> handler = new EventHandler<Event>()
{
@Override
public void handleEvent( @Nonnull Event event )
{
if ( staredAt[0] == 0 )
{
staredAt[0] = System.currentTimeMillis();
}
if ( calls.incrementAndGet() == totalCalls )
{
finishedAt[0] = System.currentTimeMillis();
}
}
};
final ByteBuffer event = ByteBuffer.allocate( 192 );
event.put( ":maven-surefire-event:".getBytes( UTF_8 ) );
event.put( (byte) 14 );
event.put( ":std-out-stream:".getBytes( UTF_8 ) );
event.put( (byte) 10 );
event.put( ":normal-run:".getBytes( UTF_8 ) );
event.put( (byte) 5 );
event.put( ":UTF-8:".getBytes( UTF_8 ) );
event.putInt( 100 );
event.put(
":0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789:"
.getBytes( UTF_8 ) );
( (Buffer) event ).flip();
byte[] frame = copyOfRange( event.array(), event.arrayOffset(), event.arrayOffset() + event.remaining() );
ReadableByteChannel channel = new Channel( frame, 100 )
{
private int countRounds;
@Override
public synchronized int read( ByteBuffer dst )
{
if ( countRounds == totalCalls )
{
return -1;
}
if ( remaining() == 0 )
{
countRounds++;
i = 0;
}
return super.read( dst );
}
};
EventConsumerThread thread = new EventConsumerThread( "t", channel, handler,
new CountdownCloseable( new MockCloseable(), 1 ), new MockForkNodeArguments() );
SECONDS.sleep( 2 );
System.gc();
SECONDS.sleep( 5 );
System.out.println( "Starting the event thread..." );
thread.start();
thread.join();
long execTime = finishedAt[0] - staredAt[0];
System.out.println( execTime );
// 0.6 seconds while using the encoder/decoder for 10 million messages
assertThat( execTime )
.describedAs( "The performance test should assert 0.75s of read time. "
+ "The limit 3.65s guarantees that the read time does not exceed this limit on overloaded CPU." )
.isPositive()
.isLessThanOrEqualTo( 3_650L );
}
private static class Channel implements ReadableByteChannel
{
private final byte[] bytes;
private final int chunkSize;
protected int i;
Channel( byte[] bytes, int chunkSize )
{
this.bytes = bytes;
this.chunkSize = chunkSize;
}
@Override
public int read( ByteBuffer dst )
{
if ( remaining() == 0 )
{
return -1;
}
else if ( dst.hasRemaining() )
{
int length = min( min( chunkSize, remaining() ), dst.remaining() ) ;
dst.put( bytes, i, length );
i += length;
return length;
}
else
{
return 0;
}
}
protected final int remaining()
{
return bytes.length - i;
}
@Override
public boolean isOpen()
{
return false;
}
@Override
public void close()
{
}
}
private static class MockCloseable implements Closeable
{
@Override
public void close()
{
}
}
private static class MockForkNodeArguments implements ForkNodeArguments
{
@Nonnull
@Override
public String getSessionId()
{
return null;
}
@Override
public int getForkChannelId()
{
return 0;
}
@Nonnull
@Override
public File dumpStreamText( @Nonnull String text )
{
return null;
}
@Nonnull
@Override
public File dumpStreamException( @Nonnull Throwable t )
{
return null;
}
@Override
public void logWarningAtEnd( @Nonnull String text )
{
}
@Nonnull
@Override
public ConsoleLogger getConsoleLogger()
{
return null;
}
@Nonnull
@Override
public Object getConsoleLock()
{
return new Object();
}
@Override
public File getEventStreamBinaryFile()
{
return null;
}
@Override
public File getCommandStreamBinaryFile()
{
return null;
}
}
}