diff options
author | valerijf <valerijf@yahoo-inc.com> | 2016-07-06 15:04:14 +0200 |
---|---|---|
committer | valerijf <valerijf@yahoo-inc.com> | 2016-07-06 15:04:14 +0200 |
commit | dc9317d6f3434408313d8e4a3075507cafbc3bac (patch) | |
tree | 40ab019079f85ac4829251489a49a78032b440c1 /vespa-http-client | |
parent | 01bd98860d8ee4521195c2588e702037a5e18af4 (diff) |
Updated Runner to use FormatInputStream
Diffstat (limited to 'vespa-http-client')
-rw-r--r-- | vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/Runner.java | 45 | ||||
-rw-r--r-- | vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/RunnerTest.java | 35 |
2 files changed, 16 insertions, 64 deletions
diff --git a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/Runner.java b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/Runner.java index bd0b8d5276f..428adfb67fc 100644 --- a/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/Runner.java +++ b/vespa-http-client/src/main/java/com/yahoo/vespa/http/client/runner/Runner.java @@ -7,14 +7,10 @@ import com.yahoo.vespa.http.client.SimpleLoggerResultCallback; import com.yahoo.vespa.http.client.core.JsonReader; import com.yahoo.vespa.http.client.core.XmlFeedReader; -import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; -import java.io.SequenceInputStream; -import java.util.Arrays; -import java.util.Collections; +import java.util.Optional; import java.util.concurrent.atomic.AtomicInteger; /** @@ -33,8 +29,7 @@ public class Runner { * @return send time in ms, not including validating */ public static long send( - FeedClient feedClient, InputStream inputStream, boolean isJson, - AtomicInteger numSent, boolean verbose, boolean addRootElementToXml) { + FeedClient feedClient, InputStream inputStream, boolean isJson, AtomicInteger numSent, boolean verbose) { if (verbose) { System.err.println("Now sending data."); @@ -44,8 +39,7 @@ public class Runner { JsonReader.read(inputStream, feedClient, numSent); } else { try { - XmlFeedReader.read( - addRootElementToXml ? addVespafeedTag(inputStream) : inputStream, feedClient, numSent); + XmlFeedReader.read(inputStream, feedClient, numSent); } catch (Exception e) { System.err.println("Stopped reading feed, got problems with XML: " + e.getMessage()); } @@ -63,38 +57,31 @@ public class Runner { return sendTotalTime; } - // public for testing. - public static InputStream addVespafeedTag(InputStream inputStream) { - return new SequenceInputStream(Collections.enumeration(Arrays.asList( - new InputStream[]{ - new ByteArrayInputStream("<vespafeed>".getBytes()), - inputStream, - new ByteArrayInputStream("</vespafeed>".getBytes()), - })) - ); - } - public static void main(String[] args) throws FileNotFoundException, InterruptedException { + public static void main(String[] args) throws IOException, InterruptedException { final CommandLineArguments commandLineArgs = CommandLineArguments.build(args); if (commandLineArgs == null) { return; } - // TODO: Rather implement this by peeking the stream if possible. - final boolean useJson = commandLineArgs.getFile().endsWith(".json"); - final AtomicInteger numSent = new AtomicInteger(0); - InputStream inputStream = new FileInputStream(commandLineArgs.getFile()); + FormatInputStream formatInputStream = new FormatInputStream( + System.in, + Optional.ofNullable(commandLineArgs.getFile()), + commandLineArgs.getAddRootElementToXml()); + int intervalOfLogging = commandLineArgs.getVerbose() ? commandLineArgs.getWhenVerboseEnabledPrintMessageForEveryXDocuments() : Integer.MAX_VALUE; + final AtomicInteger numSent = new AtomicInteger(0); final SimpleLoggerResultCallback callback = new SimpleLoggerResultCallback(numSent, intervalOfLogging); - final FeedClient feedClient = FeedClientFactory.create(commandLineArgs.createSessionParams(useJson), callback); + final FeedClient feedClient = FeedClientFactory.create( + commandLineArgs.createSessionParams(formatInputStream.getFormat()== FormatInputStream.Format.JSON), callback); long sendTotalTimeMs = send( - feedClient, inputStream, useJson, numSent, commandLineArgs.getVerbose(), - commandLineArgs.getAddRootElementToXml()); + feedClient, formatInputStream.getInputStream(), + formatInputStream.getFormat() == FormatInputStream.Format.JSON, numSent, commandLineArgs.getVerbose()); if (commandLineArgs.getVerbose()) { System.err.println(feedClient.getStatsAsJson()); @@ -102,7 +89,7 @@ public class Runner { double transferTimeSec = ((double) sendTotalTimeMs) / 1000.0; System.err.println("Sent " + fileSizeMb + " MB in " + transferTimeSec + " seconds."); System.err.println("Speed: " + ((fileSizeMb / transferTimeSec) * 8.0) + " Mbits/sec, + HTTP overhead " + - "(not taking compression into account)"); + "(not taking compression into account)"); if (transferTimeSec > 0) { System.err.printf("Docs/sec %.3f%n\n", numSent.get() / transferTimeSec); } diff --git a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/RunnerTest.java b/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/RunnerTest.java deleted file mode 100644 index 3d8edfbb141..00000000000 --- a/vespa-http-client/src/test/java/com/yahoo/vespa/http/client/runner/RunnerTest.java +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.http.client.runner; - -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; - - -public class RunnerTest { - - @Test - public void testAddFeedTag() throws IOException { - InputStream stream = new ByteArrayInputStream("foo".getBytes(StandardCharsets.UTF_8)); - InputStream streamProcessed = Runner.addVespafeedTag(stream); - assertThat(convertStreamToString(streamProcessed), is("<vespafeed>foo</vespafeed>")); - } - - private static String convertStreamToString(java.io.InputStream inputStream) throws IOException { - StringBuilder builder = new StringBuilder(); - while (true) { - int character = inputStream.read(); - if (character == -1) { - inputStream.close(); - return builder.toString(); - } - builder.append((char)character); - } - } -}
\ No newline at end of file |