99import org .loadtest4j .drivers .wrk .utils .*;
1010import org .loadtest4j .drivers .wrk .utils .Process ;
1111
12- import java .io .IOException ;
13- import java .io .InputStream ;
14- import java .io .InputStreamReader ;
15- import java .io .Reader ;
12+ import java .io .*;
1613import java .nio .charset .StandardCharsets ;
1714import java .nio .file .Path ;
1815import java .time .Duration ;
1916import java .util .Collection ;
17+ import java .util .Collections ;
2018import java .util .List ;
2119import java .util .Map ;
2220import java .util .stream .Collectors ;
@@ -61,12 +59,17 @@ private static Path createInput(List<DriverRequest> requests) {
6159 final List <Req > wrkRequests = wrkRequests (requests );
6260 final Input input = new Input (wrkRequests );
6361 final Path inputPath = FileUtils .createTempFile ("loadtest4j-wrk" , ".json" );
64- Json .serialize (inputPath .toFile (), input );
62+ try {
63+ Json .serialize (inputPath .toFile (), input );
64+ } catch (IOException e ) {
65+ throw new LoadTesterException (e );
66+ }
6567 return inputPath ;
6668 }
6769
6870 private DriverResult runWrkViaShell (Path input ) {
6971 final Path luaScript = createLuaScript ();
72+ final Path luaOutput = FileUtils .createTempFile ("loadtest4j-output" , ".json" );
7073
7174 final List <String > arguments = new ArgumentBuilder ()
7275 .addNamedArgument ("--connections" , valueOf (connections ))
@@ -77,25 +80,38 @@ private DriverResult runWrkViaShell(Path input) {
7780 .addArgument (input .toString ())
7881 .build ();
7982
80- final Command command = new Command (arguments , executable );
83+ final Map <String , String > env = Collections .singletonMap ("WRK_OUTPUT" , luaOutput .toString ());
84+
85+ final Command command = new Command (arguments , env , executable );
8186
8287 final Process process = new Shell ().start (command );
8388
89+ // Reduce sleep/wakeup cycles waiting for process by doing the sleep ourselves
90+ try {
91+ Thread .sleep (duration .toMillis ());
92+ } catch (InterruptedException e ) {
93+ // It was worth a try - just fall back to process.waitFor()
94+ }
95+
8496 final int exitStatus = process .waitFor ();
8597
8698 if (exitStatus != 0 ) {
8799 final String error = StreamReader .streamToString (process .getStderr ());
88100 throw new LoadTesterException ("Wrk error:\n \n " + error );
89101 }
90102
91- try ( Reader reader = new InputStreamReader ( process . getStderr (), StandardCharsets . UTF_8 )) {
92- return toDriverResult (reader );
103+ try {
104+ return toDriverResult (luaOutput . toAbsolutePath () );
93105 } catch (IOException e ) {
94106 throw new LoadTesterException (e );
95107 }
96108 }
97109
98- protected static DriverResult toDriverResult (Reader report ) {
110+ private static DriverResult toDriverResult (Path path ) throws IOException {
111+ return toDriverResult (new InputStreamReader (new FileInputStream (path .toFile ()), StandardCharsets .UTF_8 ));
112+ }
113+
114+ protected static DriverResult toDriverResult (Reader report ) throws IOException {
99115 final Output output = Json .parse (report , Output .class );
100116 return toDriverResult (output );
101117 }
0 commit comments