Skip to content

Instantly share code, notes, and snippets.

View justdoit0823's full-sized avatar

余森彬 justdoit0823

View GitHub Profile
//
// ViewController.swift
// SlideView
//
// Created by 余森彬 on 2017/3/10.
// Copyright © 2017年 余森彬. All rights reserved.
//
import CoreGraphics
import CoreText
@justdoit0823
justdoit0823 / ios_swift_slide_view.swift
Last active March 26, 2017 12:48
create and close slide subview in iOS with swift
func showSlideView() {
let parentFrame = self.view.frame
let leftframe = CGRect(x: 0, y: 0, width: 0, height: parentFrame.height)
let rightframe = CGRect(x: parentFrame.width * 0.8, y: 0, width: parentFrame.width * 0.2, height: parentFrame.height)
let slideView = UIView(frame: leftframe)
let rightView = UIView(frame: rightframe)
let singleFingerTap = UITapGestureRecognizer(target: self, action: #selector(self.closeSlide))
rightView.addGestureRecognizer(singleFingerTap)
@justdoit0823
justdoit0823 / tornado_request.py
Last active April 21, 2017 06:17
A simple decorator for handling tornado http request arguments.
def param_schema(**schema):
"""
请求参数检查装饰器, 使用参数如下:
key为请求参数,value可以说单个的类型值、(类型值,默认值)元组
from tornado import web
class ExampleRequestHandler(web.RequestHandler):
@justdoit0823
justdoit0823 / sqlalchemy_session.py
Last active January 25, 2023 15:50
A simple sqlalchemy session decorator and context manager for db operation function.
session_engines = {}
def get_new_session(connection=None, autocommit=None):
connection = connection or 'default'
connection_settings = settings.DATABASES[connection]
connection_autocommit = ValueUtils.none_or(
connection_settings.get('autocommit'), False)
autocommit = ValueUtils.none_or(autocommit, connection_autocommit)
@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 / epoll_wait_flag.py
Created May 4, 2017 01:16
A check script about how epoll works.
"""Check how epoll works."""
import click
import errno
import os
import select
import socket
import time
#include "stdio.h"
#include "unistd.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/epoll.h>
@justdoit0823
justdoit0823 / show_process_socket_backlog.sh
Created May 27, 2017 09:20
A simple command to list busy processes which don't read ready data.
ps -fp `netstat -tnp|awk '$2 > 0'|awk '{split($NF, a, "/"); print a[1]}'|awk '/^[0-9]/'|sort -n|uniq`
@justdoit0823
justdoit0823 / host_speed.sh
Created June 23, 2017 03:44
A simple command for measuring host speed.
for host in `echo 'google.com facebook.com twitter.com youtube.com' | cut -d ' ' -f 1-`; do ping -c 64 $host | tail -3; done
@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.