火车运煤算法

转自:http://flychao88.iteye.com/blog/2187854

你是山西的一个煤老板,你在矿区开采了有3000吨煤需要运送到市场上去卖,从你的矿区到市场有1000公里,你手里有一列烧煤的火车,这个火车最多只能装1000吨煤,且其能耗比较大――每一公里需要耗一吨煤。请问,作为一个懂编程的煤老板的你,你会怎么运送才能运最多的煤到集市?

 

这道题一开始看上去好像是无解的,因为你的火车每一公里就要消耗一吨煤,而到目的地有1000公里,而火车最多只能装1000吨媒。如果你的火车可以全部装下,到目的地也会被全部烧光,一丁点也不剩。所以,很多人的第一反应都是觉得这个不太可能。

 

思考答题是

  1. 装1000吨煤,走250公里,扔下500吨煤,回矿山。
  2. 装1000吨煤,走到250公里处,拿起250吨煤继续向前到500公里处,扔下500吨煤,回矿山。此时火车上还有250吨,再加上在250公里处还有250吨煤,所以,火车是可以回矿山的。
  3. 装上最后1000吨煤,走到500公里处,装上那里的500吨煤,然后一直走到目的。

golang debugger 之 delve

Delve is a debugger for the Go programming language. The goal of the project is to provide a simple, full featured debugging tool for Go. Delve should be easy to invoke and easy to use. Chances are if you’re using a debugger, most likely things aren’t going your way. With that in mind, Delve should stay out of your way as much as possible.

地址: https://github.com/derekparker/delve

json日志分析工具: jq

上篇文章介绍了jsawk,由于一些不足,这里又发现了一个更好的工具: jq

安装:

用法:

体验一下:

 

项目主页: https://stedolan.github.io/jq/

指南: https://stedolan.github.io/jq/tutorial/

手册: https://stedolan.github.io/jq/manual/

 

字符串连接

(注意: 这里连接的是字符串,数字是不能直接当做字符串进行连接的)

类型转换:

这里通过圆括号来单独处理一部分,然后通过tostring将输出内容转换为字符串; 也可以通过 @text 实现:

join:

 

获取对象的key:

等效于

等效于:

 

过滤:(通过select 过滤出Name为default的条目,然后只显示名字和ID列)

错误的写法: (下面这个写法把Name和ID排列组合输出了)

如:

 

根据path 和 select获取感兴趣的值:

参考: https://stackoverflow.com/questions/25780807/can-i-use-a-relative-path-or-a-wildcard-in-jq

 

关联数组的key需要使用双引号,不能使用单引号:

 

修改部分值

 

在任何一个级别中查找指定字段:

 

 

构造新对象:

 

关于paths的用法: 1.3 版本中还没有paths:

这里的paths放在圆括号内,不影响输出,就好比bash中的圆括号是在单独子进程中执行一样,不影响当前的环境,同时,使用as使得圆括号的输出保存在变量中,不影响后续的输入; 这样,使得getpath所在环境中的 dot 依然是原始的输入

 

 

参考资料: https://linuxtoy.org/archives/jq.html

Extract keys using JQ

 

https://github.com/stedolan/jq/issues/885

在线测试: https://jqplay.org/jq?

注意事项:

  1. 字符串只能使用双引号,不能使用单引号
  2. if 语句必须有else, 如果其中一个分支实在没啥说的就 select(false) 相当于empty

jsawk

主页: https://github.com/micha/jsawk

用途:一直以来,日志都采用 | 分隔的多个匿名的字段组成的,每个字段代表什么含义是基于位置确定的,应该说是很不用户友好的,对于不太熟悉系统的新手是不太愿意看这样的日志的,所以,如果把日志记录成json格式的,应该说是一个进步;但是,对于经常使用awk分析基于|分隔的日志的我们来讲,awk分析json似乎就太不方便了,那么,jsawk就是我们需要的分析json的awk级别的工具

体验一下:

感受:直接把一个有很多行json的日志文件直接cat给jsawk是不行的,jsawk每次只解析一个json对象(或一个包含很多json对象的数组);如果要聚合一些信息的话,还需要输出给另一个程序(awk?)进一步处理

要不改造一下jsawk?

stat荟萃

awstats

Advanced Web Statistics

ethstatus

Console-based ethernet statistics monitor

gitstats

Generates statistics based on GIT repository activity

iptstate

A top-like display of IP Tables state table entries

iostat

Report Central Processing Unit (CPU) statistics and input/output statistics for
devices, partitions and network filesystems (NFS)

netstat

netstat – Print network connections, routing tables, interface statistics, masquerade connec-
tions, and multicast membership

netstat-nat

A tool that displays NAT connections

ifstatus

Command line real time interface graphs using ncurses

pipestat

Watches data flowing over an anonymous pipe

vnstat

Console-based network traffic monitor

smemstat

Shared memory usage monitoring tool

sysstat

The sar and iostat system monitoring commands

statsd

A simple, lightweight network daemon to collect metrics over UDP

tcpstat

tcpstat reports certain network interface statistics much like vmstat does for system statistics. tcpstat gets its information by either monitoring a specific interface, or by reading previously saved tcpdump data from a file.

 

参考:

http://www.frenchfries.net/paul/tcpstat/

系列top

top

top – display Linux tasks

iotop

iotop – simple top-like I/O monitor

iftop

iftop – display bandwidth usage on an interface by host

ntop

ntop – display top network users

atop

atop – Advanced System & Process Monitor

ftop

ftop – show progress of open files and file systems

mytop

mytop – display MySQL server performance info like ‘top’

htop

htop – interactive process viewer

 

关于安装: 如果幸运的话,都可以通过yum install 搞定

tcprstat

tcprstat is a free, open-source TCP analysis tool that watches network traffic and computes the delay between requests and responses.

tcprstat is related to the tcpstat tool, but it focuses on response time measurements, not on the amount and size of the network traffic. This makes it useful for response time analysis, which is needed for techniques such as Goal-Driven Performance Optimization.

The advantages of tcprstat are as follows:

  • It is lightweight and unobtrusive. No bulky log files need be written and analyzed.
  • Requests and responses are timed with microsecond resolution.
  • The output is easy to import into spreadsheets, manipulate with command-line scripts, graph with gnuplot, and so on.
  • It is protocol-agnostic, and works well for a large variety of client-server protocols that have a simple request-response model.

参考资料:

https://www.percona.com/docs/wiki/tcprstat:start

mysql监控管理工具–innotop

INNOTOP是一个通过文本模式显示MySQL和InnoDB的监测工具。INNOTOP是用PERL语言写成的,这使它能更加灵活的使用在各种操作平台之上,它能详细的的监控出当前MYSQL和INNODB运行的状态,以DBA根据结果,可以合理的优化MYSQL,让MYSQL更稳定更高效的运行。

官方地址: http://innotop.googlecode.com/svn/html/index.html

github地址: https://github.com/innotop/innotop

 

安装: 先试试yum search innotop

 

参考资料: http://blog.csdn.net/wyzxg/article/details/8609981