Skip to content

Commit 9328a09

Browse files
Merge pull request EMCECS#9 from EMCECS/feature-zero-byte-speedup
Feature zero byte speedup
2 parents cf24c47 + eb10c60 commit 9328a09

File tree

4 files changed

+139
-2
lines changed

4 files changed

+139
-2
lines changed

readingTools/build.gradle

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2017 EMC Corporation. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0.txt
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
description = 'nfsclient readingTools jar - provides utility commands to test reading performance.'
16+
17+
ext.mainClass = 'com.emc.ecs.nfsclient.nfs.io.FileReadingTest'
18+
19+
buildscript {
20+
apply from: "$commonBuildDir/ecs-tool.buildscript.gradle", to: buildscript
21+
}
22+
23+
apply from: "$commonBuildDir/ecs-tool.subproject.gradle"
24+
25+
dependencies {
26+
compile project(':')
27+
testRuntime "org.slf4j:slf4j-simple:1.7.5"
28+
testCompile project(':').sourceSets.test.output,
29+
"junit:junit:4.11"
30+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Copyright 2017 EMC Corporation. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0.txt
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.emc.ecs.nfsclient.nfs.io;
16+
17+
import java.io.IOException;
18+
19+
import com.emc.ecs.nfsclient.nfs.NfsReadResponse;
20+
import com.emc.ecs.nfsclient.nfs.nfs3.Nfs3;
21+
import com.emc.ecs.nfsclient.rpc.CredentialUnix;
22+
23+
/**
24+
* @author seibed
25+
*
26+
*/
27+
public class FileReadingTest {
28+
29+
public static void main(String[] args) {
30+
String export = args[0];
31+
String fileName = args[1];
32+
boolean useStream = Boolean.valueOf(args[2]);
33+
try {
34+
if (useStream) {
35+
testStreamReading(export, fileName);
36+
} else {
37+
testReading(export, fileName);
38+
}
39+
} catch (Exception e) {
40+
System.out.println(e.getMessage());
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
public static void testStreamReading(String export, String fileName)
46+
throws Exception {
47+
Nfs3 nfs3 = new Nfs3(export, new CredentialUnix(0, 0, null), 3);
48+
NfsFileInputStream inputStream = null;
49+
try {
50+
NfsFile nfsFile = new Nfs3File(nfs3, fileName);
51+
int fileLength = (int) nfsFile.length();
52+
inputStream = new NfsFileInputStream(nfsFile);
53+
byte[] buffer = new byte[fileLength];
54+
System.out.println("Testing stream reading");
55+
int bytesRead = inputStream.read(buffer);
56+
if (NfsFileInputStream.EOF != inputStream.read()) {
57+
throw new Exception("Reading error - should have been at the end of the file");
58+
}
59+
if (fileLength != bytesRead) {
60+
throw new Exception("Reading error - read " + bytesRead
61+
+ " bytes, should have been " + fileLength);
62+
}
63+
System.out.println("Success!");
64+
} catch (Exception e) {
65+
System.out.println(e.getMessage());
66+
} finally {
67+
if (inputStream != null) {
68+
try {
69+
inputStream.close();
70+
} catch (IOException e) {
71+
// e.printStackTrace();
72+
}
73+
}
74+
}
75+
}
76+
77+
public static void testReading(String export, String fileName)
78+
throws Exception {
79+
Nfs3 nfs3 = new Nfs3(export, new CredentialUnix(0, 0, null), 3);
80+
try {
81+
NfsFile nfsFile = new Nfs3File(nfs3, fileName);
82+
int fileLength = (int) nfsFile.length();
83+
byte[] buffer = new byte[fileLength];
84+
System.out.println("Testing with plain read");
85+
NfsReadResponse response = nfsFile.read(0, buffer.length, buffer, 0);
86+
if (!response.isEof()) {
87+
throw new Exception("Reading error - should have been at the end of the file");
88+
}
89+
if (fileLength != response.getBytesRead()) {
90+
throw new Exception("Reading error - read " + response.getBytesRead()
91+
+ " bytes, should have been " + fileLength);
92+
}
93+
System.out.println("Success!");
94+
} catch (Exception e) {
95+
System.out.println(e.getMessage());
96+
}
97+
}
98+
99+
}

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 EMC Corporation. All Rights Reserved.
2+
* Copyright 2016-2017 EMC Corporation. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License").
55
* You may not use this file except in compliance with the License.
@@ -14,3 +14,4 @@
1414
*/
1515
rootProject.name = 'nfs-client'
1616
include 'tools'
17+
include 'readingTools'

src/main/java/com/emc/ecs/nfsclient/nfs/io/NfsFileInputStream.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public NfsFileInputStream(NfsFile<?, ?> nfsFile) throws IOException {
166166
*/
167167
private byte[] makeBytes(int maximumBufferSize) throws IOException {
168168
int bufferSize = Math.min((int) Math.min(_file.length() - _offset, Integer.MAX_VALUE), maximumBufferSize);
169+
if (bufferSize == 0) {
170+
_isEof = true;
171+
}
169172
return new byte[bufferSize];
170173
}
171174

@@ -322,11 +325,15 @@ private void checkForClosed() throws IOException {
322325
}
323326

324327
/**
325-
* If the buffer has no more bytes to be read, load more bytes.
328+
* If the buffer has no more bytes to be read, and there are bytes available in the file, load more bytes.
326329
*
327330
* @throws IOException
328331
*/
329332
private void loadBytesAsNeeded() throws IOException {
333+
if (available() <= 0) {
334+
_isEof = true;
335+
}
336+
330337
while ((!_isEof) && (bytesLeftInBuffer() <= 0)) {
331338
_currentBufferPosition = 0;
332339
NfsReadResponse response = _file.read(_offset, _bytes.length, _bytes, _currentBufferPosition);

0 commit comments

Comments
 (0)