profile.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <time.h>
  2. #include "lsup_rdf.h"
  3. #ifndef NT
  4. #define NT 100000
  5. #endif
  6. static LSUP_Triple *
  7. generate_triples(size_t nt)
  8. {
  9. LSUP_Triple *trp;
  10. trp = malloc((nt + 1) * sizeof(LSUP_Triple));
  11. if (!trp) exit (-1);
  12. for (size_t i = 0; i < nt; i++) {
  13. char sstr[32], pstr[32], ostr[32];
  14. sprintf(sstr, "urn:s:%lu", i % (nt / 100));
  15. sprintf(pstr, "urn:p:%lu", i % (nt / 1000));
  16. sprintf(ostr, "urn:o:%lu", i);
  17. LSUP_triple_init(
  18. trp + i, LSUP_uri_new (sstr),
  19. LSUP_uri_new (pstr), LSUP_uri_new (ostr));
  20. }
  21. LSUP_triple_init (trp + nt, NULL, NULL, NULL);
  22. log_info ("Triples generated.");
  23. return trp;
  24. }
  25. int main(int argc, char *argv[])
  26. {
  27. size_t nt = (argc > 1) ? atoi (argv[1]) : NT;
  28. // Set env variable to test path.
  29. putenv ("LSUP_MDB_STORE_PATH=" TMPDIR "/lsup_profile_mdb");
  30. // Clear out database from previous test.
  31. rm_r (getenv ("LSUP_MDB_STORE_PATH"));
  32. if (LSUP_init() != LSUP_OK) abort();
  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 = (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 (LSUP_STORE_MDB);
  44. if (!gr) {
  45. log_error ("Error creating graph!");
  46. return -1;
  47. }
  48. size_t ct;
  49. rc = LSUP_graph_add_trp(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_term_free (trp[i].s);
  54. LSUP_term_free (trp[i].p);
  55. LSUP_term_free (trp[i].o);
  56. }
  57. free (trp);
  58. tc2 = clock();
  59. wallclock = (tc2 - tc1) / CLOCKS_PER_SEC;
  60. log_info ("Time elapsed: %lf s", wallclock);
  61. log_info ("Graph size: %lu", LSUP_graph_size (gr));
  62. log_info ("Lookup...");
  63. ct = 0;
  64. LSUP_Triple *spo = TRP_DUMMY;
  65. LSUP_Term *s = LSUP_uri_new ("urn:s:8");
  66. LSUP_Term *p = LSUP_uri_new ("urn:p:0");
  67. LSUP_Term *o = LSUP_uri_new ("urn:o:300");
  68. LSUP_GraphIterator *it = LSUP_graph_lookup(gr, s, NULL, NULL, &ct);
  69. log_info ("Found triples by count: %lu", ct);
  70. ct = 0;
  71. while (LSUP_graph_iter_next (it, spo) != LSUP_END)
  72. ct ++;
  73. log_info ("Found triples by iteration: %lu", ct);
  74. LSUP_graph_iter_free (it);
  75. end = clock();
  76. wallclock = (end - tc2) / CLOCKS_PER_SEC;
  77. log_info ("Time elapsed: %lf s", wallclock);
  78. wallclock = (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. }