This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
AttributeStoragePerformanceTestSupport.java
286 lines (239 loc) · 11.4 KB
/
AttributeStoragePerformanceTestSupport.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2012 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.proxy.attributes.perf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;
import org.mockito.Mockito;
import org.sonatype.nexus.configuration.application.ApplicationConfiguration;
import org.sonatype.nexus.mime.MimeRulesSource;
import org.sonatype.nexus.mime.MimeSupport;
import org.sonatype.nexus.proxy.ItemNotFoundException;
import org.sonatype.nexus.proxy.LocalStorageException;
import org.sonatype.nexus.proxy.NoSuchRepositoryException;
import org.sonatype.nexus.proxy.ResourceStoreRequest;
import org.sonatype.nexus.proxy.access.AccessManager;
import org.sonatype.nexus.proxy.attributes.AttributeStorage;
import org.sonatype.nexus.proxy.attributes.Attributes;
import org.sonatype.nexus.proxy.attributes.DefaultAttributesHandler;
import org.sonatype.nexus.proxy.attributes.StorageFileItemInspector;
import org.sonatype.nexus.proxy.attributes.StorageItemInspector;
import org.sonatype.nexus.proxy.attributes.perf.internal.TMockRepository;
import org.sonatype.nexus.proxy.attributes.perf.internal.TestRepositoryItemUid;
import org.sonatype.nexus.proxy.item.AbstractStorageItem;
import org.sonatype.nexus.proxy.item.ContentLocator;
import org.sonatype.nexus.proxy.item.DefaultStorageFileItem;
import org.sonatype.nexus.proxy.item.LinkPersister;
import org.sonatype.nexus.proxy.item.RepositoryItemUid;
import org.sonatype.nexus.proxy.item.RepositoryItemUidFactory;
import org.sonatype.nexus.proxy.item.RepositoryItemUidLock;
import org.sonatype.nexus.proxy.item.StorageFileItem;
import org.sonatype.nexus.proxy.item.StringContentLocator;
import org.sonatype.nexus.proxy.repository.Repository;
import org.sonatype.nexus.proxy.storage.local.fs.DefaultFSLocalRepositoryStorage;
import org.sonatype.nexus.proxy.storage.local.fs.DefaultFSPeer;
import org.sonatype.nexus.proxy.storage.local.fs.FileContentLocator;
import org.sonatype.nexus.proxy.wastebasket.Wastebasket;
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
import com.carrotsearch.junitbenchmarks.BenchmarkRule;
/**
* The performance tests for specific implementations of AttributesStorage.
*/
public abstract class AttributeStoragePerformanceTestSupport
{
@Rule
public MethodRule benchmarkRun = new BenchmarkRule();
private Repository repository;
private AttributeStorage attributeStorage;
protected ApplicationConfiguration applicationConfiguration;
final protected String testFilePath = "content/file.txt";
private DefaultFSLocalRepositoryStorage localRepositoryStorageUnderTest;
protected long originalLastAccessTime;
final private RepositoryItemUidFactory repositoryItemUidFactory = Mockito.mock( RepositoryItemUidFactory.class );
final private File CONTENT_TEST_FILE = new File( "target/" + getClass().getSimpleName() + "/testContent.txt" );
final private String SHA1_ATTRIBUTE_KEY = "digest.sha1";
final private String SHA1_ATTRIBUTE_VALUE = "100f4ae295695335ef5d3346b42ed81ecd063fc9";
final private static int ITERATION_COUNT = 100;
@Before
public void setup()
throws Exception
{
File repoStorageDir = new File( "target/" + getClass().getSimpleName() + "/repo-storage/" );
String repoLocalURL = repoStorageDir.getAbsoluteFile().toURI().toString();
// write a test file
File testFile = new File( repoStorageDir, testFilePath );
testFile.getParentFile().mkdirs();
org.codehaus.plexus.util.FileUtils.fileWrite( testFile, "CONTENT" );
CONTENT_TEST_FILE.getParentFile().mkdirs();
org.codehaus.plexus.util.FileUtils.fileWrite( CONTENT_TEST_FILE, "CONTENT" );
// Mocks
Wastebasket wastebasket = mock( Wastebasket.class );
LinkPersister linkPersister = mock( LinkPersister.class );
MimeSupport mimeSupport = mock( MimeSupport.class );
when( mimeSupport.guessMimeTypeFromPath( Mockito.any( MimeRulesSource.class ), Mockito.anyString() ) ).thenReturn(
"text/plain" );
// Application Config
applicationConfiguration = mock( ApplicationConfiguration.class );
when( applicationConfiguration.getWorkingDirectory( eq( "proxy/attributes-ng" ) ) ).thenReturn(
new File( "target/" + this.getClass().getSimpleName() + "/attributes-ng" ) );
when( applicationConfiguration.getWorkingDirectory( eq( "proxy/attributes" ) ) ).thenReturn(
new File( "target/" + this.getClass().getSimpleName() + "/attributes" ) );
// remove any event inspectors from the timing
List<StorageItemInspector> itemInspectorList = new ArrayList<StorageItemInspector>();
List<StorageFileItemInspector> fileItemInspectorList = new ArrayList<StorageFileItemInspector>();
// set the AttributeStorage on the Attribute Handler
attributeStorage = getAttributeStorage();
DefaultAttributesHandler attributesHandler =
new DefaultAttributesHandler( applicationConfiguration, attributeStorage, null, itemInspectorList,
fileItemInspectorList );
// this test assumes attributes are ALWAYS written
attributesHandler.setLastRequestedResolution( 0 );
// Need to use the MockRepository
// Do NOT us a mockito mock, using answers does not play well with junitbenchmark
repository = new TMockRepository( "testRetieveItem-repo", repositoryItemUidFactory);
repository.setLocalUrl( repoLocalURL );
repository.setAttributesHandler( attributesHandler );
// setup the class under test
localRepositoryStorageUnderTest =
new DefaultFSLocalRepositoryStorage( wastebasket, linkPersister, mimeSupport, new DefaultFSPeer() );
// prime the retrieve
ResourceStoreRequest resourceRequest = new ResourceStoreRequest( testFilePath );
originalLastAccessTime =
localRepositoryStorageUnderTest.retrieveItem( repository, resourceRequest ).getLastRequested();
Thread.sleep( 10 );
}
public abstract AttributeStorage getAttributeStorage();
// ////////////
// Test writes
// ////////////
@Test
public void test0PrimePutAndGettAttribute()
throws IOException
{
writeEntryToAttributeStorage( "/prime.txt" );
getStorageItemFromAttributeStore( "/prime.txt" );
}
@Test
public void test1PutAttribute()
throws IOException
{
writeEntryToAttributeStorage( "/a.txt" );
}
@Test
public void test2PutAttributeX100()
throws IOException
{
for ( int ii = 0; ii < ITERATION_COUNT; ii++ )
{
writeEntryToAttributeStorage( "/" + ii + ".txt" );
}
}
@Test
public void test3GetAttribute()
throws IOException
{
Attributes storageItem = getStorageItemFromAttributeStore( "/a.txt" );
assertThat( storageItem.get( SHA1_ATTRIBUTE_KEY ), equalTo( SHA1_ATTRIBUTE_VALUE ) );
}
@Test
public void test4GetAttributeX100()
throws IOException
{
for ( int ii = 0; ii < ITERATION_COUNT; ii++ )
{
getStorageItemFromAttributeStore( "/" + ii + ".txt" );
}
}
// @Test
public void test5DeleteAttributes()
throws IOException
{
deleteStorageItemFromAttributeStore( "/a.txt" );
}
// @Test
public void test6DeleteAttributesX100()
throws IOException
{
for ( int ii = 0; ii < ITERATION_COUNT; ii++ )
{
deleteStorageItemFromAttributeStore( "/" + ii + ".txt" );
}
}
@BenchmarkOptions( benchmarkRounds = 1000, warmupRounds = 1 )
@Test
public void testRetieveItemWithLastAccessUpdate()
throws LocalStorageException, ItemNotFoundException
{
ResourceStoreRequest resourceRequest = new ResourceStoreRequest( testFilePath );
resourceRequest.getRequestContext().put( AccessManager.REQUEST_REMOTE_ADDRESS, "127.0.0.1" );
AbstractStorageItem storageItem = localRepositoryStorageUnderTest.retrieveItem( repository, resourceRequest );
MatcherAssert.assertThat( storageItem, Matchers.notNullValue() );
MatcherAssert.assertThat( storageItem.getLastRequested(), Matchers.greaterThan( originalLastAccessTime ) );
}
@BenchmarkOptions( benchmarkRounds = 1000, warmupRounds = 1 )
@Test
public void testRetieveItemWithoutLastAccessUpdate()
throws LocalStorageException, ItemNotFoundException
{
ResourceStoreRequest resourceRequest = new ResourceStoreRequest( testFilePath );
AbstractStorageItem storageItem = localRepositoryStorageUnderTest.retrieveItem( repository, resourceRequest );
MatcherAssert.assertThat( storageItem, Matchers.notNullValue() );
MatcherAssert.assertThat( storageItem.getLastRequested(), Matchers.equalTo( originalLastAccessTime ) );
}
protected void writeEntryToAttributeStorage( String path )
throws IOException
{
StorageFileItem storageFileItem =
new DefaultStorageFileItem( repository, new ResourceStoreRequest( path ), true, true, getContentLocator() );
storageFileItem.getRepositoryItemAttributes().put( SHA1_ATTRIBUTE_KEY, SHA1_ATTRIBUTE_VALUE );
storageFileItem.getRepositoryItemAttributes().put( "digest.md5", "f62472816fb17de974a87513e2257d63" );
storageFileItem.getRepositoryItemAttributes().put( "request.address", "127.0.0.1" );
attributeStorage.putAttributes( storageFileItem.getRepositoryItemUid(),
storageFileItem.getRepositoryItemAttributes() );
}
protected Attributes getStorageItemFromAttributeStore( String path )
throws IOException
{
RepositoryItemUid repositoryItemUid = new TestRepositoryItemUid( repositoryItemUidFactory, repository, path );
return attributeStorage.getAttributes( repositoryItemUid );
}
private void deleteStorageItemFromAttributeStore( String path )
throws IOException
{
RepositoryItemUid repositoryItemUid = new TestRepositoryItemUid( repositoryItemUidFactory, repository, path );
assertThat( "Attribute was not removed from store.", attributeStorage.deleteAttributes( repositoryItemUid ) );
}
private ContentLocator getContentLocator()
{
if ( true )
{
return new StringContentLocator( "CONTENT" );
}
else
{
return new FileContentLocator( CONTENT_TEST_FILE, "text/plain" );
}
}
}