Thread.sleep(10000);
try {
client.admin().indices().create(createIndexRequest("test")).actionGet();
StopWatch stopWatch = new StopWatch().start();
System.out.println("--> Indexing [" + COUNT + "] ...");
long ITERS = COUNT / BATCH;
long i = 1;
int counter = 0;
long[] currentTimeInMillis1 = new long[]{System.currentTimeMillis()};
long[] currentTimeInMillis2 = new long[]{System.currentTimeMillis()};
long startTimeInMillis = currentTimeInMillis1[0];
long averageMillisChange = TIME_PERIOD / COUNT * 2;
long backwardSkew = Math.max(1, (long) (averageMillisChange * 0.1));
long bigOutOfOrder = 1;
for (; i <= ITERS; i++) {
BulkRequestBuilder request = client.prepareBulk();
for (int j = 0; j < BATCH; j++) {
counter++;
XContentBuilder builder = jsonBuilder().startObject();
builder.field("id", Integer.toString(counter));
// move forward in time and sometimes a little bit back (delayed delivery)
long diff = ThreadLocalRandom.current().nextLong(2 * averageMillisChange + 2 * backwardSkew) - backwardSkew;
long[] currentTime = counter % 2 == 0 ? currentTimeInMillis1 : currentTimeInMillis2;
currentTime[0] += diff;
if (ThreadLocalRandom.current().nextLong(100) <= bigOutOfOrder) {
builder.field("l_value", currentTime[0] - 60000); // 1m delays
} else {
builder.field("l_value", currentTime[0]);
}
builder.endObject();
request.add(Requests.indexRequest("test").type("type1").id(Integer.toString(counter))
.source(builder));
}
BulkResponse response = request.execute().actionGet();
if (response.hasFailures()) {
System.err.println("--> failures...");
}
if (((i * BATCH) % 10000) == 0) {
System.out.println("--> Indexed " + (i * BATCH) + " took " + stopWatch.stop().lastTaskTime());
stopWatch.start();
}
}
System.out.println("--> Indexing took " + stopWatch.totalTime() + ", TPS " + (((double) (COUNT)) / stopWatch.totalTime().secondsFrac()));
System.out.println("Time range 1: " + (currentTimeInMillis1[0] - startTimeInMillis) / 1000.0 / 3600 + " hours");
System.out.println("Time range 2: " + (currentTimeInMillis2[0] - startTimeInMillis) / 1000.0 / 3600 + " hours");
System.out.println("--> optimizing index");
client.admin().indices().prepareOptimize().setMaxNumSegments(1).get();
} catch (IndexAlreadyExistsException e) {