use of java.io.BufferedInputStream in project hibernate-orm by hibernate.
the class EnhancerTestUtils method getEnhancerClassLoader.
private static ClassLoader getEnhancerClassLoader(EnhancementContext context, String packageName) {
return new ClassLoader() {
private Enhancer enhancer = Environment.getBytecodeProvider().getEnhancer(context);
@SuppressWarnings("ResultOfMethodCallIgnored")
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
if (!name.startsWith(packageName)) {
return getParent().loadClass(name);
}
Class c = findLoadedClass(name);
if (c != null) {
return c;
}
InputStream is = getResourceAsStream(name.replace('.', '/') + ".class");
if (is == null) {
throw new ClassNotFoundException(name + " not found");
}
try {
byte[] original = new byte[is.available()];
new BufferedInputStream(is).read(original);
byte[] enhanced = enhancer.enhance(name, original);
if (enhanced != null) {
File f = new File(workingDir + File.separator + name.replace(".", File.separator) + ".class");
f.getParentFile().mkdirs();
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
out.write(enhanced);
out.close();
} else {
enhanced = original;
}
return defineClass(name, enhanced, 0, enhanced.length);
} catch (Throwable t) {
throw new ClassNotFoundException(name + " not found", t);
} finally {
try {
is.close();
} catch (IOException ignore) {
}
}
}
};
}
use of java.io.BufferedInputStream in project pinot by linkedin.
the class PinotResponseTime method main.
public static void main(String[] args) throws Exception {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost post = new HttpPost("http://localhost:8099/query");
CloseableHttpResponse res;
if (STORE_RESULT) {
File dir = new File(RESULT_DIR);
if (!dir.exists()) {
dir.mkdirs();
}
}
int length;
// Make sure all segments online
System.out.println("Test if number of records is " + RECORD_NUMBER);
post.setEntity(new StringEntity("{\"pql\":\"select count(*) from tpch_lineitem\"}"));
while (true) {
System.out.print('*');
res = client.execute(post);
boolean valid;
try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent())) {
length = in.read(BUFFER);
valid = new String(BUFFER, 0, length, "UTF-8").contains("\"value\":\"" + RECORD_NUMBER + "\"");
}
res.close();
if (valid) {
break;
} else {
Thread.sleep(5000);
}
}
System.out.println("Number of Records Test Passed");
// Start Benchmark
for (int i = 0; i < QUERIES.length; i++) {
System.out.println("--------------------------------------------------------------------------------");
System.out.println("Start running query: " + QUERIES[i]);
post.setEntity(new StringEntity("{\"pql\":\"" + QUERIES[i] + "\"}"));
// Warm-up Rounds
System.out.println("Run " + WARMUP_ROUND + " times to warm up cache...");
for (int j = 0; j < WARMUP_ROUND; j++) {
res = client.execute(post);
if (!isValid(res, null)) {
System.out.println("\nInvalid Response, Sleep 20 Seconds...");
Thread.sleep(20000);
}
res.close();
System.out.print('*');
}
System.out.println();
// Test Rounds
int[] time = new int[TEST_ROUND];
int totalTime = 0;
int validIdx = 0;
System.out.println("Run " + TEST_ROUND + " times to get average time...");
while (validIdx < TEST_ROUND) {
long startTime = System.currentTimeMillis();
res = client.execute(post);
long endTime = System.currentTimeMillis();
boolean valid;
if (STORE_RESULT && validIdx == 0) {
valid = isValid(res, RESULT_DIR + File.separator + i + ".json");
} else {
valid = isValid(res, null);
}
if (!valid) {
System.out.println("\nInvalid Response, Sleep 20 Seconds...");
Thread.sleep(20000);
res.close();
continue;
}
res.close();
time[validIdx] = (int) (endTime - startTime);
totalTime += time[validIdx];
System.out.print(time[validIdx] + "ms ");
validIdx++;
}
System.out.println();
// Process Results
double avgTime = (double) totalTime / TEST_ROUND;
double stdDev = 0;
for (int temp : time) {
stdDev += (temp - avgTime) * (temp - avgTime) / TEST_ROUND;
}
stdDev = Math.sqrt(stdDev);
System.out.println("The average response time for the query is: " + avgTime + "ms");
System.out.println("The standard deviation is: " + stdDev);
}
}
}
use of java.io.BufferedInputStream in project pinot by linkedin.
the class PinotResponseTime method isValid.
private static boolean isValid(CloseableHttpResponse res, String path) throws Exception {
if (res.getStatusLine().getStatusCode() != 200) {
return false;
}
String response = "";
int length;
try (BufferedInputStream in = new BufferedInputStream(res.getEntity().getContent())) {
while ((length = in.read(BUFFER)) > 0) {
response += new String(BUFFER, 0, length, "UTF-8");
}
}
if (response.contains("\"numDocsScanned\":0")) {
return false;
}
if (path != null) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(path, false))) {
writer.write(response);
}
}
return true;
}
use of java.io.BufferedInputStream in project pinot by linkedin.
the class TarGzCompressionUtils method unTar.
/** Untar an input file into an output file.
* The output file is created in the output folder, having the same name
* as the input file, minus the '.tar' extension.
*
* @param inputFile the input .tar file
* @param outputDir the output directory file.
* @throws IOException
* @throws FileNotFoundException
*
* @return The {@link List} of {@link File}s with the untared content.
* @throws ArchiveException
*/
public static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
LOGGER.debug(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));
TarArchiveInputStream debInputStream = null;
InputStream is = null;
final List<File> untaredFiles = new LinkedList<File>();
try {
is = new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());
if (entry.isDirectory()) {
LOGGER.debug(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.exists()) {
LOGGER.debug(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
if (!outputFile.mkdirs()) {
throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
}
} else {
LOGGER.error("The directory already there. Deleting - " + outputFile.getAbsolutePath());
FileUtils.deleteDirectory(outputFile);
}
} else {
LOGGER.debug(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
File directory = outputFile.getParentFile();
if (!directory.exists()) {
directory.mkdirs();
}
OutputStream outputFileStream = null;
try {
outputFileStream = new FileOutputStream(outputFile);
IOUtils.copy(debInputStream, outputFileStream);
} finally {
IOUtils.closeQuietly(outputFileStream);
}
}
untaredFiles.add(outputFile);
}
} finally {
IOUtils.closeQuietly(debInputStream);
IOUtils.closeQuietly(is);
}
return untaredFiles;
}
use of java.io.BufferedInputStream in project pinot by linkedin.
the class FileUploadUtils method sendFile.
public static int sendFile(final String host, final String port, final String path, final String fileName, final InputStream inputStream, final long lengthInBytes, SendFileMethod httpMethod) {
EntityEnclosingMethod method = null;
try {
method = httpMethod.forUri("http://" + host + ":" + port + "/" + path);
Part[] parts = { new FilePart(fileName, new PartSource() {
@Override
public long getLength() {
return lengthInBytes;
}
@Override
public String getFileName() {
return fileName;
}
@Override
public InputStream createInputStream() throws IOException {
return new BufferedInputStream(inputStream);
}
}) };
method.setRequestEntity(new MultipartRequestEntity(parts, new HttpMethodParams()));
FILE_UPLOAD_HTTP_CLIENT.executeMethod(method);
if (method.getStatusCode() >= 400) {
String errorString = "POST Status Code: " + method.getStatusCode() + "\n";
if (method.getResponseHeader("Error") != null) {
errorString += "ServletException: " + method.getResponseHeader("Error").getValue();
}
throw new HttpException(errorString);
}
return method.getStatusCode();
} catch (Exception e) {
LOGGER.error("Caught exception while sending file: {}", fileName, e);
Utils.rethrowException(e);
throw new AssertionError("Should not reach this");
} finally {
if (method != null) {
method.releaseConnection();
}
}
}
Aggregations