From 52f663dd2e7f8cf855e301bb338fb2c1b02a049a Mon Sep 17 00:00:00 2001 From: Aliot Date: Mon, 31 Dec 2018 08:16:09 +0300 Subject: [PATCH] move profile tests to other directory --- profile/main.cpp | 5 +- profile/{ => tests}/fib.das | 47 ++-- profile/tests/lua/fibI.lua | 29 +++ profile/tests/lua/particles.lua | 32 +++ profile/tests/lua/prime.lua | 24 ++ profile/{ => tests}/native.das | 26 +- profile/tests/particles.das | 32 +++ profile/{ => tests}/primes.das | 42 ++-- .../profile_array_of_structures_vec.das | 226 +++++++++--------- profile/{ => tests}/profile_try_catch.das | 0 profile/tests/squirrel/fibI.nut | 24 ++ profile/tests/squirrel/native.nut | 9 + profile/tests/squirrel/prime.nut | 26 ++ 13 files changed, 350 insertions(+), 172 deletions(-) rename profile/{ => tests}/fib.das (64%) create mode 100644 profile/tests/lua/fibI.lua create mode 100644 profile/tests/lua/particles.lua create mode 100644 profile/tests/lua/prime.lua rename profile/{ => tests}/native.das (94%) create mode 100644 profile/tests/particles.das rename profile/{ => tests}/primes.das (94%) rename profile/{ => tests}/profile_array_of_structures_vec.das (96%) rename profile/{ => tests}/profile_try_catch.das (100%) create mode 100644 profile/tests/squirrel/fibI.nut create mode 100644 profile/tests/squirrel/native.nut create mode 100644 profile/tests/squirrel/prime.nut diff --git a/profile/main.cpp b/profile/main.cpp index bf5a0dbc6a..19ebcfbdbb 100644 --- a/profile/main.cpp +++ b/profile/main.cpp @@ -33,7 +33,7 @@ bool unit_test ( const string & fn ) { return false; } else { // cout << *program << "\n"; - Context ctx(&str); + Context ctx(&str, 64<<20); program->simulate(ctx); // vector of 10000 objects vector objects; @@ -108,7 +108,8 @@ int main(int argc, const char * argv[]) { return 0; #endif // run tests - run_tests(TEST_PATH "profile", unit_test); + if (argc == 1) + run_tests(TEST_PATH "profile/tests", unit_test); for ( int i=1; i!=argc; ++i ) { string path=argv[i]; unit_test(path); diff --git a/profile/fib.das b/profile/tests/fib.das similarity index 64% rename from profile/fib.das rename to profile/tests/fib.das index 4af956351e..3e92de3fd2 100644 --- a/profile/fib.das +++ b/profile/tests/fib.das @@ -1,23 +1,24 @@ -// options log=true - -def fibR(n:int):int - if (n < 2) - return n - return fibR(n - 1) + fibR(n - 2) - -def fibI(n:int):int - let last:int = 0 - let cur:int = 1 - for i in range(0, n-1) - let tmp:int = cur - cur = last + cur - last = tmp - return cur - -def test():bool - profile(4,"fibbonacci loop") <| - fibI(11111134) - profile(1,"fibbonacci recursive") <| - fibR(34) - return true - +// options log=true + +def fibR(n:int):int + if (n < 2) + return n + else + return fibR(n - 1) + fibR(n - 2) + +def fibI(n:int):int + let last:int = 0 + let cur:int = 1 + for i in range(0, n-1) + let tmp:int = cur + cur += last + last = tmp + return cur + +def test():bool + profile(8,"fibbonacci loop") <| + fibI(11111134) + profile(4,"fibbonacci recursive") <| + fibR(34) + return true + diff --git a/profile/tests/lua/fibI.lua b/profile/tests/lua/fibI.lua new file mode 100644 index 0000000000..d743a2cc07 --- /dev/null +++ b/profile/tests/lua/fibI.lua @@ -0,0 +1,29 @@ +function fibR(n) + + if (n < 2) then return n end + return (fibR(n-2) + fibR(n-1)) +end + + +function fibI(n) + + local last = 0 + local cur = 1 + n = n - 1 + while (n > 0) + do + n = n - 1 + local tmp = cur + cur = last + cur + last = tmp + end + return cur +end + + + +local start = os.clock() +--N = 34 --Should return 433494437 +fibI(11111134) +--print("fib: " .. fibR(N) .. " = " .. fibI(N)) +io.write(string.format("elapsed: %.8f\n", os.clock() - start)) \ No newline at end of file diff --git a/profile/tests/lua/particles.lua b/profile/tests/lua/particles.lua new file mode 100644 index 0000000000..b7ad2a894a --- /dev/null +++ b/profile/tests/lua/particles.lua @@ -0,0 +1,32 @@ +function update_particle(p) + p.pos.x=p.pos.x+p.vel.x + p.pos.y=p.pos.y+p.vel.y + p.pos.z=p.pos.z+p.vel.z +end + +function update(particles) + for i,p in ipairs(particles) do + update_particle(p) + end +end + +function update_several_times(particles, count) + for i = 0, count do + update(particles) + end +end + + +particles = {} +for i = 0, 1000000 do + table.insert(particles, + { + pos = {x = i + 0.1, y = i + 0.2, z = i + 0.3}, + vel = {x = 1.1, y = 2.1, z = 3.1} + }) +end + +--- +start = os.clock() +update_several_times(particles, 100) +print("took " .. (os.clock()-start)) \ No newline at end of file diff --git a/profile/tests/lua/prime.lua b/profile/tests/lua/prime.lua new file mode 100644 index 0000000000..3d6fa744c3 --- /dev/null +++ b/profile/tests/lua/prime.lua @@ -0,0 +1,24 @@ +function isprime(n) + for i = 2, (n - 1) do + if (n % i == 0) then + return false + end + end + return true +end + + +function primes(n) + local count = 0 + + for i = 2, n do + if (isprime(i)) then + count = count + 1 + end + end + return count +end + + +N = 50000 +print("primes: " .. primes(N)) diff --git a/profile/native.das b/profile/tests/native.das similarity index 94% rename from profile/native.das rename to profile/tests/native.das index 5ef039c725..ffba0c8f4d 100644 --- a/profile/native.das +++ b/profile/tests/native.das @@ -1,13 +1,13 @@ -require testProfile - -def testAdds():int - let count:int = 0 - for i in range(0, 100000000) - count = AddOne(count) - return count - -def test():bool - profile(1,"native loop") <| - assert(testAdds()==100000000) - return true - +require testProfile + +def testAdds():int + let count:int = 0 + for i in range(0, 100000000) + count = AddOne(count) + return count + +def test():bool + profile(1,"native loop") <| + assert(testAdds()==100000000) + return true + diff --git a/profile/tests/particles.das b/profile/tests/particles.das new file mode 100644 index 0000000000..382e855ed7 --- /dev/null +++ b/profile/tests/particles.das @@ -0,0 +1,32 @@ +require testProfile + +struct NObject + position, velocity : float3 + +def update(a:NObject) + a.position+=a.velocity + +def testSim(objects:array) + for obj in objects + update(obj) + +def testSim2(objects:array; count:int) + for i in range(0,count) + testSim(objects) + +def init(objects:array) + resize(objects, 1000000) + let index:int = 0 + for obj in objects + let ( oi:float=float(index++); ii:float=oi*2.0 ) + obj.position=float3(oi+0.1,oi+0.2,oi+0.3) + obj.velocity=float3(1.0,2.0,3.0) + assert(index==length(objects)) + +def test():bool + let objects:array + init(objects) + let(total:int=2) + profile(total,"array of nobjects") <| + testSim2(objects,100) + return true diff --git a/profile/primes.das b/profile/tests/primes.das similarity index 94% rename from profile/primes.das rename to profile/tests/primes.das index a15a7b138b..a94b6ee841 100644 --- a/profile/primes.das +++ b/profile/tests/primes.das @@ -1,21 +1,21 @@ -def isprime(n:int):bool - for i in range(2, n) - if (n % i == 0) - return false - return true - - - -def primes(n:int):int - let count:int = 0 - for i in range(2, n+1) - if (isprime(i)) - ++count - return count; - - -def test():bool - profile(2,"primes loop") <| - primes(50000) - return true - +def isprime(n:int):bool + for i in range(2, n) + if (n % i == 0) + return false + return true + + + +def primes(n:int):int + let count:int = 0 + for i in range(2, n+1) + if (isprime(i)) + ++count + return count; + + +def test():bool + profile(2,"primes loop") <| + primes(50000) + return true + diff --git a/profile/profile_array_of_structures_vec.das b/profile/tests/profile_array_of_structures_vec.das similarity index 96% rename from profile/profile_array_of_structures_vec.das rename to profile/tests/profile_array_of_structures_vec.das index d8f23161e8..a6a1503e4e 100644 --- a/profile/profile_array_of_structures_vec.das +++ b/profile/tests/profile_array_of_structures_vec.das @@ -1,113 +1,113 @@ -require testProfile - -struct NObject - position, velocity : float3 - -let - nobjects:NObject[10000] - -def update(a:NObject) - a.position+=a.velocity - -def testSim(objects:NObject[10000]) - for obj in objects - update(obj) - -def ks_update(pos:float3 &;vel:float3) - pos += vel - -def update(a:Object) - a.position+=a.velocity - -def testSim(objects:ObjectArray) - for obj in objects - update(obj) - -def testInterop(objects:ObjectArray) - for obj in objects - interopUpdate(obj) - -def initObj(index:int;a:Object) - let ( oi:float=float(index); ii:float=oi*2.0 ) - a.position=float3(oi+0.1,oi+0.2,oi+0.3) - a.velocity=float3(1.0,2.0,3.0) - -def initObj(index:int;a:NObject) - let ( oi:float=float(index); ii:float=oi*2.0 ) - a.position=float3(oi+0.1,oi+0.2,oi+0.3) - a.velocity=float3(1.0,2.0,3.0) - -def init(objects:ObjectArray) - let index:int = 0 - for obj in objects - initObj(index++,obj) - assert(index==objects.length) - -def init(objects:NObject[10000]) - let index:int = 0 - for obj in objects - initObj(index++,obj) - assert(index==10000) - -def verify(total:int;objects:ObjectArray) - // print("\n") - let index:int = 0 - let t:float=float(total) - let tt:float3 = float3(t,t,t) - for obj in objects - let ( oi:float=float(index); ii:float=oi*2.0 ) - let apos:float3=float3(oi+0.1,oi+0.2,oi+0.3) - let avel:float3=float3(1.0,2.0,3.0) - let npos:float3 = apos + avel*tt - // debug(npos,"npos=") - // debug(obj.position,"obj.position=") - assert(obj.position==npos) - index ++ - -def verify(total:int;objects:NObject[10000]) - // print("\n") - let index:int = 0 - let t:float=float(total) - let tt:float3 = float3(t,t,t) - for obj in objects - let ( oi:float=float(index); ii:float=oi*2.0 ) - let apos:float3=float3(oi+0.1,oi+0.2,oi+0.3) - let avel:float3=float3(1.0,2.0,3.0) - let npos:float3 = apos + avel*tt - // debug(npos,"npos=") - // debug(obj.position,"obj.position=") - assert(obj.position==npos) - index ++ - -def test(objects:ObjectArray):bool - let(total:int=200;simTN,simT,cT,intT,manyT,manyKsT:float) - init(nobjects) - simTN = profile(total,"native basic version") <| - testSim(nobjects) - verify(total,nobjects) - init(objects) - simT = profile(total,"basic version") <| - testSim(objects) - verify(total,objects) - init(objects) - cT = profile(total,"c++ version") <| - interopUpdateTest(objects) - verify(total,objects) - init(objects) - intT = profile(total,"interop version") <| - testInterop(objects) - verify(total,objects) - init(objects) - manyT = profile(total,"interop 10000 version") <| - update10000(objects) - verify(total,objects) - init(objects) - manyKsT = profile(total,"interop 10000-ks version") <| - update10000ks(objects) - verify(total,objects) - print("ratio nsim/c++ "+string(simTN/cT)+"\n") - print("ratio sim/c++: "+string(simT/cT)+"\n") - print("ratio interop/c++: "+string(intT/cT)+"\n") - print("ratio interop-10000/c++: "+string(manyT/cT)+"\n"); - print("ratio interop-10000-ks/c++: "+string(manyKsT/cT)+"\n") - return true +require testProfile + +struct NObject + position, velocity : float3 + +let + nobjects:NObject[10000] + +def update(a:NObject) + a.position+=a.velocity + +def testSim(objects:NObject[10000]) + for obj in objects + update(obj) + +def ks_update(pos:float3 &;vel:float3) + pos += vel + +def update(a:Object) + a.position+=a.velocity + +def testSim(objects:ObjectArray) + for obj in objects + update(obj) + +def testInterop(objects:ObjectArray) + for obj in objects + interopUpdate(obj) + +def initObj(index:int;a:Object) + let ( oi:float=float(index); ii:float=oi*2.0 ) + a.position=float3(oi+0.1,oi+0.2,oi+0.3) + a.velocity=float3(1.0,2.0,3.0) + +def initObj(index:int;a:NObject) + let ( oi:float=float(index); ii:float=oi*2.0 ) + a.position=float3(oi+0.1,oi+0.2,oi+0.3) + a.velocity=float3(1.0,2.0,3.0) + +def init(objects:ObjectArray) + let index:int = 0 + for obj in objects + initObj(index++,obj) + assert(index==objects.length) + +def init(objects:NObject[10000]) + let index:int = 0 + for obj in objects + initObj(index++,obj) + assert(index==10000) + +def verify(total:int;objects:ObjectArray) + // print("\n") + let index:int = 0 + let t:float=float(total) + let tt:float3 = float3(t,t,t) + for obj in objects + let ( oi:float=float(index); ii:float=oi*2.0 ) + let apos:float3=float3(oi+0.1,oi+0.2,oi+0.3) + let avel:float3=float3(1.0,2.0,3.0) + let npos:float3 = apos + avel*tt + // debug(npos,"npos=") + // debug(obj.position,"obj.position=") + assert(obj.position==npos) + index ++ + +def verify(total:int;objects:NObject[10000]) + // print("\n") + let index:int = 0 + let t:float=float(total) + let tt:float3 = float3(t,t,t) + for obj in objects + let ( oi:float=float(index); ii:float=oi*2.0 ) + let apos:float3=float3(oi+0.1,oi+0.2,oi+0.3) + let avel:float3=float3(1.0,2.0,3.0) + let npos:float3 = apos + avel*tt + // debug(npos,"npos=") + // debug(obj.position,"obj.position=") + assert(obj.position==npos) + index ++ + +def test(objects:ObjectArray):bool + let(total:int=200;simTN,simT,cT,intT,manyT,manyKsT:float) + init(nobjects) + simTN = profile(total,"native basic version") <| + testSim(nobjects) + verify(total,nobjects) + init(objects) + simT = profile(total,"basic version") <| + testSim(objects) + verify(total,objects) + init(objects) + cT = profile(total,"c++ version") <| + interopUpdateTest(objects) + verify(total,objects) + init(objects) + intT = profile(total,"interop version") <| + testInterop(objects) + verify(total,objects) + init(objects) + manyT = profile(total,"interop 10000 version") <| + update10000(objects) + verify(total,objects) + init(objects) + manyKsT = profile(total,"interop 10000-ks version") <| + update10000ks(objects) + verify(total,objects) + print("ratio nsim/c++ "+string(simTN/cT)+"\n") + print("ratio sim/c++: "+string(simT/cT)+"\n") + print("ratio interop/c++: "+string(intT/cT)+"\n") + print("ratio interop-10000/c++: "+string(manyT/cT)+"\n"); + print("ratio interop-10000-ks/c++: "+string(manyKsT/cT)+"\n") + return true diff --git a/profile/profile_try_catch.das b/profile/tests/profile_try_catch.das similarity index 100% rename from profile/profile_try_catch.das rename to profile/tests/profile_try_catch.das diff --git a/profile/tests/squirrel/fibI.nut b/profile/tests/squirrel/fibI.nut new file mode 100644 index 0000000000..e4587a488c --- /dev/null +++ b/profile/tests/squirrel/fibI.nut @@ -0,0 +1,24 @@ +function fibR(n) +{ + if (n < 2) return n; + return (fibR(n-2) + fibR(n-1)); +} + + +function fibI(n) +{ + local last = 0; + local cur = 1; + n = n - 1; + while(n) + { + --n; + local tmp = cur; + cur = last + cur; + last = tmp; + } + return cur; +} + +print("fib: " + fibI(11111134) + "\n"); +//print("fib: " + fibR(N) + " = " + fibI(N) + "\n"); diff --git a/profile/tests/squirrel/native.nut b/profile/tests/squirrel/native.nut new file mode 100644 index 0000000000..c92846758d --- /dev/null +++ b/profile/tests/squirrel/native.nut @@ -0,0 +1,9 @@ +const iterations = 1000000000; + + +local count = 0; +for (local i = 0; i < iterations; ++i) + count = ::AddOne(count); + + +print("Count: " + count + "\n"); diff --git a/profile/tests/squirrel/prime.nut b/profile/tests/squirrel/prime.nut new file mode 100644 index 0000000000..54b630d0ae --- /dev/null +++ b/profile/tests/squirrel/prime.nut @@ -0,0 +1,26 @@ +function isprime(n) +{ + local i; + for (i = 2; i < n; ++i) + if (n % i == 0) + return false; + return true; +} + + + +function primes(n) +{ + local count = 0; + local i; + for (i = 2; i <= n; ++i) + if (isprime(i)) + ++count; + return count; +} + + + + +local N = 50000; +print("primes: " + primes(N) + "\n");