程序时间测试和内存测试

%%time
import scanpy as sc
import matplotlib.pyplot as plt
from time import time
## test time 
filename="./1M_neurons_neuron20k.h5"
x0=time()
adata = sc.read_10x_h5(filename)
print(adata)
x1=time()
print("read done..............cost {}s".format(round(x1-x0)))
sc.pp.recipe_zheng17(adata)
x2=time()
print("preprocess.............cost {}s".format(round(x2-x1)))
sc.pp.neighbors(adata)
x3=time()
print("cal neighbors..........cost {}s".format(round(x3-x2)))
sc.tl.louvain(adata)
x4=time()
print("louvain done ..........cost {}s".format(round(x4-x3)))
sc.tl.umap(adata)
x5=time()
print("cal umap done..........cost {}s".format(round(x5-x4)))
sc.pl.umap(adata,color="louvain",show=False)
x6=time()
print("plot umap done.........cost {}s".format(round(x6-x5)))
plt.savefig("./20k_louvain.png")
x7=time()
print("all done.........total cost {}s".format(round(x7-x0)))

结果如下
在这里插入图片描述记录一下

内存使用

如果同时测试时间和内存,方式如下
其中有monitor_time_linux.sh

echo -e "Runtime(s)\tMemoryUse"
SECONDS=0
while [ 1 ]; do
        ## Get the PID of the process name given as argument 1
        #specify PID 
        pidno=$1
        ## If the process is running, print the memory usage
        sleep 1
        #on ubuntu/centos
        if [ -e /proc/$pidno/statm ]; then
                ## Get the memory info,on ubunt
                #m=`awk '{OFS="\t";print $1, $2,$3,$6}' /proc/$pidno/statm`
                ## Get the memory percentage
                #perc=`top -bd .10 -p $pidno -n 1  | grep $pidno | awk '{OFS="\t";print $6,$10}'`# $10 is percentage
                perc=`top -bd .10 -p $pidno -n 1  | grep $pidno | awk '{OFS="\t";print $6}'`
        else
                echo "# $1 is not running";
                break;
        fi
         echo -e "$SECONDS\t$perc";
done

那么使用的方式如下,首先写好自己的测试.py文件(mousebrain_20k_cluster.py)

import scanpy as sc
import matplotlib.pyplot as plt
from time import time
## test time 
filename="./1M_neurons_neuron20k.h5"
x0=time()
adata = sc.read_10x_h5(filename)
print(adata)
x1=time()
print("read done..............cost {}s".format(round(x1-x0)))
sc.pp.recipe_zheng17(adata)
x2=time()
print("preprocess.............cost {}s".format(round(x2-x1)))
sc.pp.neighbors(adata)
x3=time()
print("cal neighbors..........cost {}s".format(round(x3-x2)))
sc.tl.louvain(adata)
x4=time()
print("louvain done ..........cost {}s".format(round(x4-x3)))
sc.tl.umap(adata)
x5=time()
print("cal umap done..........cost {}s".format(round(x5-x4)))
sc.pl.umap(adata,color="louvain",show=False)
x6=time()
print("plot umap done.........cost {}s".format(round(x6-x5)))
plt.savefig("./20k_louvain.png")
x7=time()
print("all done.........total cost {}s".format(round(x7-x0)))

那么在服务器上就可以运行以下命令就可以了

nohup python -u mousebrain_20k_cluster.py >log_20k.txt 2>&1 &
nohup bash monitor_time_linux.sh $! > runtime_20k.txt 2>&1 &
(加回车)

等程序运行完就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
测试程序内存泄漏和内存溢出、OOM(Out of Memory)和ANR(Application Not Responding)是为了确保程序在使用内存和响应用户输入时的稳定性和可靠性。 内存泄漏是指程序中已经不再使用的内存没有被正确释放,导致内存的占用不断增加,最终可能导致程序崩溃。为了测试内存泄漏,可以创建一个长时间运行的程序,并通过监测内存使用情况来判断是否有内存泄漏。可以使用内存分析工具来检测未被垃圾回收器回收的对象,以及通过分析堆转储文件来查找内存泄漏的源头。 内存溢出是指程序在申请内存时,无法分配到足够的内存空间,导致程序崩溃。为了测试内存溢出,可以通过申请大量的内存空间来触发溢出,或者通过无限制地生成对象导致内存快速占满。可以使用性能测试工具来模拟大量并发请求和数据量,以模拟真实环境中的内存使用情况,从而找出内存溢出的问题。 OOM是指由于内存不足导致程序无法继续分配内存空间而崩溃。为了测试OOM,可以通过限制程序可用内存的上限,观察程序在分配内存时是否能够正常运行,当内存达到上限时,是否能够优雅地处理内存不足的情况。 ANR是指应用程序无法在规定的时间内响应用户的输入事件,导致系统认为应用程序无响应而弹出ANR对话框。为了测试ANR,可以创建一个需要执行较长时间的代码块,来模拟应用程序无法及时响应用户输入的情况。可以通过监测主线程的响应时间来判断是否出现ANR。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值