Skip to content

Commit

Permalink
Simple but significant performance improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
brettwooldridge committed Jun 1, 2017
1 parent df18f64 commit 81c7223
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
35 changes: 18 additions & 17 deletions src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ public class Point {
.addEscape(',', "\\,")
.addEscape('=', "\\=")
.build();

private static final int MAX_FRACTION_DIGITS = 340;
private static final ThreadLocal<NumberFormat> NUMBER_FORMATTER =
ThreadLocal.withInitial(() -> {
NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
numberFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
numberFormat.setGroupingUsed(false);
numberFormat.setMinimumFractionDigits(1);
return numberFormat;
});

Point() {
}
Expand Down Expand Up @@ -322,33 +331,29 @@ public String toString() {
public String lineProtocol() {
final StringBuilder sb = new StringBuilder();
sb.append(KEY_ESCAPER.escape(this.measurement));
sb.append(concatenatedTags());
sb.append(concatenateFields());
sb.append(formatedTime());

concatenatedTags(sb);
concatenatedFields(sb);
formatedTime(sb);

return sb.toString();
}

private StringBuilder concatenatedTags() {
final StringBuilder sb = new StringBuilder();
private void concatenatedTags(final StringBuilder sb) {
for (Entry<String, String> tag : this.tags.entrySet()) {
sb.append(",")
.append(KEY_ESCAPER.escape(tag.getKey()))
.append("=")
.append(KEY_ESCAPER.escape(tag.getValue()));
}
sb.append(" ");
return sb;
}

private StringBuilder concatenateFields() {
final StringBuilder sb = new StringBuilder();
private void concatenatedFields(final StringBuilder sb) {
final int fieldCount = this.fields.size();
int loops = 0;

NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH);
numberFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS);
numberFormat.setGroupingUsed(false);
numberFormat.setMinimumFractionDigits(1);
NumberFormat numberFormat = NUMBER_FORMATTER.get();

for (Entry<String, Object> field : this.fields.entrySet()) {
loops++;
Expand All @@ -375,14 +380,10 @@ private StringBuilder concatenateFields() {
sb.append(",");
}
}

return sb;
}

private StringBuilder formatedTime() {
final StringBuilder sb = new StringBuilder();
private void formatedTime(final StringBuilder sb) {
sb.append(" ").append(TimeUnit.NANOSECONDS.convert(this.time, this.precision));
return sb;
}

}
22 changes: 11 additions & 11 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

Expand All @@ -67,9 +67,9 @@ public class InfluxDBImpl implements InfluxDB {
private final InfluxDBService influxDBService;
private BatchProcessor batchProcessor;
private final AtomicBoolean batchEnabled = new AtomicBoolean(false);
private final AtomicLong writeCount = new AtomicLong();
private final AtomicLong unBatchedCount = new AtomicLong();
private final AtomicLong batchedCount = new AtomicLong();
private final LongAdder writeCount = new LongAdder();
private final LongAdder unBatchedCount = new LongAdder();
private final LongAdder batchedCount = new LongAdder();
private volatile DatagramSocket datagramSocket;
private final HttpLoggingInterceptor loggingInterceptor;
private final GzipRequestInterceptor gzipRequestInterceptor;
Expand Down Expand Up @@ -190,8 +190,8 @@ public void disableBatch() {
this.batchProcessor.flushAndShutdown();
if (this.logLevel != LogLevel.NONE) {
System.out.println(
"total writes:" + this.writeCount.get()
+ " unbatched:" + this.unBatchedCount.get()
"total writes:" + this.writeCount
+ " unbatched:" + this.unBatchedCount
+ " batchPoints:" + this.batchedCount);
}
}
Expand Down Expand Up @@ -240,9 +240,9 @@ public void write(final String database, final String retentionPolicy, final Poi
.retentionPolicy(retentionPolicy).build();
batchPoints.point(point);
this.write(batchPoints);
this.unBatchedCount.incrementAndGet();
this.unBatchedCount.increment();
}
this.writeCount.incrementAndGet();
this.writeCount.increment();
}

/**
Expand All @@ -255,14 +255,14 @@ public void write(final int udpPort, final Point point) {
this.batchProcessor.put(batchEntry);
} else {
this.write(udpPort, point.lineProtocol());
this.unBatchedCount.incrementAndGet();
this.unBatchedCount.increment();
}
this.writeCount.incrementAndGet();
this.writeCount.increment();
}

@Override
public void write(final BatchPoints batchPoints) {
this.batchedCount.addAndGet(batchPoints.getPoints().size());
this.batchedCount.add(batchPoints.getPoints().size());
RequestBody lineProtocol = RequestBody.create(MEDIA_TYPE_STRING, batchPoints.lineProtocol());
execute(this.influxDBService.writePoints(
this.username,
Expand Down

0 comments on commit 81c7223

Please sign in to comment.