Skip to content

Instantly share code, notes, and snippets.

@henryr
Created March 5, 2012 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henryr/1977470 to your computer and use it in GitHub Desktop.
Save henryr/1977470 to your computer and use it in GitHub Desktop.
HDFS-2834 test code
#include <time.h>
long get_time() {
struct timespec tp;
clock_gettime(CLOCK_MONOTONIC, &tp);
return (long)((tp.tv_sec * 1000000000) + tp.tv_nsec);
}
#include "../hadoop-common/hadoop-hdfs-project/hadoop-hdfs/target/native/hdfs.h"
#define SIZE 512*1024*1024
#define READ_SIZE 512*1024*1024
#define DISCARD_COUNT 5
int main(int argc, char** argv) {
hdfsFS fs = hdfsConnect("localhost", 20500);
/* printf("File is null: %d\n", file == NULL ? 1 : 0); */
char *buf = malloc(sizeof(unsigned char) * SIZE);
printf("Read size: %d\n", READ_SIZE);
int iterations = 5;
if (argc > 2) {
iterations = atoi(argv[2]);
}
if (iterations <= DISCARD_COUNT) {
printf("Iterations should be at least %d\n", DISCARD_COUNT + 1);
exit(0);
}
printf("Running %d iterations\n", iterations);
float time_total;
float max = 0.f;
float min = 999999999999999.f;
int do_direct = 0;
if (argc > 1 && strcmp(argv[1], "direct") == 0) {
do_direct = 1;
}
printf(do_direct ? "Using direct read\n" : "Using copying read\n");
int i;
for (i=0; i<iterations; ++i) {
long start = get_time();
hdfsFile file = hdfsOpenFile(fs, "/henry/2048_mb", O_RDONLY, 0, 0, 0);
int n = 0;
while (n < SIZE) {
int nread;
if (do_direct) {
nread = hdfsReadDirect(fs, file, buf + n, READ_SIZE);
} else {
nread = hdfsRead(fs, file, buf + n, READ_SIZE);
}
if (nread == -1) {
printf("EOF before finished, read %d bytes\n", n);
hdfsDisconnect(fs);
return 0;
}
n += nread;
/* printf("Read %d kilobytes\n", nread / 1024); */
}
long end = get_time();
printf("Read %d bytes, hoping for %d.\n", n, SIZE);
long elapsed = (end - start);
// printf("Start: %lu, end: %lu\n", start, end);
float time = elapsed / (1000000000.0f);
printf ("Took %2.6fs\n", time);
printf("Throughput: %2.2fMB/s\n", SIZE * 1.0f / (1024 * 1024 * time));
if (i >= DISCARD_COUNT) {
time_total += time;
if (time < min) {
min = time;
}
if (time > max) {
max = time;
}
}
}
hdfsDisconnect(fs);
printf("------\n");
printf("Average time: %2.2fs\n", time_total / (iterations - DISCARD_COUNT));
printf("Max. time: %2.2f, min. time: %2.2f\n", max, min);
float maxt = SIZE * 1.f / (1024 * 1024 * max);
float mint = SIZE * 1.f / (1024 * 1024 * min);
printf("Average throughput: %2.2fMB/s\n", 1.f * SIZE * (iterations - DISCARD_COUNT) / (1024 * 1024 * time_total));
printf("Max. throughput: %2.2f, min. throughput: %2.2f\n", maxt, mint);
// printf("File contents: %d\n", buf[0]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment