home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / CFF51B.ZIP / HASHTEST.C < prev    next >
C/C++ Source or Header  |  1993-10-22  |  7KB  |  290 lines

  1. /* HASHTEST.C */
  2. /* Copyright 1990, 1991, 1992, 1993 Norman D. Culver Ft. Lauderdale, FL */
  3. /*                    All Rights Reserved                                   */
  4.  
  5. #include <stdlib.h>
  6. #include "../cff.h"
  7.  
  8. #define CLOCKS_PER_SECOND 1000000L
  9.  
  10. /* TEST CHOICES */
  11. #define PFLAGS 0
  12. #define MEMFILE 1
  13. #define EXTDMEM 1
  14. #define CFFFILE 1
  15.  
  16. extern int errno;
  17. extern long clock();
  18. extern int rand();
  19. extern long tree_testcnt;
  20. extern long tree_bincnt;
  21. OPNINFO info;
  22.  
  23. static void hash_test(void *hd, void *hd1);
  24.  
  25. void
  26. main()
  27. {
  28. void *hxxx;
  29. void *huniq;
  30. void *hcfile;
  31. void *hsxxx;
  32. void *hsxxx1;
  33. void *hcxxx;
  34.  
  35.  
  36.     cfport_settestflags(1);
  37.     cfinit("hashtest",512,NULL);
  38.     cfprintf("HASHED DIRECTORIES keyed inserts, finds and deletes.\n");
  39.  
  40.     /* HASHED DIRECTORIES CAN BE PREALLOCATED */
  41.     info.initial_entries = 10000;
  42.     info.bitmap_prealloc = 0;
  43.     info.data_prealloc = 0;
  44.     
  45. #if MEMFILE
  46.     cfprintf("MEMORY FILE\n");
  47.     hxxx = cfopen("MEMORY/XXX",F_RDWR|F_CREAT,NULL);
  48.     if(!hxxx) {
  49.         cfprintf("ERROR: create XXX err=%d\n", errno);
  50.         exit(0);
  51.     }
  52.     huniq = cfopen("MEMORY/XXX",
  53.                     F_RDWR|F_CREAT|F_TEMP|F_UNIQ|F_HUGEDIR, &info);
  54.     if(!huniq) {
  55.         cfprintf("ERROR: create UNIQUE TEMP FILE err=%d\n", errno);
  56.         exit(0);
  57.     }
  58. #if PFLAGS
  59.     cfpflags("hxxx", hxxx);
  60.     cfpflags("huniq", huniq);
  61. #endif
  62.     hash_test(huniq, NULL);
  63. cfprintf("NOW CLOSING\n");
  64.     cfclose(huniq);
  65. #endif /* MEMFILE */
  66.  
  67. #if EXTDMEM
  68.     cfprintf("EXTDMEM FILE\n");
  69.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR|F_CREAT|F_HUGEDIR, &info);
  70.     if(!hsxxx) {
  71.         cfprintf("ERROR: create EXTDMEM/XXX err=%d\n", errno);
  72.         exit(0);
  73.     }
  74.     {/* Special case file, with preallocation of local bitmap */
  75.     OPNINFO info;
  76.         info.initial_entries = 0;
  77.         info.bitmap_prealloc = 240000;
  78.         info.data_prealloc = 0;
  79.         hsxxx1 = cfopen("EXTDMEM/XXX1",
  80.                         F_RDWR|F_CREAT|F_BITMAP|F_TEMP,
  81.                         &info);
  82.         if(!hsxxx1) {
  83.             cfprintf("ERROR: create EXTDMEM/XXX1 err=%d\n", errno);
  84.             exit(0);
  85.         }
  86.     }
  87. #if PFLAGS
  88.     cfpflags("hsxxx", hsxxx);
  89.     cfpflags("hsxxx1", hsxxx1);
  90. #endif
  91.     cfsetlazy(hsxxx);
  92.     hash_test(hsxxx, hsxxx1);
  93. cfprintf("NOW CLOSING\n");
  94.     cfclose(hsxxx1);
  95.     cfclose(hsxxx);
  96.  
  97.     cfprintf("REOPEN EXTDMEM FILE\n");    
  98.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR, NULL);
  99.     if(!hsxxx) {
  100.         cfprintf("ERROR: reopen EXTDMEM:/XXX err=%d\n", errno);
  101.         exit(0);
  102.     }
  103.     cfsetlazy(hsxxx);
  104.     hash_test(hsxxx,NULL);
  105. cfprintf("NOW CLOSING\n");
  106.     cfunlink(hsxxx, NULL);
  107. #endif /* EXTDMEM */
  108.  
  109. #if CFFFILE
  110.     cfprintf("CFF DISK FILE\n");
  111.     hcfile = cfopen("testfile.cff", F_RDWR|F_CREAT, NULL);
  112.     if(!hcfile) {    
  113.         cfprintf("ERROR: create testfile.cff err=%d\n", errno);
  114.         exit(0);
  115.     }
  116.     hcxxx = cfopen("testfile.cff/XXX",
  117.                         F_RDWR|F_CREAT|F_HUGEDIR, &info);
  118.     if(!hcxxx) {    
  119.         cfprintf("ERROR: create testfile.cff/XXX err=%d\n", errno);
  120.         exit(0);
  121.     }
  122. #if 0
  123.     cfpflags("hcfile", hcfile);
  124.     cfpflags("hcxxx", hcxxx);
  125. #endif
  126.     cfsetlazy(hcxxx);
  127.     hash_test(hcxxx, NULL);
  128. cfprintf("NOW CLOSING\n");
  129.     cfclose(hcxxx);
  130.     cfclose(hcfile);
  131.  
  132.     cfprintf("REOPEN CFF DISK FILE\n");    
  133.     hcfile = cfopen("testfile.cff", F_RDWR, NULL);
  134.     if(!hcfile) {    
  135.         cfprintf("ERROR: reopen testfile.cff err=%d\n", errno);
  136.         exit(0);
  137.     }
  138.     hcxxx = cfopen("testfile.cff/XXX", F_RDWR, NULL);
  139.     if(!hcxxx) {    
  140.         cfprintf("ERROR: reopen testfile.cff/XXX err=%d\n", errno);
  141.         exit(0);
  142.     }
  143.     cfsetlazy(hcxxx);
  144.     hash_test(hcxxx,NULL);
  145. cfprintf("NOW CLOSING\n");
  146.     cfclose(hcxxx);
  147.     cfclose(hcfile);
  148.     cfunlink("testfile.cff");
  149. #endif /* CFF FILE */
  150.  
  151.     cfexit();
  152. } /* MAIN */
  153.  
  154. static Item keyval;
  155.  
  156. static void
  157. hash_test(void *hd, void *hd1)
  158. {
  159. long i, start, end, diff;
  160. long amount;
  161. long obtype = cfobtype(hd);
  162.  
  163.         if(obtype & OB_MEM)
  164.             amount = 10000;
  165.         else if(obtype & OB_CFILE)
  166.             amount = 10000;
  167.         else if(obtype & OB_SMEM)
  168.             amount = 10000;
  169.         else {
  170.             cfprintf("UNKNOWN OBTYPE: %x\n", obtype);
  171.             return;
  172.         }
  173.  
  174.         if(cfisnew(hd)) {
  175.  
  176.         int result;
  177.             start = clock();
  178.             tree_testcnt = tree_bincnt = 0;
  179.             for(i = 0; i < amount; ++i)
  180.             {
  181.                 keyval.a0 = i+2;
  182.                 if((result = cfinsert(hd, &keyval, 4, &keyval)) < OK) {
  183.                     cfprintf("CFINSERT FAILED at i=%d result=%d\n", i, result);
  184.                     break;
  185.                 }
  186.             }            
  187.             end = clock();
  188.             diff = end - start;
  189.             diff /= CLOCKS_PER_SECOND/10;
  190.             if(diff == 0) diff = 1;
  191.             cfprintf("  KEYINSERTS PER SEC = %ld\n", (i*10) / diff);
  192.         }
  193. #if 0
  194. print_entries(hd);
  195. exit(0);
  196. #endif
  197.         /* FINDITEM */
  198.         start = clock();
  199.         {
  200.         int result;
  201.             tree_testcnt = tree_bincnt = 0;
  202.             for(i = 0; i < amount; ++i)
  203.             {
  204.                 keyval.a0 = i+2;
  205.                 if((result = cffind_item(hd, &keyval, 4, &keyval)) != FOUND) {
  206.                     cfprintf("CFFINDITEM FAILED at i=%d result=%d val=%d\n",
  207.                         i, result, (int)keyval.a0);
  208.                     break;
  209.                 } else {
  210.                   if(keyval.a0 != i+2)
  211.                     cfprintf("CFFINDITEM returned wrong value=%d should be %d\n",
  212.                             (int)keyval.a0, i+2);
  213.                 }
  214.             }            
  215.             end = clock();
  216.             diff = end - start;
  217.             diff /= CLOCKS_PER_SECOND/10;
  218.             if(diff == 0) diff = 1;
  219.             cfprintf("  KEYFINDS PER SEC = %ld\n", (i*10) / diff);
  220.         }
  221.  
  222.         /* DELETE */
  223.         start = clock();
  224.         {
  225.         int result;
  226.             tree_testcnt = tree_bincnt = 0;
  227.             for(i = 0; i < amount/2; ++i)
  228.             {
  229.                 keyval.a0 = i+2;
  230.                 if((result = cfdelete_item(hd, &keyval, 4, &keyval)) != OK) {
  231.                     cfprintf("CFDELETEITEM FAILED at i=%d result=%d val=%d\n",
  232.                         i, result, (int)keyval.a0);
  233.                     break;
  234.                 }
  235.             }            
  236.             end = clock();
  237.             diff = end - start;
  238.             diff /= CLOCKS_PER_SECOND/10;
  239.             if(diff == 0) diff = 1;
  240.             cfprintf("  KEYDELETEITEMS PER SEC = %ld\n", (i*10) / diff);
  241.         }
  242.  
  243.         /* CHECK DELETE */
  244.         start = clock();
  245.         {
  246.         int result;
  247.             tree_testcnt = tree_bincnt = 0;
  248.             for(i = 0; i < amount/2; ++i)
  249.             {
  250.                 keyval.a0 = i+2;
  251.                 if((result = cffind_item(hd, &keyval, 4, &keyval)) >= FOUND){
  252.                     cfprintf("CFFINDDELETED FAILED at i=%d result=%d val=%d\n",
  253.                         i, result, (int)keyval.a0);
  254.                     break;
  255.                 }
  256.             }            
  257.             end = clock();
  258.             diff = end - start;
  259.             diff /= CLOCKS_PER_SECOND/10;
  260.             if(diff == 0) diff = 1;
  261.             cfprintf("  KEYNOFINDS PER SEC = %ld\n", (i*10) / diff);
  262.         }
  263.  
  264.         /* REINSERT */
  265.         start = clock();
  266.         {
  267.         int result;
  268.             tree_testcnt = tree_bincnt = 0;
  269.             for(i = 0; i < amount/2; ++i)
  270.             {
  271.                 keyval.a0 = i+2;
  272.  
  273.                 if((result = cfinsert(hd, &keyval, 4, &keyval)) < OK) {
  274.                     cfprintf("CFREINSERT FAILED at i=%d result=%d\n",
  275.                         i, result);
  276.                     break;
  277.                 }
  278.             }            
  279.             end = clock();
  280.             diff = end - start;
  281.             diff /= CLOCKS_PER_SECOND/10;
  282.             if(diff == 0) diff = 1;
  283.             cfprintf("  KEYREINSERTS PER SEC = %ld\n", (i*10) / diff);
  284.         }
  285.  
  286.         cfprintf("  ALLOCATED:%lu USED:%lu PER ENTRY=%lu\n",
  287.           cfbytesalloc(hd), cfbytesused(hd), cfbytesused(hd)/amount);
  288. }
  289.  
  290.