#define PK_IMPL_UUID #define PK_IMPL_TMR // #define PK_UUID_CLOCK CLOCK_REALTIME_ALARM #include "../pkmacros.h" #include "../pkuuid.h" #include "../pktmr.h" #include void print_uuid_v7_breakdown(FILE *f, struct pk_uuid id) { fprintf(f, "-------------------------------------------\n"); fprintf(f, "field bits value\n"); fprintf(f, "-------------------------------------------\n"); fprintf(f, "unix_ts_ms 48 0x"); fprintf(f, "%.2x", id.uuid[0]); fprintf(f, "%.2x", id.uuid[1]); fprintf(f, "%.2x", id.uuid[2]); fprintf(f, "%.2x", id.uuid[3]); fprintf(f, "%.2x", id.uuid[4]); fprintf(f, "%.2x", id.uuid[5]); fprintf(f, "\n"); fprintf(f, "ver 4 0x"); fprintf(f, "%.x", (id.uuid[6] & 0xF0) >> 4); fprintf(f, "\n"); fprintf(f, "rand_a 12 0x"); fprintf(f, "%.1x", (id.uuid[6] & 0x0F)); fprintf(f, "%.2x", id.uuid[7]); fprintf(f, "\n"); fprintf(f, "var 2 0b"); fprintf(f, "%.c", (id.uuid[8] & 0x80) ? '1' : '0'); fprintf(f, "%.c", (id.uuid[8] & 0x40) ? '1' : '0'); fprintf(f, "\n"); fprintf(f, "rand_b 62 0b"); fprintf(f, "%.c", (id.uuid[8] & 0x20) ? '1' : '0'); fprintf(f, "%.c", (id.uuid[8] & 0x10) ? '1' : '0'); fprintf(f, ", 0x"); fprintf(f, "%.x", (id.uuid[8] & 0x0F)); fprintf(f, "%.2x", id.uuid[9]); fprintf(f, "%.2x", id.uuid[10]); fprintf(f, "%.2x", id.uuid[11]); fprintf(f, "%.2x", id.uuid[12]); fprintf(f, "%.2x", id.uuid[13]); fprintf(f, "%.2x", id.uuid[14]); fprintf(f, "%.2x", id.uuid[15]); fprintf(f, "\n"); fprintf(f, "-------------------------------------------\n"); } int main(int argc, char *argv[]) { (void)argc; (void)argv; pk_uuid_init(time(NULL)); fprintf(stdout, "\n"); { struct pk_uuid id; id = pk_uuid_zed; fprintf(stdout, "[%s] zed: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); } { struct pk_uuid id; id = pk_uuid_max; fprintf(stdout, "[%s] one: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(id)); } { const int count = 4; struct pk_uuid ids[count]; for (int i = 0; i < count; ++i) { ids[i] = pk_uuid_new_v7(); } for (int i = 0; i < count; ++i) { fprintf(stdout, "[%s] new: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(ids[i])); } for (int i = 0; i < count; ++i) { fprintf(stdout, "\n"); print_uuid_v7_breakdown(stdout, ids[i]); fprintf(stdout, "[%s] new: " pk_uuid_printf_format "\n", __FILE__, pk_uuid_printf_var(ids[i])); } fprintf(stdout, "\n"); } { double ms; const int count = 1000; struct pk_uuid ids[count]; struct pk_tmr tmr; pk_tmr_start(tmr); for (int i = 0; i < count; ++i) { ids[i] = pk_uuid_new_v7(); } pk_tmr_stop(tmr); fprintf(stdout, "generated %i ids...\n", count); ms = pk_tmr_duration_dbl_mili(tmr); fprintf(stdout, "elapsed time : %f ms\n", ms); fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); } // not really a test, just checking time /* 2025-03-19 JCB - Disabled because we added a macro to define the clock * instead of passing it as a parameter. { double ms; const int count = 1000; struct pk_uuid ids[count]; struct pk_tmr tmr; pk_tmr_start(tmr); for (int i = 0; i < count; ++i) { ids[i] = pk_uuid_new_v7(CLOCK_REALTIME); } pk_tmr_stop(tmr); fprintf(stdout, "generated %i ids using CLOCK_REALTIME...\n", count); ms = pk_tmr_duration_dbl_mili(tmr); fprintf(stdout, "elapsed time : %f ms\n", ms); fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); pk_tmr_start(tmr); for (int i = 0; i < count; ++i) { ids[i] = pk_uuid_new_v7(CLOCK_REALTIME_COARSE); } pk_tmr_stop(tmr); fprintf(stdout, "generated %i ids using CLOCK_REALTIME_COARSE...\n", count); ms = pk_tmr_duration_dbl_mili(tmr); fprintf(stdout, "elapsed time : %f ms\n", ms); fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); pk_tmr_start(tmr); for (int i = 0; i < count; ++i) { ids[i] = pk_uuid_new_v7(CLOCK_TAI); } pk_tmr_stop(tmr); fprintf(stdout, "generated %i ids using CLOCK_TAI...\n", count); ms = pk_tmr_duration_dbl_mili(tmr); fprintf(stdout, "elapsed time : %f ms\n", ms); fprintf(stdout, "elapsed time (avg): %f ms\n", ms / (double)count); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[0])); fprintf(stdout, pk_uuid_printf_format "\n", pk_uuid_printf_var(ids[99])); } */ pk_uuid_teardown(); return 0; }