aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/orig-5.1.3/test/bench_llex.lua
blob: b904470ee132a43019cb670a3dbbc3497bfe4c12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
--[[--------------------------------------------------------------------

  bench_llex.lua
  Benchmark test for llex.lua
  This file is part of Yueliang.

  Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
  The COPYRIGHT file describes the conditions
  under which this software may be distributed.

  See the ChangeLog for more information.

----------------------------------------------------------------------]]

dofile("../lzio.lua")
dofile("../llex.lua")
luaX:init()

------------------------------------------------------------------------
-- load in a standard set of sample files
-- * file set is 5.0.3 front end set sans luac.lua
------------------------------------------------------------------------

local fileset, totalsize = {}, 0
for fn in string.gmatch([[
../../orig-5.0.3/lcode.lua
../../orig-5.0.3/ldump.lua
../../orig-5.0.3/llex.lua
../../orig-5.0.3/lopcodes.lua
../../orig-5.0.3/lparser.lua
../../orig-5.0.3/lzio.lua
]], "%S+") do
  fileset[#fileset+1] = fn
end

for i = 1, #fileset do
  local fn = fileset[i]
  local inf = io.open(fn, "rb")
  if not inf then
    error("failed to open "..fn.." for reading")
  end
  local data = inf:read("*a")
  local data_sz = #data
  inf:close()
  if not data or data_sz == 0 then
    error("failed to read data from "..fn.." or file is zero-length")
  end
  totalsize = totalsize + data_sz
  fileset[i] = data
end

------------------------------------------------------------------------
-- benchmark tester
------------------------------------------------------------------------

local DURATION = 5      -- how long the benchmark should run

local L = {} -- LuaState
local LS = {} -- LexState

local time = os.time
local lexedsize = 0
local tnow, elapsed = time(), 0

while time() == tnow do end    -- wait for second to click over
tnow = time()

while true do
  for i = 1, #fileset do
    ------------------------------------------------------------
    local chunk = fileset[i]
    local z = luaZ:init(luaZ:make_getS(chunk), nil)
    luaX:setinput(L, LS, z, "=string")
    while true do
      LS.t.token = luaX:llex(LS, LS.t)
      local tok, seminfo = LS.t.token, LS.t.seminfo
      if tok == "TK_EOS" then break end
    end
    ------------------------------------------------------------
    lexedsize = lexedsize + #chunk
    if time() > tnow then
      tnow = time()
      elapsed = elapsed + 1
      if elapsed >= DURATION then
        -- report performance of lexer
        lexedsize = lexedsize / 1024
        local speed = lexedsize / DURATION
        print("Lexer performance:")
        print("Size of data lexed (KB): "..string.format("%.1f", lexedsize))
        print("Speed of lexer (KB/s): "..string.format("%.1f", speed))
        -- repeat until user breaks program
        elapsed = 0
      end
    end
    ------------------------------------------------------------
  end--for
end--while

-- end of script