diff --git a/Makefile b/Makefile index 3307a58..1619c8e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ +CFLAGS = -Wall -fPIC + all: libsfuid.so example libsfuid.so: lib/sfuid.o @@ -14,9 +16,16 @@ lib/sfuid.o: lib/sfuid.h %.o: %.c @echo Compiling $<... - @gcc -Wall -fPIC -c $< -o $@ + @gcc ${CFLAGS} -c $< -o $@ + +benchmark: CFLAGS += -DBENCHMARK +benchmark: clean example + @mv example benchmark + @rm -f lib/*.o clean: - @rm *.o - @rm lib/*.o - @rm *.so example + @rm -f *.o + @rm -f lib/*.o + @rm -f *.so + @rm -f example + @rm -f benchmark diff --git a/benchmark.sh b/benchmark.sh new file mode 100755 index 0000000..549722a --- /dev/null +++ b/benchmark.sh @@ -0,0 +1,65 @@ +#!/bin/bash + +make benchmark +if test $? != 0; then + exit 1 +fi + +export LD_LIBRARY_PATH=. + +iterations=$1 + +if test "$iterations" = ""; then + iterations=200 +fi + +resultfile=/dev/shm/$$.result +timefile=/dev/shm/$$.time + +encoding=() +decoding=() + +minMax() { + tmp=$(sort -g) + echo -n "min: " + echo "$tmp" | head -n 1 + echo -n "max: " + echo "$tmp" | tail -n 1 +} + +standardDeviation() { + awk '{ sum += $1; sumsq += $1 * $1 } END { print "mu: " sum/NR "\nsigma: " sqrt(sumsq/NR - (sum/NR)**2)}' +} + +for i in $(seq 0 $iterations); do + echo -n "." + parameter=$RANDOM + ./benchmark $2 $parameter 1> $resultfile 2> $timefile + result=$(cat $resultfile) + ns=$(cat $timefile | awk -F: '{ print $2 }' | awk '{ print $1 }') + + encoding+=($ns) + + ./benchmark $2 -d $result 1> $resultfile 2> $timefile + result=$(cat $resultfile) + ns=$(cat $timefile | awk -F: '{ print $2 }' | awk '{ print $1 }') + + decoding+=($ns) + + if test "$result" != "$parameter"; then + echo + echo "Error on input $parameter. Result: $result" + fi +done + +echo +echo +echo "Encoding: " +printf "%s\n" "${encoding[@]}" | minMax +printf "%s\n" "${encoding[@]}" | standardDeviation +echo +echo "Decoding: " +printf "%s\n" "${decoding[@]}" | minMax +printf "%s\n" "${decoding[@]}" | standardDeviation + +rm benchmark diff --git a/lib/sfuid.c b/lib/sfuid.c index 11c0dd7..ded0758 100644 --- a/lib/sfuid.c +++ b/lib/sfuid.c @@ -331,6 +331,11 @@ int sfuid_encode(uint64_t value, char* string) { if (!settingsValid) return SFUID_ERROR_SETTINGS_INVALID; + #ifdef BENCHMARK + struct timespec before, after; + clock_gettime(CLOCK_MONOTONIC, &before); + #endif + value += settings.offset; if (value > properties.max) @@ -341,10 +346,26 @@ int sfuid_encode(uint64_t value, char* string) { if (tmp != SFUID_ERROR_NONE) return tmp; + #ifdef BENCHMARK + clock_gettime(CLOCK_MONOTONIC, &after); + unsigned long long diff = (int64_t)(after.tv_sec - before.tv_sec) * (int64_t) 1000000000UL + (int64_t)(after.tv_nsec - before.tv_nsec); + fprintf(stderr, "Time for conversion: %llu ns\n", diff); + #endif + + return SFUID_ERROR_NONE; } int sfuid_decode(const char* string, uint64_t* value) { + if (!settingsValid) + return SFUID_ERROR_SETTINGS_INVALID; + + #ifdef BENCHMARK + struct timespec before, after; + clock_gettime(CLOCK_MONOTONIC, &before); + #endif + + int tmp = convertFromString(string, value); if (tmp != SFUID_ERROR_NONE) return tmp; @@ -355,6 +376,12 @@ int sfuid_decode(const char* string, uint64_t* value) { *value -= settings.offset; + #ifdef BENCHMARK + clock_gettime(CLOCK_MONOTONIC, &after); + unsigned long long diff = (int64_t)(after.tv_sec - before.tv_sec) * (int64_t) 1000000000UL + (int64_t)(after.tv_nsec - before.tv_nsec); + fprintf(stderr, "Time for conversion: %llu ns\n", diff); + #endif + return SFUID_ERROR_NONE; }