profile.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(*trp));
  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. trp[i] = LSUP_triple_new(
  16. LSUP_iriref_new (sstr, NULL),
  17. LSUP_iriref_new (pstr, NULL),
  18. LSUP_iriref_new (ostr, NULL));
  19. }
  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 (NULL, NULL, NULL);
  44. if (!gr) {
  45. log_error ("Error creating graph!");
  46. return -1;
  47. }
  48. size_t ct;
  49. rc = LSUP_graph_add(gr, trp, &ct);
  50. if (rc != LSUP_OK) log_warn ("Graph loading interrupted: %d.", rc);
  51. else log_info ("Graph populated with %lu triples.", ct);
  52. for (size_t i = 0; i < nt; i++)
  53. LSUP_triple_free (trp[i]);
  54. free (trp);
  55. tc2 = clock();
  56. wallclock = (double) (tc2 - tc1) / CLOCKS_PER_SEC;
  57. log_info ("Time elapsed: %lf s", wallclock);
  58. log_info ("Graph size: %lu", LSUP_graph_size (gr));
  59. log_info ("Lookup...");
  60. ct = 0;
  61. LSUP_Term *s = LSUP_iriref_new ("urn:s:8", NULL);
  62. LSUP_Term *p = LSUP_iriref_new ("urn:p:0", NULL);
  63. LSUP_Term *o = LSUP_iriref_new ("urn:o:300", NULL);
  64. LSUP_GraphIterator *it = LSUP_graph_lookup(gr, s, NULL, NULL, &ct);
  65. log_info ("Found triples by count: %lu", ct);
  66. LSUP_Triple *spo = NULL;
  67. ct = 0;
  68. while (LSUP_graph_iter_next (it, &spo) != LSUP_END) {
  69. ct ++;
  70. LSUP_triple_free (spo);
  71. spo = NULL;
  72. }
  73. log_info ("Found triples by iteration: %lu", ct);
  74. LSUP_graph_iter_free (it);
  75. end = clock();
  76. wallclock = (double) (end - tc2) / CLOCKS_PER_SEC;
  77. log_info ("Time elapsed: %lf s", wallclock);
  78. wallclock = (double) (end - start) / CLOCKS_PER_SEC;
  79. rate = nt / wallclock;
  80. log_info (
  81. "%d triples created and inserted in %lf s (%lf triples/s)",
  82. nt, wallclock, rate);
  83. LSUP_term_free (s);
  84. LSUP_term_free (p);
  85. LSUP_term_free (o);
  86. LSUP_graph_free(gr);
  87. return rc;
  88. }