mirror of
https://github.com/sigmasternchen/libsfuid
synced 2025-03-15 07:59:02 +00:00
added benchmark
This commit is contained in:
parent
0e511a94c3
commit
113383066d
3 changed files with 105 additions and 4 deletions
17
Makefile
17
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
|
||||
|
|
65
benchmark.sh
Executable file
65
benchmark.sh
Executable file
|
@ -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
|
27
lib/sfuid.c
27
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue