profile.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <time.h>
  2. #include "lsup_rdf.h"
  3. #define NT 100000
  4. static LSUP_Triple *
  5. generate_triples(size_t nt)
  6. {
  7. LSUP_Triple *trp;
  8. trp = malloc((nt + 1) * sizeof(LSUP_Triple));
  9. if (!trp) exit (-1);
  10. for (size_t i = 0; i < nt; i++) {
  11. char sstr[32], pstr[32], ostr[32];
  12. sprintf(sstr, "urn:s:%lu", i % (nt / 100));
  13. sprintf(pstr, "urn:p:%lu", i % (nt / 1000));
  14. sprintf(ostr, "urn:o:%lu", i);
  15. LSUP_triple_init(
  16. trp + i, LSUP_iriref_new (sstr, NULL),
  17. LSUP_iriref_new (pstr, NULL), LSUP_iriref_new (ostr, NULL));
  18. }
  19. LSUP_triple_init (trp + nt, NULL, NULL, NULL);
  20. log_info ("Triples generated.");
  21. return trp;
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. size_t nt = (argc > 1) ? atoi (argv[1]) : NT;
  26. // Set env variable to test path.
  27. putenv ("LSUP_MDB_STORE_PATH=" TMPDIR "/lsup_profile_mdb");
  28. // Clear out database from previous test.
  29. rm_r (getenv ("LSUP_MDB_STORE_PATH"));
  30. if (LSUP_init() != LSUP_OK) {
  31. log_fatal ("Failed to initialize LSUP environment.");
  32. exit (-1);
  33. }
  34. int rc;
  35. clock_t start, tc1, tc2, end;
  36. double wallclock, rate;
  37. log_info ("Generating %lu triples.", nt);
  38. start = clock();
  39. LSUP_Triple *trp = generate_triples(nt);
  40. tc1 = clock();
  41. wallclock = (tc1 - start) / CLOCKS_PER_SEC;
  42. log_info ("Time elapsed: %lf s", wallclock);
  43. log_info ("Inserting triples.");
  44. LSUP_Graph *gr = LSUP_graph_new (
  45. LSUP_iriref_new (NULL, NULL), LSUP_STORE_MDB);
  46. if (!gr) {
  47. log_error ("Error creating graph!");
  48. return -1;
  49. }
  50. size_t ct;
  51. rc = LSUP_graph_add(gr, trp, &ct);
  52. if (rc != LSUP_OK) log_warn ("Graph loading interrupted: %d.", rc);
  53. else log_info ("Graph populated with %lu triples.", ct);
  54. for (size_t i = 0; i < nt; i++) {
  55. LSUP_term_free (trp[i].s);
  56. LSUP_term_free (trp[i].p);
  57. LSUP_term_free (trp[i].o);
  58. }
  59. free (trp);
  60. tc2 = clock();
  61. wallclock = (tc2 - tc1) / CLOCKS_PER_SEC;
  62. log_info ("Time elapsed: %lf s", wallclock);
  63. log_info ("Graph size: %lu", LSUP_graph_size (gr));
  64. log_info ("Lookup...");
  65. ct = 0;
  66. LSUP_Triple *spo = TRP_DUMMY;
  67. LSUP_Term *s = LSUP_iriref_new ("urn:s:8", NULL);
  68. LSUP_Term *p = LSUP_iriref_new ("urn:p:0", NULL);
  69. LSUP_Term *o = LSUP_iriref_new ("urn:o:300", NULL);
  70. LSUP_GraphIterator *it = LSUP_graph_lookup(gr, s, NULL, NULL, &ct);
  71. log_info ("Found triples by count: %lu", ct);
  72. ct = 0;
  73. while (LSUP_graph_iter_next (it, spo) != LSUP_END)
  74. ct ++;
  75. log_info ("Found triples by iteration: %lu", ct);
  76. LSUP_graph_iter_free (it);
  77. end = clock();
  78. wallclock = (end - tc2) / CLOCKS_PER_SEC;
  79. log_info ("Time elapsed: %lf s", wallclock);
  80. wallclock = (end - start) / CLOCKS_PER_SEC;
  81. rate = nt / wallclock;
  82. log_info (
  83. "%d triples created and inserted in %lf s (%lf triples/s)",
  84. nt, wallclock, rate);
  85. LSUP_term_free (s);
  86. LSUP_term_free (p);
  87. LSUP_term_free (o);
  88. LSUP_graph_free(gr);
  89. return rc;
  90. }