
import requests
import time
from threading import Thread
from multiprocessing import Process
import datetime

# 定义CPU密集的计算函数
def count(x, y):
    # 使程序完成150万计算
    c = 0
    while c < 500000:
        c += 1
        x += x
        y += y

# 定义IO密集的文件读写函数
def write():
    f = open("test.txt", "w")
    for x in range(5000000):
        f.write("testwrite\n")
    f.close()


def read():
    f = open("test.txt", "r")
    lines = f.readlines()
    f.close()

# 定义网络请求函数
_head = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36'}
url = "http://www.tieba.com"
def http_request():
    try:
        webPage = requests.get(url, headers=_head)
        html = webPage.text
        return {"context": html}
        print("context") #debug
    except Exception as e:
        return {"error": e}


# 测试线性执行IO密集操作、CPU密集操作所需时间、网络请求密集型操作所需时间
def single_thread():
    # CPU密集操作
    t = time.time()

    for x in range(10):
        count(1, 1)
    print("Line cpu", time.time() - t)

    # IO密集操作
    t = time.time()
    for x in range(10):
        write()
        read()
    print("Line IO", time.time() - t)

    # 网络请求密集型操作
    t = time.time()
    for x in range(10):
        http_request()
    print("Line Http Request", time.time() - t)

def multi_thread():
    # 多线程并发执行CPU密集操作所需时间
    counts = []
    t = time.time()
    for x in range(10):
        thread = Thread(target=count, args=(1, 1))
        counts.append(thread)
        thread.start()

    e = counts.__len__()
    while True:
        for th in counts:
            if not th.is_alive():
                e -= 1
        if e <= 0:
            break
    print(time.time() - t)

    #多线程并发执行IO密集操作所需时间
    def io():
        write()
        read()

    t = time.time()
    ios = []
    t = time.time()
    for x in range(10):
        thread = Thread(target=count, args=(1, 1))
        ios.append(thread)
        thread.start()

    e = ios.__len__()
    while True:
        for th in ios:
            if not th.is_alive():
                e -= 1
        if e <= 0:
            break
    print(time.time() - t)

    # 多线程并发执行网络密集操作所需时间
    t = time.time()
    ios = []
    t = time.time()
    for x in range(10):
        thread = Thread(target=http_request)
        ios.append(thread)
        thread.start()

    e = ios.__len__()
    while True:
        for th in ios:
            if not th.is_alive():
                e -= 1
        if e <= 0:
            break
    print("Thread Http Request", time.time() - t)

def multi_process():
    # 多进程并发执行CPU密集操作所需时间
    counts = []
    t = time.time()
    for x in range(10):
        process = Process(target=count, args=(1, 1))
        counts.append(process)
        process.start()
    e = counts.__len__()
    while True:
        for th in counts:
            if not th.is_alive():
                e -= 1
        if e <= 0:
            break
    print("Multiprocess cpu", time.time() - t)

    # 多进程并发执行IO密集型操作
    t = time.time()
    ios = []
    t = time.time()
    for x in range(10):
        process = Process(target=io)
        ios.append(process)
        process.start()

    e = ios.__len__()
    while True:
        for th in ios:
            if not th.is_alive():
                e -= 1
        if e <= 0:
            break
    print("Multiprocess IO", time.time() - t)

    # 多进程并发执行Http请求密集型操作
    t = time.time()
    httprs = []
    t = time.time()
    for x in range(10):
        process = Process(target=http_request)
        ios.append(process)
        process.start()

    e = httprs.__len__()
    while True:
        for th in httprs:
            if not th.is_alive():
                e -= 1
        if e <= 0:
            break
    print("Multiprocess Http Request", time.time() - t)


def main():
    # # 多进程并发执行CPU密集操作所需时间
    # counts = []
    # t = time.time()
    # for x in range(10):
    #     print("loop %d start: %s" %(x+1,datetime.datetime.now()))
    #     process = Process(target=count, args=(1, 1))
    #     counts.append(process)
    #     process.start()
    # e = counts.__len__()
    # while True:
    #     for th in counts:
    #         if not th.is_alive():
    #             e -= 1
    #     if e <= 0:
    #         break
    # print("Multiprocess cpu", time.time() - t)

    # 多线程并发执行网络密集操作所需时间
    t = time.time()
    ios = []
    t = time.time()
    for x in range(10):
        print("thread %d at %s:" %(x+1,datetime.datetime.now()))
        thread = Thread(target=http_request)
        ios.append(thread)
        thread.start()

    e = ios.__len__()
    while True:
        for th in ios:
            if not th.is_alive():
                e -= 1
        if e <= 0:
            break
    print("Thread Http Request", time.time() - t)

if __name__=="__main__":
    main()