11package com .cowtowncoder .jsonperf .dzone .write ;
22
3- import java .io .*;
3+ import java .io .OutputStream ;
4+ import java .io .Writer ;
45import java .util .concurrent .TimeUnit ;
56
67import org .openjdk .jmh .annotations .OutputTimeUnit ;
1011import com .cowtowncoder .jsonperf .dzone .MeasurementPOJO ;
1112import com .squareup .moshi .JsonAdapter ;
1213import com .squareup .moshi .Moshi ;
14+ import okio .BufferedSink ;
15+ import okio .Okio ;
1316
1417/**
1518 * Test codec for Moshi (https://github.com/square/moshi)
16- *<p>
17- * Challenges here are mostly due to rather exotic API that Moshi exposes: it seems
18- * to aim at highly optimized I/O handling, buffering; but the end result is something
19- * that is difficult use in ways other than just buffering the whole contents in
20- * memory. Since the goal here is to minimize actual buffering overhead there does not
21- * seem to be a good match, and what we shall do then is to build a JSON String,
22- * write that in varius no-op outputs. This is not optimal, but with given API there
23- * isn't much we can do, without digging knee-deep in obscure buffer/sink classes
24- * that Moshi exposes from ok-io.
25- *<p>
26- * If anyone wants to tackle write-to-Stream / write-to-Writer cases, PRs happily accepted.
2719 */
2820@ State (Scope .Thread )
2921@ OutputTimeUnit (TimeUnit .SECONDS )
@@ -40,16 +32,15 @@ public DZoneWriteMoshi()
4032 @ Override
4133 public int _writeItems (MeasurementPOJO items , OutputStream out ) throws Exception
4234 {
43- OutputStreamWriter w = new OutputStreamWriter (out , "UTF-8" );
44- w .write (adapter .toJson (items ));
45- w .close ();
35+ BufferedSink sink = Okio .buffer (Okio .sink (out ));
36+ adapter .toJson (sink , items );
4637 return items .size ();
4738 }
4839
4940 @ Override
5041 public int _writeItems (MeasurementPOJO items , Writer out ) throws Exception
5142 {
52- out .write (adapter . toJson (items ));
43+ out .write (_writeAsString (items ));
5344 return items .size ();
5445 }
5546
0 commit comments