Skip to content

Commit

Permalink
Use nano time instead of current time millis. (#552)
Browse files Browse the repository at this point in the history
* Use nano time instead of current time millis.

* Add license header
  • Loading branch information
matenhagen authored and hierynomus committed Jan 22, 2020
1 parent 327a4c4 commit d10a33e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main/java/net/schmizz/sshj/common/StreamCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;

public class StreamCopier {

Expand Down Expand Up @@ -125,7 +126,7 @@ public long copy()
long count = 0;
int read = 0;

final long startTime = System.currentTimeMillis();
final long startTime = System.nanoTime();

if (length == -1) {
while ((read = in.read(buf)) != -1) {
Expand All @@ -140,7 +141,7 @@ public long copy()
if (!keepFlushing)
out.flush();

final double timeSeconds = (System.currentTimeMillis() - startTime) / 1000.0;
final double timeSeconds = TimeUnit.NANOSECONDS.toMillis (System.nanoTime() - startTime) / 1000.0;
final double sizeKiB = count / 1024.0;
log.debug(String.format("%1$,.1f KiB transferred in %2$,.1f seconds (%3$,.2f KiB/s)", sizeKiB, timeSeconds, (sizeKiB / timeSeconds)));

Expand Down
49 changes: 49 additions & 0 deletions src/test/java/net/schmizz/sshj/common/StreamCopierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C)2009 - SSHJ Contributors
*
* Licensed 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.
*/
package net.schmizz.sshj.common;

import org.junit.Test;

import java.io.*;
import java.util.Random;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;


public class StreamCopierTest {

@Test
public void copy() throws IOException {
Random random = new Random();
byte[] data = new byte[1024];
random.nextBytes(data);
InputStream inputStream = new ByteArrayInputStream(data);

OutputStream outputStream = new ByteArrayOutputStream();
LoggerFactory loggerFactory = mock(LoggerFactory.class);

org.slf4j.Logger logger = mock(org.slf4j.Logger.class);
when(loggerFactory.getLogger(StreamCopier.class)).thenReturn(logger);
StreamCopier streamCopier = new StreamCopier(inputStream, outputStream, loggerFactory);

long copied = streamCopier.copy();
assertThat(copied, is(1024L));

verify(logger).debug(contains("1.0 KiB transferred"));
}
}

0 comments on commit d10a33e

Please sign in to comment.