go语言获取设备cpu golang获取硬件信息( 四 )


/**
* 获取ip地址
*
* @param mContext Context
* @return ip地址字符串
*/
public static String getIpAddress(Context mContext) {
String ipAddress = null;
try {
for (Enumeration en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration enumIpAddr = intf
.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ipAddress = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
return null;
}
if (DEBUG) {
Log.d(TAG, "ip address:" + ipAddress);
}
return ipAddress;
}
232.【go 语言】PProf 的使用——CPU和内存占用分析(二) 项目更目录下执行go tool pprof,结束之后会默认进入 PProf 的命令行交互模式,接着输入top 10 ,如下图,
项目根目录下执行go tool pprof,结束之后会默认进入 PProf 的命令行交互模式,接着输入top,如图所示,
上面可以看到 ,  main.main.fun1的cum大小正好等于自身的flat大小加上main.Add大小的flat大小
Golang的pprof的使用心得(CPU,Heap)参照go语言获取设备cpu的是 这个文章
首先自己写一段demo
里面负责2件事
doSomeThingOne
genSomeBytes
运行这个程序go run main.go
To install thewrk,you need only:
git clone
cd wrk
make
wrk relies on the openssl and luajit, learn more from its github page
Generating requests
Our demo is listening on the port 9876 ,so let's generate some requests for that.
./wrk -c400 -t8 -d5m
-c400means we have 400 connections to keep open
-t8means we use 8 threads to build requests
-d5mmeans the duration of the test will last for 5 minutes
用这段命令来压服务器
Our server is very busy now and we can see some information via browser. Inputlocalhost:9876/debug/pprofyou will see:
然后用命令进入
在这里能看见各种方法go语言获取设备cpu的运行时间
所以go语言获取设备cpu我们安装Graphviz 在mac下
brew install graphviz
之后再这个(pprof)里面输入web
会生产一个svg文件
用浏览器打开go语言获取设备cpu我们就会看到
很显然gensomebytes里面go语言获取设备cpu的math方法最消耗时间 。这个就是我们优化的对象
其实也很方便在
localhost:9876/debug/pprof/profile改成
localhost:9876/debug/pprof/heap
后面的结果一样 。。和cpu一样可以看到那个heap占用了大量的内存到时候优化吧
这个文章里面的第一个方法就可以做测试内存占用的.
有空试试把
分布式程序 A B C D 4个进程在服务器. 监控程序E 打包程序F
写一个监控程序定时监控这4个进程的CPU 内存(搞成配置文件)
达到性能瓶颈(例如 90%CPU 内存剩下10%)E用shell触发打包程序F把pprof等信息打包.并发送邮件
给配置者.
go语言获取设备cpu的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于golang获取硬件信息、go语言获取设备cpu的信息别忘了在本站进行查找喔 。

推荐阅读