Skip to content

Instantly share code, notes, and snippets.

View justdoit0823's full-sized avatar

余森彬 justdoit0823

View GitHub Profile
@justdoit0823
justdoit0823 / http_log_statistic.sh
Last active April 26, 2017 10:06
A simple awk script for http request duration statistic
# tornado http request duration histogram
awk '$1 == "[I" && $5 == 200 {sum += 1; duration = int(substr($10, 0, length($10) - 2)); if(duration < 100) r100 +=1; else if(duration < 200) r200 += 1; else if(duration < 500) r500 += 1; else r1000 += 1;} END {print "总请求量", "100ms以内", "200ms以内", "500ms以内", "500ms以外"; print sum, r100, r200, r500, r1000, (r100 + r200) / sum}' /path/app.log
# tornado http request top ten qps time
awk '$1 == "[I" && $5 == 200 {split($3, time, ","); second = $2" "time[1]; agg[second] += 1} END {for(second in agg) print second, agg[second]}' /path/app.log | sort -nr -k 3 | head
# nginx top ten qps time
awk '{second = substr($4, 2, length($4) - 1); agg[second] += 1} END {for(second in agg) print second, agg[second]}' logs/access.log | sort -nr -k 2 | head
# tornado http request duration greater than 100ms
@justdoit0823
justdoit0823 / pb3_remove_field_with_default_value.py
Last active August 28, 2017 11:29
why field with default value doesn't exist in protobuf 3
def _AddPropertiesForNonRepeatedScalarField(field, cls):
"""Adds a public property for a nonrepeated, scalar protocol message field.
Clients can use this property to get and directly set the value of the field.
Note that when the client sets the value of a field by using this property,
all necessary "has" bits are set as a side-effect, and we also perform
type-checking.
Args:
field: A FieldDescriptor for this field.
cls: The class we're constructing.
@justdoit0823
justdoit0823 / deadlock-after-fork-in-python.py
Created July 2, 2017 13:44
Deadlock in python after fork
import multiprocessing
import os
import threading
import time
def child_thread(lock):
with lock:
time.sleep(10)
@justdoit0823
justdoit0823 / type_getattr.c
Last active August 28, 2017 11:28
How Python resolves type attribute access
/* This is similar to PyObject_GenericGetAttr(),
but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
static PyObject *
type_getattro(PyTypeObject *type, PyObject *name)
{
PyTypeObject *metatype = Py_TYPE(type);
PyObject *meta_attribute, *attribute;
descrgetfunc meta_get;
if (!PyUnicode_Check(name)) {
@justdoit0823
justdoit0823 / object_getattr.c
Last active August 28, 2017 11:28
How Python resolves object attribute access
/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */
PyObject *
_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict)
{
PyTypeObject *tp = Py_TYPE(obj);
PyObject *descr = NULL;
PyObject *res = NULL;
descrgetfunc f;
Py_ssize_t dictoffset;
@justdoit0823
justdoit0823 / object_setattr.c
Created July 6, 2017 02:26
How Python object set attribute
// type object set attribute
static int
type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
{
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
PyErr_Format(
PyExc_TypeError,
"can't set attributes of built-in/extension type '%s'",
type->tp_name);
@justdoit0823
justdoit0823 / grpc_fixture.py
Created July 6, 2017 12:15
A simple grpc test fixture in pytest.
from concurrent.futures import ThreadPoolExecutor
import grpc
import pytest
import a_pb2_grpc
from rpc.server import AGRpcServer, BGRpcServer
import b_pb2_grpc
@justdoit0823
justdoit0823 / multi_update_same_row.sql
Created July 17, 2017 15:10
Update same row multiple times in postgresql
drop table if exists s_multi_update_same_row;
create table s_multi_update_same_row(
id int,
count int
);
insert into s_multi_update_same_row select id, 0 from generate_series(1, 1000) as id;
update s_multi_update_same_row A set count = count + 1 from generate_series(1, 10000) as B(id) where A.id = B.id % 1000 + 1;
@justdoit0823
justdoit0823 / hll-in-postgresql.sql
Created July 18, 2017 04:38
How hll works in postgresql
drop table if exists s_user_table;
create table s_user_table (
id int,
user_id int
);
drop table if exists s_user_cnt_table;
create table s_user_cnt_table (
@justdoit0823
justdoit0823 / emacs-startup.el
Created July 28, 2017 07:55
Autoload protobuf model when open proto file.
(defun load-protobuf ()
"Load protobuf."
(add-to-list 'load-path "~/.emacs.d/el-get/protobuf-mode")
(require 'protobuf-mode)
(add-to-list 'auto-mode-alist '("\\.proto\\'" . protobuf-mode))
(add-hook 'protobuf-mode-hook (lambda () (auto-complete-mode)))
)