Skip to content

Instantly share code, notes, and snippets.

View pfmiles's full-sized avatar
🚀
Busy...may not respond.

pf_miles pfmiles

🚀
Busy...may not respond.
View GitHub Profile
@pfmiles
pfmiles / ComputeUtil.java
Created May 9, 2012 01:53
实时、持续计算大规模流数据平均值的函数
public class ComputeUtil {
/**
* 根据已有平均值、总量、新增值递推新平均值的公共函数, 计算公式: newAvg = (oldAvg * oldCount)/(oldCount+1) + newVal/(oldCount+1);
* 另外由于目前的需求是保留2位小数,所以当oldCount大到一定程度时,oldCount/(oldCount+1) 趋近于 1 且 1/(oldCount+1) 趋近于 0,就不必执行某些实际计算以减少不必要操作
*
* @param oldAvg 老平均值
* @param oldCount 老的总量
* @param newVal 新值
* @return 新平均值
@pfmiles
pfmiles / MongDBAtomicUpdate.java
Created May 22, 2012 09:46
超级麻烦的、基于“乐观锁”的mongoDB原子更新操作(在老值上加上一个新值)
// 1.查找相同主键对象
DBObject old = col.findOne(qo);
if (old == null) {
// 2.若不存在,尝试插入, 若出错"duplicate 主键",则转到3.更新操作
try {
col.insert(newObj, WriteConcern.SAFE);
} catch (DuplicateKey e) {
// 2.1 瞬间又被插了一条记录,尝试更新操作
BasicDBObject upObj = new BasicDBObject();
WriteResult rst = null;
@pfmiles
pfmiles / latency.txt
Created May 31, 2012 14:36 — forked from jboner/latency.txt
Latency numbers every programmer should know with 中文常用单位注释
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns
Mutex lock/unlock 25 ns
Main memory reference 100 ns
Compress 1K bytes with Zippy 3,000 ns // 3微秒
Send 2K bytes over 1 Gbps network 20,000 ns // 20微秒
Read 1 MB sequentially from memory 250,000 ns // 0.25毫秒
Round trip within same datacenter 500,000 ns // 0.5毫秒
Disk seek 10,000,000 ns // 10毫秒
@pfmiles
pfmiles / SetStack.java
Created June 20, 2012 05:47
A data structure which have both stack & set features, especially useful to detect 'circles' in trees or graphs.
/**
* A data structure which have both stack & set features, especially useful to
* detect 'circles' in trees or graphs.
*
* @author pf-miles
*
*/
public class SetStack<E> extends LinkedHashSet<E> {
private static final long serialVersionUID = -3159906489148656808L;
private Deque<E> innerStack = new ArrayDeque<E>();
@pfmiles
pfmiles / Test.java
Created August 4, 2012 12:04
Dummy example of dropincc.java
package test;
import com.github.pfmiles.dropincc.Action;
import com.github.pfmiles.dropincc.CC;
import com.github.pfmiles.dropincc.Exe;
import com.github.pfmiles.dropincc.Grule;
import com.github.pfmiles.dropincc.Lang;
import com.github.pfmiles.dropincc.TokenDef;
public class Test {
@pfmiles
pfmiles / Calculator.java
Created August 13, 2012 11:10
Code for a full-featured calculator in dropincc.java
import com.github.pfmiles.dropincc.Action;
import com.github.pfmiles.dropincc.CC;
import com.github.pfmiles.dropincc.Exe;
import com.github.pfmiles.dropincc.Grule;
import com.github.pfmiles.dropincc.Lang;
import com.github.pfmiles.dropincc.TokenDef;
public class Calculator {
private static Exe exe = null;
/**
@pfmiles
pfmiles / prime.py
Created September 4, 2012 15:09
python统计N以内素数个数程序
class CloneableLazyList(object):
def __init__(self, gen):
self.gen = gen
self.cache = []
self.index = 0
def __iter__(self):
return self
def next(self):
if self.index < len(self.cache):
ret = self.cache[self.index]
@pfmiles
pfmiles / jtgrep.sh
Created September 5, 2012 11:36
Helper script to find the specified thread id(nid) in a stack dump file
#!/bin/sh
nid=`python -c "print hex($1)"`
grep -i $nid $2
@pfmiles
pfmiles / loadDetectJstack.sh
Created November 6, 2012 03:14
crontab根据load阀值自动记录jstack结果的脚本
#!/bin/sh
waterMark=10
pid=`/usr/alibaba/java/bin/jps|grep AvatarEngine|cut -d ' ' -f 1`
curLoad=`cat /proc/loadavg |cut -d ' ' -f 1`
file="/tmp/$pid-`date +%Y-%m-%d-%H-%M-%S`-stack$curLoad.log"
if [ 1 == `echo "$curLoad > $waterMark"|bc` ];then
/usr/alibaba/java/bin/jstack $pid > $file
fi
@pfmiles
pfmiles / GatewayUrgentDnsSwitchServiceImpl.java
Created November 14, 2013 09:04
Java应用本地内存dns cache更改、恢复示例程序; 用于参考实现java进程内部范围内的dns绑定功能(无需更改部署机器的dns绑定或dns服务器的配置)
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.Map;
public class GatewayUrgentDnsSwitchServiceImpl implements GatewayUrgentDnsSwitchService {