profile.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include <time.h>
  2. #include "graph.h"
  3. #ifndef NT
  4. #define NT 1000
  5. #endif
  6. static LSUP_Triple *
  7. generate_triples()
  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. static LSUP_rc
  26. insert_triples (LSUP_Graph *gr, LSUP_Triple *trp)
  27. {
  28. size_t ct;
  29. LSUP_rc rc = LSUP_graph_add_trp(gr, trp, &ct);
  30. if (rc != LSUP_OK) log_warn ("Graph loading interrupted: %d.", rc);
  31. else log_info ("Graph populated with %lu triples.", ct);
  32. return rc;
  33. }
  34. int main()
  35. {
  36. // Set env variable to test path.
  37. putenv ("LSUP_MDB_STORE_PATH=" TMPDIR "/lsup_profile_mdb");
  38. // Clear out database from previous test.
  39. rm_r (getenv ("LSUP_MDB_STORE_PATH"));
  40. LSUP_init();
  41. int rc;
  42. clock_t start, tc1, tc2, end;
  43. double wallclock, rate;
  44. log_info ("Generating triples.");
  45. start = clock();
  46. LSUP_Triple *trp = generate_triples();
  47. tc1 = clock();
  48. wallclock = (tc1 - start) / CLOCKS_PER_SEC;
  49. log_info ("Time elapsed: %lf s", wallclock);
  50. log_info ("Inserting triples.");
  51. LSUP_Graph *gr = LSUP_graph_new (LSUP_STORE_MDB);
  52. if (!gr) {
  53. log_error ("Error creating graph!");
  54. return -1;
  55. }
  56. rc = insert_triples (gr, trp);
  57. for (size_t i = 0; i < NT; i++) {
  58. LSUP_term_free (trp[i].s);
  59. LSUP_term_free (trp[i].p);
  60. LSUP_term_free (trp[i].o);
  61. }
  62. free (trp);
  63. tc2 = clock();
  64. wallclock = (tc2 - tc1) / CLOCKS_PER_SEC;
  65. log_info ("Time elapsed: %lf s", wallclock);
  66. log_info ("Graph size: %lu", LSUP_graph_size (gr));
  67. log_info ("Lookup...");
  68. size_t ct = 0;
  69. LSUP_Triple *spo = TRP_DUMMY;
  70. LSUP_Term *s = LSUP_uri_new ("urn:s:0");
  71. LSUP_Term *p = LSUP_uri_new ("urn:p:3200");
  72. LSUP_Term *o = LSUP_uri_new ("urn:o:3200");
  73. LSUP_GraphIterator *it = LSUP_graph_lookup(gr, NULL, NULL, NULL, NULL);
  74. while (LSUP_graph_iter_next (it, spo) != LSUP_END)
  75. ct ++;
  76. log_info ("Found triples per subject: %lu", ct);
  77. LSUP_graph_iter_free (it);
  78. end = clock();
  79. wallclock = (end - tc2) / CLOCKS_PER_SEC;
  80. log_info ("Time elapsed: %lf s", wallclock);
  81. wallclock = (end - start) / CLOCKS_PER_SEC;
  82. rate = NT / wallclock;
  83. log_info (
  84. "%d triples created and inserted in %lf s (%lf triples/s)",
  85. NT, wallclock, rate);
  86. LSUP_term_free (s);
  87. LSUP_term_free (p);
  88. LSUP_term_free (o);
  89. LSUP_graph_free(gr);
  90. return rc;
  91. }