profile.c 3.1 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_URN=file://" TMPDIR "/lsup_profile_mdb");
  28. if (LSUP_init() != LSUP_OK) {
  29. log_fatal ("Failed to initialize LSUP environment.");
  30. exit (-1);
  31. }
  32. LSUP_store_int (LSUP_STORE_MDB)->setup_fn (NULL, true);
  33. int rc;
  34. clock_t start, tc1, tc2, end;
  35. double wallclock, rate;
  36. log_info ("Generating %lu triples.", nt);
  37. start = clock();
  38. LSUP_Triple *trp = generate_triples(nt);
  39. tc1 = clock();
  40. wallclock = (double) (tc1 - start) / CLOCKS_PER_SEC;
  41. log_info ("Time elapsed: %lf s", wallclock);
  42. log_info ("Inserting triples.");
  43. LSUP_Graph *gr = LSUP_graph_new (
  44. LSUP_iriref_new (NULL, NULL), LSUP_STORE_MDB, NULL, NULL, nt);
  45. if (!gr) {
  46. log_error ("Error creating graph!");
  47. return -1;
  48. }
  49. size_t ct;
  50. rc = LSUP_graph_add(gr, trp, &ct);
  51. if (rc != LSUP_OK) log_warn ("Graph loading interrupted: %d.", rc);
  52. else log_info ("Graph populated with %lu triples.", ct);
  53. for (size_t i = 0; i < nt; i++) {
  54. LSUP_term_free (trp[i].s);
  55. LSUP_term_free (trp[i].p);
  56. LSUP_term_free (trp[i].o);
  57. }
  58. free (trp);
  59. tc2 = clock();
  60. wallclock = (double) (tc2 - tc1) / CLOCKS_PER_SEC;
  61. log_info ("Time elapsed: %lf s", wallclock);
  62. log_info ("Graph size: %lu", LSUP_graph_size (gr));
  63. log_info ("Lookup...");
  64. ct = 0;
  65. LSUP_Triple *spo = TRP_DUMMY;
  66. LSUP_Term *s = LSUP_iriref_new ("urn:s:8", NULL);
  67. LSUP_Term *p = LSUP_iriref_new ("urn:p:0", NULL);
  68. LSUP_Term *o = LSUP_iriref_new ("urn:o:300", NULL);
  69. LSUP_GraphIterator *it = LSUP_graph_lookup(gr, s, NULL, NULL, &ct);
  70. log_info ("Found triples by count: %lu", ct);
  71. ct = 0;
  72. while (LSUP_graph_iter_next (it, spo) != LSUP_END)
  73. ct ++;
  74. log_info ("Found triples by iteration: %lu", ct);
  75. LSUP_graph_iter_free (it);
  76. end = clock();
  77. wallclock = (double) (end - tc2) / CLOCKS_PER_SEC;
  78. log_info ("Time elapsed: %lf s", wallclock);
  79. wallclock = (double) (end - start) / CLOCKS_PER_SEC;
  80. rate = nt / wallclock;
  81. log_info (
  82. "%d triples created and inserted in %lf s (%lf triples/s)",
  83. nt, wallclock, rate);
  84. LSUP_term_free (s);
  85. LSUP_term_free (p);
  86. LSUP_term_free (o);
  87. LSUP_graph_free(gr);
  88. return rc;
  89. }