Skip to content

Instantly share code, notes, and snippets.

@serj-lotutovici
Created November 7, 2016 22:31
Show Gist options
  • Save serj-lotutovici/e6ebfb61f3f675036759325fecbfe7d8 to your computer and use it in GitHub Desktop.
Save serj-lotutovici/e6ebfb61f3f675036759325fecbfe7d8 to your computer and use it in GitHub Desktop.
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@BenchmarkMode({Mode.AverageTime})
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark) public class Benchmarker {
private static final String JSON;
static {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < 300; i++) {
if (i > 0) sb.append(", ");
sb.append("922337203685477580").append(i % 7);
}
sb.append("]");
JSON = sb.toString();
}
private static final Type LIST_OF_LONGS_TYPE = Types.newParameterizedType(List.class, Long.class);
private Moshi moshi = new Moshi.Builder().build();
@Benchmark public List<Long> parseLongs() throws IOException {
JsonAdapter<List<Long>> adapter = moshi.adapter(LIST_OF_LONGS_TYPE);
return adapter.fromJson(JSON);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment