home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / database.ex < prev    next >
Text File  |  1994-03-08  |  3KB  |  121 lines

  1.             -------------------------------
  2.             -- database update benchmark --
  3.             -------------------------------
  4. without type_check -- makes no difference
  5.  
  6. constant BATCH = 200
  7. constant BENCH_TIME = 15
  8.  
  9. -- output device
  10. constant LOG = 1
  11.  
  12. -- update codes
  13. constant NEW = 1,    -- add a new account
  14.      UPDATE = 2, -- update an account
  15.      DELETE = 3  -- delete an account
  16.  
  17. -- data fields
  18. constant NAME = 1,
  19.      AMOUNT = 2,
  20.      CODE = 3
  21.  
  22. sequence raw_data 
  23. raw_data = {
  24. {"George Bush", 1000, NEW},
  25. {"Bill Clinton", 2000, NEW},
  26. {"Brian Mulroney", 500, NEW},
  27. {"Ross Perot", 10000, NEW},
  28. {"Ross Perot", 0, DELETE},
  29. {"George Bush", -30.55, UPDATE},
  30. {"Madonna", 2500, NEW},
  31. {"Boris Yeltsin", 100, NEW},
  32. {"Michael Jackson", 50, NEW},
  33. {"Peter Mansbridge", 1200, NEW},
  34. {"Bill Clinton", +500, UPDATE},
  35. {"Rod Stewart", 3000, NEW},
  36. {"Boris Yeltsin", 0, DELETE},
  37. {"Sharon Stone", 1500, NEW},
  38. {"Clint Eastwood", 1900, NEW},
  39. {"Madonna", 0, DELETE },
  40. {"Sally Jessy Raphael", 750, NEW},
  41. {"Brian Mulroney", -400, DELETE},
  42. {"Richard Gere", 299, NEW},
  43. {"Rod Stewart", 0, DELETE},
  44. {"Demi Moore", 350, NEW},
  45. {"Bruce Willis", 480, NEW},
  46. {"Sharon Stone", +900.50, UPDATE},
  47. {"Arsenio Hall", 300, NEW},
  48. {"David Letterman", 450, NEW},
  49. {"Whoopi Goldberg", 1050, NEW},
  50. {"Clint Eastwood", +2500, UPDATE},
  51. {"Michael Jackson", -50, UPDATE},
  52. {"Clint Eastwood", 0, DELETE},
  53. {"Jack Nicholson", 3000, NEW}
  54. }
  55.  
  56. sequence database 
  57. database = {}
  58.  
  59. procedure dump()
  60. -- used to verify that the program has worked correctly
  61. -- not part of timing loop
  62.     for i = 1 to length(database) do
  63.     printf(LOG, "%20s: %8.2f\n", database[i])
  64.     end for
  65. end procedure
  66.  
  67. procedure purge()
  68. -- empty the database - free all storage
  69.     database = {}
  70. end procedure
  71.  
  72. procedure update(sequence data_stream)
  73. -- process a series of transactions
  74.     integer action, account_no
  75.     sequence pname, record
  76.  
  77.     for i = 1 to length(data_stream) do
  78.     record = data_stream[i]
  79.     action = record[CODE]
  80.     
  81.     if action = NEW then
  82.         database = append(database, record[NAME..AMOUNT])
  83.  
  84.     else 
  85.         -- look up name
  86.         pname = record[NAME]
  87.         for j = 1 to length(database) do
  88.         if compare(pname, database[j][NAME]) = 0 then
  89.             account_no = j
  90.             exit
  91.         end if
  92.         end for
  93.  
  94.         if action = UPDATE then
  95.         database[account_no][AMOUNT] = database[account_no][AMOUNT] + 
  96.                            record[AMOUNT]
  97.  
  98.         elsif action = DELETE then
  99.         database = database[1..account_no-1] & 
  100.                database[account_no+1..length(database)]
  101.         end if
  102.     end if
  103.     end for        
  104. end procedure
  105.  
  106. atom t, cycles
  107. puts(1, "database update benchmark ...\n\n")
  108. cycles = 0
  109. t = time()
  110. while time() < t + BENCH_TIME do
  111.     for i = 1 to BATCH do
  112.         purge()
  113.         update(raw_data)
  114.     end for
  115.     cycles = cycles + BATCH * length(raw_data)
  116. end while
  117. t = time() - t
  118. printf(1, "%d transactions per second\n\n", cycles / t)
  119. dump()
  120.  
  121.