-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from primenumber-dev/main
Add logger interval option
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/java/org/embulk/output/http_json/util/Durations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.embulk.output.http_json.util; | ||
|
||
import java.time.Duration; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
public class Durations { | ||
private static final Pattern PATTERN = | ||
Pattern.compile( | ||
"\\s*(?:(?<days>\\d+)\\s*d)?\\s*(?:(?<hours>\\d+)\\s*h)?\\s*(?:(?<minutes>\\d+)\\s*m)?\\s*(?:(?<seconds>\\d+)\\s*s)?\\s*", | ||
Pattern.CASE_INSENSITIVE); | ||
|
||
public static Duration parseDuration(CharSequence text) { | ||
Matcher matcher = PATTERN.matcher(text); | ||
if (!matcher.matches()) { | ||
throw new DateTimeParseException("Invalid duration", text, 0); | ||
} | ||
String d = matcher.group("days"); | ||
String h = matcher.group("hours"); | ||
String m = matcher.group("minutes"); | ||
String s = matcher.group("seconds"); | ||
if (d == null && h == null && m == null && s == null) { | ||
throw new DateTimeParseException("Invalid duration", text, 0); | ||
} | ||
return Duration.ofDays(d == null ? 0 : Long.parseLong(d)) | ||
.plusHours(h == null ? 0 : Long.parseLong(h)) | ||
.plusMinutes(m == null ? 0 : Long.parseLong(m)) | ||
.plusSeconds(s == null ? 0 : Long.parseLong(s)); | ||
} | ||
|
||
public static String formatDuration(Duration duration) { | ||
long d = duration.toDays(); | ||
long h = duration.minusDays(d).toHours(); | ||
long m = duration.minusDays(d).minusHours(h).toMinutes(); | ||
long s = duration.minusDays(d).minusHours(h).minusMinutes(m).getSeconds(); | ||
|
||
return Stream.of( | ||
d == 0 ? null : d + "d", | ||
h == 0 ? null : h + "h", | ||
m == 0 ? null : m + "m", | ||
s == 0 ? null : s + "s") | ||
.filter(v -> v != null) | ||
.collect(Collectors.joining(" ")); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/org/embulk/output/http_json/util/ProgressLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.embulk.output.http_json.util; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import jersey.repackaged.com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class ProgressLogger { | ||
private static final long INITIAL_DELAY_SECONDS = 1; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ProgressLogger.class); | ||
|
||
private final ScheduledExecutorService service; | ||
|
||
private final AtomicLong globalRequestCount = new AtomicLong(0); | ||
private final AtomicLong globalElapsedTime = new AtomicLong(0); | ||
|
||
public ProgressLogger(Duration loggingInterval) { | ||
service = | ||
Executors.newSingleThreadScheduledExecutor( | ||
new ThreadFactoryBuilder() | ||
.setNameFormat(ProgressLogger.class.getSimpleName()) | ||
.build()); | ||
long loggingIntervalSecond = loggingInterval.getSeconds(); | ||
if (loggingIntervalSecond > 0) { | ||
setSchedule(loggingIntervalSecond); | ||
} else { | ||
logger.warn("disabled progress log."); | ||
service.shutdown(); | ||
} | ||
} | ||
|
||
private void setSchedule(long loggingInterval) { | ||
service.scheduleAtFixedRate( | ||
() -> outputProgress(), INITIAL_DELAY_SECONDS, loggingInterval, TimeUnit.SECONDS); | ||
} | ||
|
||
public void finish() { | ||
if (!service.isShutdown()) { | ||
service.submit(() -> outputProgress()); | ||
service.shutdown(); | ||
} | ||
} | ||
|
||
public void incrementRequestCount() { | ||
globalRequestCount.incrementAndGet(); | ||
} | ||
|
||
public void addElapsedTime(long elapsedTIme) { | ||
globalElapsedTime.addAndGet(elapsedTIme); | ||
} | ||
|
||
private void outputProgress() { | ||
long requestCount = globalRequestCount.get(); | ||
long elapsedTime = globalElapsedTime.get(); | ||
if (requestCount == 0) { | ||
return; | ||
} | ||
logger.info( | ||
"request count: {}, response time avg: {} ms", | ||
requestCount, | ||
elapsedTime / requestCount); | ||
} | ||
} |