bitterapricot commited on
Commit
02252d8
·
1 Parent(s): bc08ced

fix error.

Browse files
Files changed (4) hide show
  1. Dockerfile +3 -1
  2. app_cpppy/test.py +41 -0
  3. app_fast/app_fast.py +5 -2
  4. scripts/start.sh +1 -1
Dockerfile CHANGED
@@ -46,7 +46,9 @@ RUN chown -R user:user /var/lib/nginx/ \
46
  && chown -R user:user /run/ \
47
  && chmod -R 755 /run
48
  USER user
49
- ENV PATH="/home/user/.local/bin:$PATH"
 
 
50
 
51
  WORKDIR /app
52
 
 
46
  && chown -R user:user /run/ \
47
  && chmod -R 755 /run
48
  USER user
49
+ # 设置环境变量
50
+ ENV PATH="/home/user/.local/bin:/app/bin:$PATH"
51
+ ENV PYTHONPATH=/usr/local/lib/python3.12/site-packages:/app/bin:$PYTHONPATH
52
 
53
  WORKDIR /app
54
 
app_cpppy/test.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import time
3
+ import os
4
+ import sys
5
+ import app_cpppy # 导入C++扩展
6
+
7
+ def python_matmul(x, y):
8
+ return np.dot(x, y)
9
+
10
+ size = 1000
11
+ a = np.random.rand(size, size).astype(np.float64)
12
+ b = np.random.rand(size, size).astype(np.float64)
13
+
14
+
15
+ try:
16
+ # 测试C++扩展
17
+ start = time.time()
18
+ cpp_result = app_cpppy.matrix_multiply(a, b)
19
+ cpp_time = time.time() - start
20
+ except Exception as e:
21
+ cpp_time = time.time() - start
22
+ cpp_result = f'❌ Import failed: {e}'
23
+
24
+ # 测试Python实现
25
+ start = time.time()
26
+ py_result = python_matmul(a, b)
27
+ py_time = time.time() - start
28
+
29
+ # 验证结果一致性
30
+ diff = np.abs(cpp_result - py_result).max()
31
+
32
+ res = {
33
+ "cpp_time": cpp_time,
34
+ "python_time": py_time,
35
+ "speedup": py_time / cpp_time if cpp_time > 0 else 0,
36
+ "max_difference": diff,
37
+ "matrix_size": size
38
+ }
39
+
40
+ print(res)
41
+
app_fast/app_fast.py CHANGED
@@ -252,6 +252,7 @@ def cppbenchmark():
252
  def python_matmul(x, y):
253
  return np.dot(x, y)
254
 
 
255
  try:
256
  # 测试C++扩展
257
  start = time.time()
@@ -260,7 +261,8 @@ def cppbenchmark():
260
  cpp_time = time.time() - start
261
  except Exception as e:
262
  cpp_time = time.time() - start
263
- cpp_result = f'❌ Import failed: {e}'
 
264
 
265
  # 测试Python实现
266
  start = time.time()
@@ -275,5 +277,6 @@ def cppbenchmark():
275
  "python_time": py_time,
276
  "speedup": py_time / cpp_time if cpp_time > 0 else 0,
277
  "max_difference": diff,
278
- "matrix_size": size
 
279
  }
 
252
  def python_matmul(x, y):
253
  return np.dot(x, y)
254
 
255
+ res = "ok"
256
  try:
257
  # 测试C++扩展
258
  start = time.time()
 
261
  cpp_time = time.time() - start
262
  except Exception as e:
263
  cpp_time = time.time() - start
264
+ cpp_result = 0
265
+ res = f'❌ Import failed: {e}'
266
 
267
  # 测试Python实现
268
  start = time.time()
 
277
  "python_time": py_time,
278
  "speedup": py_time / cpp_time if cpp_time > 0 else 0,
279
  "max_difference": diff,
280
+ "matrix_size": size,
281
+ "result": res
282
  }
scripts/start.sh CHANGED
@@ -12,6 +12,6 @@ echo "Current Directory: $(pwd)"; echo "CPU Info:"; lscpu; echo "Disk Usage:"; d
12
  #nginx -c "$(pwd)/nginx/nginx.conf"
13
 
14
  # 启动 FastAPI // --workers 3
15
- #uvicorn fast_app:app --app-dir ./fast_app --reload --host 127.0.0.1 --port 8000 --log-level info
16
 
17
  echo "Server started ..."
 
12
  #nginx -c "$(pwd)/nginx/nginx.conf"
13
 
14
  # 启动 FastAPI // --workers 3
15
+ #uvicorn app_fast_test:app --app-dir ./app_fast --reload --host 127.0.0.1 --port 3300 --log-level info
16
 
17
  echo "Server started ..."