use of java.util.zip.DeflaterOutputStream in project bazel by bazelbuild.
the class Profiler method start.
/**
* Enable profiling.
*
* <p>Subsequent calls to beginTask/endTask will be recorded
* in the provided output stream. Please note that stream performance is
* extremely important and buffered streams should be utilized.
*
* @param profiledTaskKinds which kinds of {@link ProfilerTask}s to track
* @param stream output stream to store profile data. Note: passing unbuffered stream object
* reference may result in significant performance penalties
* @param comment a comment to insert in the profile data
* @param recordAllDurations iff true, record all tasks regardless of their duration; otherwise
* some tasks may get aggregated if they finished quick enough
* @param clock a {@code BlazeClock.instance()}
* @param execStartTimeNanos execution start time in nanos obtained from {@code clock.nanoTime()}
*/
public synchronized void start(ProfiledTaskKinds profiledTaskKinds, OutputStream stream, String comment, boolean recordAllDurations, Clock clock, long execStartTimeNanos) throws IOException {
Preconditions.checkState(!isActive(), "Profiler already active");
taskStack = new TaskStack();
taskQueue = new ConcurrentLinkedQueue<>();
describer = new ObjectDescriber();
this.profiledTaskKinds = profiledTaskKinds;
this.clock = clock;
// sanity check for current limitation on the number of supported types due
// to using enum.ordinal() to store them instead of EnumSet for performance reasons.
Preconditions.checkState(TASK_COUNT < 256, "The profiler implementation supports only up to 255 different ProfilerTask values.");
// reset state for the new profiling session
taskId.set(0);
this.recordAllDurations = recordAllDurations;
this.saveException = null;
if (stream != null) {
this.timer = new Timer("ProfilerTimer", true);
// Wrapping deflater stream in the buffered stream proved to reduce CPU consumption caused by
// the save() method. Values for buffer sizes were chosen by running small amount of tests
// and identifying point of diminishing returns - but I have not really tried to optimize
// them.
this.out = new DataOutputStream(new BufferedOutputStream(new DeflaterOutputStream(stream, new Deflater(Deflater.BEST_SPEED, false), 65536), 262144));
// magic
this.out.writeInt(MAGIC);
// protocol_version
this.out.writeInt(VERSION);
this.out.writeUTF(comment);
// ProfileTask.values() method sorts enums using their ordinal() value, so
// there there is no need to store ordinal() value for each entry.
this.out.writeInt(TASK_COUNT);
for (ProfilerTask type : ProfilerTask.values()) {
this.out.writeUTF(type.toString());
}
// Start save thread
timer.schedule(new TimerTask() {
@Override
public void run() {
save();
}
}, SAVE_DELAY, SAVE_DELAY);
} else {
this.out = null;
}
// activate profiler
profileStartTime = execStartTimeNanos;
}
use of java.util.zip.DeflaterOutputStream in project dropwizard by dropwizard.
the class BiDiGzipHandlerTest method testDecompressDeflateRequestGzipIncompatible.
@Test
public void testDecompressDeflateRequestGzipIncompatible() throws Exception {
request.setMethod("POST");
request.setURI("/banner");
request.setHeader(HttpHeaders.CONTENT_ENCODING, "deflate");
request.setHeader(HttpHeaders.CONTENT_TYPE, PLAIN_TEXT_UTF_8);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (DeflaterOutputStream deflate = new DeflaterOutputStream(baos)) {
Resources.copy(Resources.getResource("assets/new-banner.txt"), deflate);
}
request.setContent(baos.toByteArray());
gzipHandler.setInflateNoWrap(false);
HttpTester.Response response = HttpTester.parseResponse(servletTester.getResponses(request.generate()));
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContent()).isEqualTo("Banner has been updated");
}
use of java.util.zip.DeflaterOutputStream in project bazel by bazelbuild.
the class ProfilerTest method testUnsupportedProfilerRecord.
@Test
public void testUnsupportedProfilerRecord() throws Exception {
Path dataFile = cacheDir.getRelative("profile5.dat");
profiler.start(ProfiledTaskKinds.ALL, dataFile.getOutputStream(), "phase test", false, BlazeClock.instance(), BlazeClock.instance().nanoTime());
profiler.startTask(ProfilerTask.TEST, "outer task");
profiler.logEvent(ProfilerTask.EXCEPTION, "inner task");
profiler.completeTask(ProfilerTask.TEST);
profiler.startTask(ProfilerTask.SCANNER, "outer task 2");
profiler.logSimpleTask(Profiler.nanoTimeMaybe(), ProfilerTask.TEST, "inner task 2");
profiler.completeTask(ProfilerTask.SCANNER);
profiler.stop();
// Validate our test profile.
ProfileInfo info = ProfileInfo.loadProfile(dataFile);
info.calculateStats();
assertFalse(info.isCorruptedOrIncomplete());
assertEquals(2, info.getStatsForType(ProfilerTask.TEST, info.rootTasksById).count);
assertEquals(0, info.getStatsForType(ProfilerTask.UNKNOWN, info.rootTasksById).count);
// Now replace "TEST" type with something unsupported - e.g. "XXXX".
InputStream in = new InflaterInputStream(dataFile.getInputStream(), new Inflater(false), 65536);
byte[] buffer = new byte[60000];
int len = in.read(buffer);
in.close();
// Validate that file was completely decoded.
assertTrue(len < buffer.length);
String content = new String(buffer, ISO_8859_1);
int infoIndex = content.indexOf("TEST");
assertTrue(infoIndex > 0);
content = content.substring(0, infoIndex) + "XXXX" + content.substring(infoIndex + 4);
OutputStream out = new DeflaterOutputStream(dataFile.getOutputStream(), new Deflater(Deflater.BEST_SPEED, false), 65536);
out.write(content.getBytes(ISO_8859_1));
out.close();
// Validate that XXXX records were classified as UNKNOWN.
info = ProfileInfo.loadProfile(dataFile);
info.calculateStats();
assertFalse(info.isCorruptedOrIncomplete());
assertEquals(0, info.getStatsForType(ProfilerTask.TEST, info.rootTasksById).count);
assertEquals(1, info.getStatsForType(ProfilerTask.SCANNER, info.rootTasksById).count);
assertEquals(1, info.getStatsForType(ProfilerTask.EXCEPTION, info.rootTasksById).count);
assertEquals(2, info.getStatsForType(ProfilerTask.UNKNOWN, info.rootTasksById).count);
}
use of java.util.zip.DeflaterOutputStream in project dubbo by alibaba.
the class BenchmarkRunner method compressDeflate.
private static byte[] compressDeflate(byte[] data) {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
compresser.write(data, 0, data.length);
compresser.finish();
compresser.flush();
return bout.toByteArray();
} catch (IOException ex) {
AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
ae.initCause(ex);
throw ae;
}
}
use of java.util.zip.DeflaterOutputStream in project jodd by oblac.
the class ZipUtil method zlib.
/**
* Compresses a file into zlib archive.
*/
public static File zlib(File file) throws IOException {
if (file.isDirectory()) {
throw new IOException("Can't zlib folder");
}
FileInputStream fis = new FileInputStream(file);
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
String zlibFileName = file.getAbsolutePath() + ZLIB_EXT;
DeflaterOutputStream dos = new DeflaterOutputStream(new FileOutputStream(zlibFileName), deflater);
try {
StreamUtil.copy(fis, dos);
} finally {
StreamUtil.close(dos);
StreamUtil.close(fis);
}
return new File(zlibFileName);
}
Aggregations