blueloveTH 3 سال پیش
والد
کامیت
ce6dba5240
5فایلهای تغییر یافته به همراه28 افزوده شده و 35 حذف شده
  1. 2 2
      .github/workflows/main.yml
  2. 0 5
      benchmarks/primes.py
  3. 1 5
      benchmarks/simple.py
  4. 2 7
      benchmarks/sort.py
  5. 23 16
      scripts/run_tests.py

+ 2 - 2
.github/workflows/main.yml

@@ -13,7 +13,7 @@ jobs:
         mv src/pocketpy.h src/pocketpy.cpp
         CL -std:c++17 -GR- -EHsc -O2 -LD -Fe:pocketpy src/pocketpy.cpp
         python3 scripts/run_tests.py
-        python3 scripts/run_tests.py benchmarks/
+        python3 scripts/run_tests.py benchmark
         mkdir -p output/windows/x86_64
         mv pocketpy.exe output/windows/x86_64
         mv pocketpy.dll output/windows/x86_64
@@ -54,7 +54,7 @@ jobs:
         bash build_cpp.sh
         bash build_linux.sh
         python3 scripts/run_tests.py
-        python3 scripts/run_tests.py benchmarks/
+        python3 scripts/run_tests.py benchmark
         mkdir -p output/linux/x86_64
         mv pocketpy output/linux/x86_64
         mv pocketpy.so output/linux/x86_64

+ 0 - 5
benchmarks/primes.py

@@ -1,6 +1,3 @@
-import time
-_0 = time.time()
-
 UPPER_BOUND = 5000000
 PREFIX = 32338
 
@@ -112,5 +109,3 @@ def verify():
 verify()
 results = find(UPPER_BOUND, PREFIX)
 assert results == [323381, 323383, 3233803, 3233809, 3233851, 3233863, 3233873, 3233887, 3233897]
-
-print(round(time.time()-_0, 6), 's')

+ 1 - 5
benchmarks/simple.py

@@ -1,6 +1,3 @@
-import time
-_0 = time.time()
-
 def is_prime(x):
   if x<2:
     return False
@@ -20,5 +17,4 @@ def test(n):
 # dis(test)
 # dis(is_prime)
 
-print(test(10000))
-print(round(time.time()-_0, 6), 's')
+print(test(10000))

+ 2 - 7
benchmarks/sort.py

@@ -1,9 +1,4 @@
-import time
-_0 = time.time()
-
 import random
 
-a = [random.randint(-100000, 100000) for i in range(1500)]
-a = sorted(a)
-
-print(round(time.time()-_0, 6), 's')
+a = [random.randint(-100000, 100000) for i in range(100000)]
+a = sorted(a)

+ 23 - 16
scripts/run_tests.py

@@ -1,31 +1,38 @@
 import os
 import sys
+import time
 
-def test_file(filepath):
+def test_file(filepath, cpython=False):
+    if cpython:
+        return os.system("python3 " + filepath) == 0
     if sys.platform == 'win32':
         return os.system("pocketpy.exe " + filepath) == 0
     else:
         return os.system("./pocketpy " + filepath) == 0
 
 def test_dir(path):
-    has_error = False
+    print("Testing directory:", path)
     for filename in os.listdir(path):
         if not filename.endswith('.py'):
             continue
         filepath = os.path.join(path, filename)
         print("> " + filepath)
-        code = test_file(filepath)
-        if not code:
-            has_error = True
-            exit(1)
-    return not has_error
 
-if __name__ == '__main__':
-    if len(sys.argv) > 1:
-        d = sys.argv[1]
-    else:
-        d = 'tests/'
-    print("Testing directory:", d)
-    ok = test_dir(d)
-    if ok:
-        print("ALL TESTS PASSED")
+        if path == 'benchmarks/':
+            _0 = time.time()
+            if not test_file(filepath, cpython=True): exit(1)
+            _1 = time.time()
+            if not test_file(filepath): exit(1)
+            _2 = time.time()
+            print(f'  cpython:  {_1 - _0:.6f}s (100%)')
+            print(f'  pocketpy: {_2 - _1:.6f}s ({(_2 - _1) / (_1 - _0) * 100:.2f}%)')
+        else:
+            if not test_file(filepath): exit(1)
+
+if len(sys.argv) == 2:
+    assert sys.argv[1] == 'benchmark'
+    d = 'benchmarks/'
+else:
+    d = 'tests/'
+test_dir(d)
+print("ALL TESTS PASSED")