-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax FILE_ERROR handling. Benchmark example
- Loading branch information
Showing
6 changed files
with
250 additions
and
24 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python2 | ||
|
||
''' | ||
Based on - https://www.unixuser.org/~euske/doc/cdbinternals/pycdb.py.html | ||
''' | ||
|
||
# calc hash value with a given key | ||
def calc_hash(s): | ||
return reduce(lambda h, c: (((h << 5) + h) ^ ord(c)) & 0xffffffffL, s, 5381) | ||
|
||
# cdbmake(filename, hash) | ||
def cdbmake(f, a): | ||
from struct import pack | ||
|
||
# write cdb | ||
def write_cdb(fp): | ||
pos_header = fp.tell() | ||
|
||
# skip header | ||
p = pos_header + 8 * 256 | ||
fp.seek(p) | ||
|
||
bucket = [[] for i in range(256)] | ||
|
||
# write data & make hash | ||
for (k, v) in a.iteritems(): | ||
fp.write(pack('<LL', len(k), len(v))) | ||
fp.write(k) | ||
fp.write(v) | ||
h = calc_hash(k) | ||
bucket[h % 256].append((h, p)) | ||
p += len(k) + len(v) + 8 | ||
|
||
pos_hash = p | ||
|
||
# write hash tables | ||
for bt in bucket: | ||
if bt: | ||
nslots = 2 * len(bt) | ||
hash_tab = [(0, 0) for i in range(nslots)] | ||
for (h, p) in bt: | ||
i = (h >> 8) % nslots | ||
while hash_tab[i][1]: # is slot already occupied? | ||
i = (i + 1) % nslots | ||
hash_tab[i] = (h, p) | ||
for (h, p) in hash_tab: | ||
fp.write(pack('<LL', h, p)) | ||
|
||
# write header | ||
fp.seek(pos_header) | ||
for bt in bucket: | ||
fp.write(pack('<LL', pos_hash, 2 * len(bt))) | ||
pos_hash += 16 * len(bt) | ||
return | ||
|
||
fp = file(f, "wb") | ||
write_cdb(fp) | ||
fp.close() | ||
return | ||
|
||
if __name__ == "__main__": | ||
|
||
bench = {} | ||
for i in xrange(0, 5*1000000, 5): | ||
bench[str(i)] = str(i) | ||
print bench['255'] | ||
cdbmake("bench.cdb", bench) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
uCDB benchmark sketch | ||
Write file bench.cdb ([0..5000000] divisible by 5) on SD card. Connect to Ardino board. | ||
Compile and upload sketch. Open Serial Monitor. | ||
Board: Arduino Uno | ||
SD card: SDHC 7.31Gb FAT32, sectorsPerCluster - 64 | ||
SD chip select pin: 10 | ||
Arduino IDE Serial Monitors settings: 9600 baud, no line ending. | ||
Created by Ioulianos Kakoulidis, 2021. | ||
Released into the public domain. | ||
*/ | ||
|
||
#include "uCDB.h" | ||
|
||
void setup() { | ||
Serial.begin(9600); | ||
while (!Serial) { | ||
; | ||
} | ||
|
||
if (!SD.begin(10)) { | ||
Serial.println("SD card initialization failed!"); | ||
while (true) { | ||
; | ||
} | ||
} | ||
} | ||
|
||
void loop() { | ||
uCDB ucdb; | ||
char str[16]; | ||
long key; | ||
unsigned long startMillis; | ||
cdbResult rt; | ||
|
||
Serial.println("Press any key to start test"); | ||
while (!Serial.available()) { | ||
; | ||
} | ||
|
||
if (ucdb.open("bench.cdb") != CDB_OK) { | ||
Serial.print("Invalid CDB: "); | ||
Serial.println("bench.cdb"); | ||
return; | ||
} | ||
|
||
Serial.println("Querying 1000 random keys from interval [0..5000000)..."); | ||
startMillis = millis(); | ||
for (int i = 0; i < 1000; ++i) { | ||
key = random(5000000); | ||
sprintf(str, "%ld", key); | ||
rt = ucdb.findKey(str, strlen(str)); | ||
if (key%5) { | ||
if (rt != KEY_NOT_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(key); | ||
break; | ||
} | ||
} | ||
else { | ||
if (rt != KEY_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(key); | ||
break; | ||
} | ||
} | ||
} | ||
Serial.print("Query millis: "); | ||
Serial.println(millis() - startMillis); | ||
|
||
Serial.println("Querying 1000 random keys from interval [-5000000,0)..."); | ||
startMillis = millis(); | ||
for (int i = 0; i < 1000; ++i) { | ||
key = random(-5000000, 0); | ||
sprintf(str, "%ld", key); | ||
rt = ucdb.findKey(str, strlen(str)); | ||
if (rt != KEY_NOT_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(str); | ||
break; | ||
} | ||
} | ||
Serial.print("Query millis: "); | ||
Serial.println(millis() - startMillis); | ||
|
||
Serial.println("Querying 1000 random keys with findNextValue() from interval [0..5000000)..."); | ||
startMillis = millis(); | ||
for (int i = 0; i < 1000; ++i) { | ||
key = random(5000000); | ||
sprintf(str, "%ld", key); | ||
rt = ucdb.findKey(str, strlen(str)); | ||
if (key%5) { | ||
if (rt != KEY_NOT_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(key); | ||
break; | ||
} | ||
} | ||
else { | ||
if (rt != KEY_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(key); | ||
break; | ||
} | ||
} | ||
rt = ucdb.findNextValue(); | ||
if (rt != KEY_NOT_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(key); | ||
break; | ||
} | ||
} | ||
Serial.print("Query millis: "); | ||
Serial.println(millis() - startMillis); | ||
|
||
Serial.println("Querying 1000 random keys with findNextValue() from interval [-5000000,0)..."); | ||
startMillis = millis(); | ||
for (int i = 0; i < 1000; ++i) { | ||
key = random(-5000000, 0); | ||
sprintf(str, "%ld", key); | ||
rt = ucdb.findKey(str, strlen(str)); | ||
if (rt != KEY_NOT_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(str); | ||
break; | ||
} | ||
rt = ucdb.findNextValue(); | ||
if (rt != KEY_NOT_FOUND) { | ||
Serial.print("Error: "); | ||
Serial.println(key); | ||
break; | ||
} | ||
} | ||
Serial.print("Query millis: "); | ||
Serial.println(millis() - startMillis); | ||
|
||
|
||
ucdb.close(); | ||
while (Serial.available()) { | ||
Serial.read(); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name=uCDB | ||
version=0.4.1 | ||
version=0.4.2 | ||
author=Ioulianos Kakoulidis | ||
maintainer=Ioulianos Kakoulidis <[email protected]> | ||
sentence=API for querying Constant DataBase file store. | ||
|
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
Oops, something went wrong.