Created
September 15, 2016 09:06
-
-
Save fuweid/29f6aaab6939d55fbe245235adc3dbfe to your computer and use it in GitHub Desktop.
APUE Note
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
#! /bin/bash | |
set -euo pipefail | |
CODE=$(cat <<EOF | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#define BUFFER_SIZE BUFFER_SIZE_T | |
void | |
main(void) | |
{ | |
int n; | |
char buffer[BUFFER_SIZE]; | |
while (n = read(STDIN_FILENO, buffer, BUFFER_SIZE)) { | |
if (write(STDOUT_FILENO, buffer, n) != n) { | |
printf("failed to write\n"); | |
exit(1); | |
} | |
} | |
if (n < 0) { | |
printf("failed to read\n"); | |
exit(1); | |
} | |
exit(0); | |
} | |
EOF | |
) | |
code() { | |
local size=${1:-4096} | |
local file_name="bufsize_${size}" | |
echo "${CODE}" | sed -e "s|BUFFER_SIZE_T|${size}|g" > ${file_name}.c | |
gcc ${file_name}.c -o ${file_name} | |
echo ${file_name} | |
} | |
random_string() { | |
len=${1:-4096} | |
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c ${len} | |
} | |
export -f random_string | |
run_code() { | |
local size=${1:-4096} | |
local len=${2:-4096} | |
local binary=$(code ${size}) | |
result="$(sh -c "time random_string ${len} | ./${binary} &> /dev/null" 2>&1)" | |
echo "${result}" | |
} | |
clear_code() { | |
rm -f bufsize_* | |
} | |
trap clear_code EXIT | |
echo "BufSize Real User System" | |
TOTAL_BYTES=4096 | |
for i in $(seq 0 1 19); do | |
bufsize=$(echo "2 ^ ${i}" | bc) | |
run_code ${bufsize} ${TOTAL_BYTES} | awk ' | |
{ | |
for (i=1; i<=NF; i++) { | |
a[NR,i] = $i | |
} | |
} | |
NF>p { p = NF } | |
END { | |
for(j=1; j<=p; j++) { | |
str=a[1,j] | |
for(i=2; i<=NR; i++){ | |
str=str" "a[i,j]; | |
} | |
print str | |
} | |
}' | awk -v var=${bufsize} '{if (NR!=1) {print var,$0}}' | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment