Python Windows Service

Python Windows Service

Background

最近在优化LoadTest的流程,在执行测试的时候,我们需要在10台虚拟机上运行HostProxy, 这意味着需要登陆这10台机子并且一个个运行代理程序,这简直要人命。

所以我第一个想到的就是把这个过程做成一个windows service,让他们一直监控一个文件命令,当下达运行指令的时候,10台机子就会自动运行,同理也能停止。

虽然最后发现通过service启的进程不能被Host Manager访问到(有时间再仔细研究,应该有解决方案),用Scheduler替代了,但其它操作还是没有问题的。

Read more
Progress Bar in Python

Progress Bar in Python

Progress Bar in Python

很多时候,我们在测试的过程中,希望能够直观的看到测试的进度,而不是去看 log 或者查询数据库得到信息。

这个时候,如果有数据完成的进度的话就非常的方便。

今天就简单总结下这两天查询的资料,并且已经在实际中用到。

Read more
XPath in Python

XPath in Python

XPath In Python

在对XML文件进行处理的时候,可以使用标准DOM的 api,但是相对来说不是很方便。

使用XPath的话就可以快速的定位节点,选取得到想要的值。

下面就对在Python中使用XPath处理XML文档做简单的介绍。

Read more
Class In Python

Class In Python

Class In Python

今天简单总结下在Python中定义类的方法,包括属性,方法,继承等方面的内容。

主要还是根据自己的实践和踩过的坑来描述,后续如果有新的内容,会继续更新。

Read more
Files Operation In Python

Files Operation In Python

1
2
# update log:
# 08/30/2018: update the method name style

Files Operation In Python

We usually need to handle files like read/write files with different type, so today I’ll show some codes to do this.

Read more
Data Structure In Python

Data Structure In Python

Data Structure In Python

Today I met a problem that I would like to count the duplicate items in a list. Generally, I could iterate the list and count it by myself.

But I don’t think that’s a good idea, and python perhaps have the easiest way to do this.

So I searched for the solution and find it. there is a build-in function to do this.

1
2
3
4
5
6
7
8
9
>>> list_test = ['test','test','xiche','xiche','xiche',1,1,1,1,1,['a']]
>>> print(list_test.count('test'))
2
>>> print(list_test.count('xiche'))
3
>>> print(list_test.count(1))
5
>>> print(list_test.count(['a']))
1

So it’s important to be familar with the usage of data structure in python and it will help you to code more efficient.

And let me summarize some basic operations about them and I will continue to update this article when I find some new interesting tips.

Learn to look into offical document is very import.

Read more
Performance Test Training (2)

Performance Test Training (2)

Performance Test Training (2)

性能测试培训的第二天,主要内容是性能测试一些实例,这些要总结好难,还是要实际操作。

核心的规则还是从进程,到线程,CPU,内存,堆栈,IO,网络一步步分析。

培训上基本上是都是 linux 的命令,我下面简单罗列了一些window下的命令,而且很不全,参考意义不大。

之后有时候再一块一块深入,每一块都有好多细节。。。今天就算给自己交个差吧(捂脸)

Read more
Performance Test Training (1)

Performance Test Training (1)

Performance Test Training (1)

今天是性能测试培训的第一天,主要内容是性能测试涉及到的概念,以及讲师自己的一些实践,个人感觉还是有些收获的。

下面就简单总结下一点知识。

Read more
Spider for Douban (01)

Spider for Douban (01)

Top n Movies of Spider for Douban

网上有许多爬虫的入门脚本,都是去爬豆瓣前 250 的电影信息,我也参考并写了一个,做为入门。

因为豆瓣的网页结构相对稳定,变化不多,不然要花太多时候去维护。

在代码中可以修改 page 的值来取得更多的页数,我发现其实是可以超过 250 个的,排名 250 后的资源,也可以通过手动修改得到。

Read more
System Monitor by Powershell

System Monitor by Powershell

Process Monitor

先上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
$CurrentDir = Split-Path $MyInvocation.MyCommand.Path
$file = "$CurrentDir/Monitor.csv"
$ps_name = "java"
$show_sys_cpu_rate = $false
$show_sys_mem_rate = $false

$memory_sys_total = (Get-WmiObject -Class Win32_PhysicalMemory |measure capacity -sum).Sum #(gwmi win32_computersystem).TotalPhysicalMemory
$cpu_cores = (Get-WmiObject Win32_ComputerSystem).NumberOfLogicalProcessors
# $memory_sys_total = (Get-Counter "\Memory\System Driver Total Bytes").CounterSamples | Sort-Object Path
# $memory_sys_total = $memory_sys_total[0].CookedValue
"time,CPU(%),Memory(%),CPU_$ps_name(%), Memory_$ps_name(%)" >> $file
while ($True) {
$cpu_rate_pss = (Get-Counter "\process($ps_name*)\% Processor Time").CounterSamples | Sort-Object Path
$cpu_rate_sys = (Get-Counter "\processor(_total)\% processor time").CounterSamples | Sort-Object Path
$cpu_rate_sys = $cpu_rate_sys[0].CookedValue
if($show_sys_cpu_rate -eq $false){
$cpu_rate_sys = 0
}

$memory_pss = (Get-Counter "\Process($ps_name*)\Working Set - Private").CounterSamples | Sort-Object Path

if($show_sys_mem_rate){
$memory_sys_available = (Get-Counter "\Memory\Available Bytes").CounterSamples | Sort-Object Path
$memory_sys_available = $memory_sys_available[0].CookedValue
$memory_sys = $memory_sys_total - $memory_sys_available
}else{
$memory_sys = 0
}

$cpu_rate_pss_total = 0
$memory_pss_total = 0
$cpu_rate_pss.Count
try {
For ($i = 0; $i -lt $cpu_rate_pss.Count; $i++) {
$cpu_rate_pss_total = $cpu_rate_pss_total + $cpu_rate_pss[$i].CookedValue
$memory_pss_total = $memory_pss_total + $memory_pss[$i].CookedValue
}
}catch{
Write-Output ""
}

$cpu_rate_sys = "{0:F2}" -f $cpu_rate_sys
$cpu_rate_pss_total = "{0:F2}" -f $cpu_rate_pss_total / $cpu_cores

$memory_rate_sys = "{0:F2}" -f ( $memory_sys / $memory_sys_total * 100)
$memory_rate_pss_total = "{0:F2}" -f ( $memory_pss_total / $memory_sys_total * 100)

$time = Get-Date -format "MM/dd/yyyy HH:mm:ss"

Write-Output "Time=$time;CPU=$cpu_rate_sys;Mem=$memory_rate_sys;CPU($ps_name)=$cpu_rate_pss_total;Mem($ps_name)=$memory_rate_pss_total"
"$time,$cpu_rate_sys,$memory_rate_sys,$cpu_rate_pss_total,$memory_rate_pss_total" >> $file

sleep 1
}

之前也有写过监测系统CPU和内存的Powershell脚本和Python脚本,不过当时都只有监测系统的CPU和内存的占比变化,没有针对进程。

最近在做测试的时候,已经在脚本中加入了Python对进程的操作,不过因为和具体项目结合在一起,还没有时间把代码抽象出来。今天本来想着在之前Powershell脚本的基础上改一下,不过没想到踩了一些坑,主要还是不熟悉。尤其如果对C#比较了解的话,写起来会方便很多。Powershell可以直接调用.Net的许多对象和方法。

Read more