profile.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. TRACE(STR, "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) printf ("Graph loading interrupted: %d.\n", rc);
  31. else printf ("Graph populated with %lu triples.\n", 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. int rc;
  41. clock_t start, tc1, tc2, end;
  42. double wallclock, rate;
  43. printf ("Generating triples.\n");
  44. start = clock();
  45. LSUP_Triple *trp = generate_triples();
  46. tc1 = clock();
  47. wallclock = (tc1 - start) / CLOCKS_PER_SEC;
  48. printf("Time elapsed: %lf s\n", wallclock);
  49. printf("Inserting triples.\n");
  50. LSUP_Graph *gr = LSUP_graph_new (LSUP_STORE_MDB);
  51. if (!gr) {
  52. fprintf (stderr, "Error creating graph!\n");
  53. return -1;
  54. }
  55. rc = insert_triples (gr, trp);
  56. for (size_t i = 0; i < NT; i++) {
  57. LSUP_term_free (trp[i].s);
  58. LSUP_term_free (trp[i].p);
  59. LSUP_term_free (trp[i].o);
  60. }
  61. free (trp);
  62. tc2 = clock();
  63. wallclock = (tc2 - tc1) / CLOCKS_PER_SEC;
  64. printf("Time elapsed: %lf s\n", wallclock);
  65. printf ("Graph size: %lu\n", LSUP_graph_size (gr));
  66. printf("Lookup...\n");
  67. size_t ct = 0;
  68. LSUP_Triple *spo = TRP_DUMMY;
  69. LSUP_Term *s = LSUP_uri_new ("urn:s:0");
  70. LSUP_Term *p = LSUP_uri_new ("urn:p:3200");
  71. LSUP_Term *o = LSUP_uri_new ("urn:o:3200");
  72. LSUP_GraphIterator *it = LSUP_graph_lookup(gr, NULL, NULL, NULL, NULL);
  73. while (LSUP_graph_iter_next (it, spo) != LSUP_END)
  74. ct ++;
  75. printf("Found triples per subject: %lu\n", ct);
  76. LSUP_graph_iter_free (it);
  77. end = clock();
  78. wallclock = (end - tc2) / CLOCKS_PER_SEC;
  79. printf("Time elapsed: %lf s\n", wallclock);
  80. wallclock = (end - start) / CLOCKS_PER_SEC;
  81. rate = NT / wallclock;
  82. printf(
  83. "%d triples created and inserted in %lf s (%lf triples/s)\n",
  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. }