]> git.lizzy.rs Git - lua-star.git/commitdiff
Add performance test
authorWesley <wesley.werner@gmail.com>
Sat, 27 Oct 2018 07:40:29 +0000 (09:40 +0200)
committerWesley <wesley.werner@gmail.com>
Sat, 27 Oct 2018 07:40:29 +0000 (09:40 +0200)
.gitignore
README.md
tests/performance.lua [new file with mode: 0644]

index a813bbb2546405f5d435791ecbb6f3d298fdbb7b..59cfc882702c548e5506cd0be17067070731f77e 100644 (file)
@@ -1 +1,2 @@
 example/lua-star.lua
+tests/lua-star.lua
index 6fd2ebe16d530b69f8c727d249e884a19d50dfd8..5cb49b5b67c69074871c82fcc62357aa47caeaeb 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
 # lua-star
 
-[![Build Status](https://travis-ci.org/wesleywerner/lua-star.svg?branch=master)](https://travis-ci.org/wesleywerner/lua-star) Easy A* path finding for Lua
+[![Build Status](https://travis-ci.org/wesleywerner/lua-star.svg?branch=master)](https://travis-ci.org/wesleywerner/lua-star) Lua-star is a pure Lua A* path-finding library.
 
 ![lua star example screenshot](example/lua-star-01.png)
 
@@ -60,6 +60,24 @@ Unit testing is done with busted, the `.busted` config already defines everythin
 
     busted
 
+# Performance
+
+There is a performance measurement tool in `tests/performance.lua`, it calculates the average time to find a path on a large, random map.
+
+    # copy the lib to tests
+    $ cp ../src/lua-star.lua .
+
+    # measure performance
+    $ lua performance.lua
+    Running with seed 1540584306
+    Building a map of 3000x3000...
+    Precalculating 6000 random start/goal positions...
+    Finding 1000 paths...
+        Done in 16.37 seconds.
+        That is 0.0164 seconds, or 16 milliseconds, per path.
+        The map has 9.0 million locations, with about 65% open space.
+
+
 # Example
 
 There is an [interactive example](example/main.lua) that can be run with [Love](https://love2d.org).
diff --git a/tests/performance.lua b/tests/performance.lua
new file mode 100644 (file)
index 0000000..dfdbafa
--- /dev/null
@@ -0,0 +1,63 @@
+--[[
+    Copyright 2018 Wesley Werner <wesley.werner@gmail.com>
+
+    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+]]--
+
+local luastar = require("lua-star")
+local map = { }
+local mapsize = 3000
+local numberOfTests = 1000
+local mapDensity = 0.65
+
+local seed = os.time()
+math.randomseed(seed)
+print (string.format("Running with seed %d", seed))
+
+print (string.format("Building a map of %dx%d...", mapsize, mapsize))
+for x=1, mapsize do
+    map[x] = {}
+    for y=1, mapsize do
+        map[x][y] = math.random()
+    end
+end
+
+-- precalculate a bunch of start and goal positions
+-- doubled up for each start/goal pair
+
+print (string.format("Precalculating %d random start/goal positions...", mapsize * 2))
+local testPoints = { }
+for i = 1, mapsize * 2 do
+    table.insert (testPoints, { x = math.random(1, mapsize), y = math.random(1, mapsize)})
+end
+
+print (string.format("Finding %d paths...", numberOfTests))
+function positionIsOpenFunc(x, y)
+    return map[x][y] > mapDensity
+end
+local testStart = os.clock()
+for testNumber = 1, numberOfTests do
+    luastar:find(
+        mapsize, mapsize, -- map size
+        table.remove (testPoints), -- start
+        table.remove (testPoints), -- goal
+        positionIsOpenFunc)
+end
+local testEnd = os.clock()
+local totalSec = testEnd - testStart
+local pathSec = totalSec / numberOfTests
+
+print (string.format([[
+    Done in %.2f seconds.
+    That is %.4f seconds, or %d milliseconds, per path.
+    The map has %.1f million locations, with about %d%% open space.]],
+    totalSec, -- total seconds
+    pathSec, -- seconds per path
+    pathSec*1000, -- milliseconds per path
+    (mapsize*mapsize)/1000000, -- number of locations
+    mapDensity*100 -- % open space on the map
+))