Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Latest commit

 

History

History
172 lines (130 loc) · 7.03 KB

파이썬_파헤치기.md

File metadata and controls

172 lines (130 loc) · 7.03 KB

Custom_parser VS shlex

# Normal
    def test_custom_parser(self):
        file_paths = glob.glob("/Users/len/log-analyer-assignment/logdata/20180824/*.txt", recursive=False)
        start_time = time.time()
        for file_path in file_paths:
            with open(str(file_path), 'r', encoding='utf8') as infile:
                lines = infile.readlines()
                for row_list in tqdm(lines):
                    a = custom_log_parser(row_list)
        print(time.time() - start_time)

        self.assertEqual([], [])

100%|██████████| 2789569/2789569 [02:35<00:00, 17979.13it/s] 100%|██████████| 2805010/2805010 [02:21<00:00, 19786.39it/s] 100%|██████████| 2690228/2690228 [03:00<00:00, 14905.02it/s]

약 8분

    def test_shlex_parser(self):
        file_paths = glob.glob("/Users/len/log-analyer-assignment/logdata/20180824/*.txt", recursive=False)
        start_time = time.time()
        for file_path in file_paths:
            with open(str(file_path), 'r', encoding='utf8') as infile:
                lines = infile.readlines()
                for row_list in tqdm(lines):
                    a = get_the_request_api_and_last_one_and_datetime(row_list)

        print(time.time() - start_time)

        self.assertEqual([], [])

12%|█▏ | 333477/2789569 [03:31<25:55, 1579.12it/s]

하나에 약 26분 그러므로 3개면 1시간 예상.

멀티쓰레딩 VS 멀티프로세스

멀티프로세스 상황 (cf, 행아웃을 켜놓은 상태에서 진행됨.)

it/s => Insert Time / Second

Common

라인을 파싱 한 후-

Output은 동기화된 하나의 List가 나온다.

List 를 안했을 경우

 99%|█████████▉| 2789592/2805010 [02:54<00:00, 16029.65it/s]
100%|█████████▉| 2794039/2805010 [02:54<00:00, 16043.91it/s]
100%|█████████▉| 2799177/2805010 [02:54<00:00, 16064.19it/s]
100%|█████████▉| 2804653/2805010 [02:54<00:00, 16086.38it/s]
100%|██████████| 2805010/2805010 [02:54<00:00, 16085.95it/s]
178.66

List를 append했을 경우

1번째
100%|█████████▉| 2802522/2805010 [22:29<00:01, 2075.98it/s]
100%|█████████▉| 2802970/2805010 [22:30<00:00, 2076.16it/s]
100%|█████████▉| 2803382/2805010 [22:30<00:00, 2076.23it/s]
100%|█████████▉| 2803823/2805010 [22:30<00:00, 2076.41it/s]
100%|█████████▉| 2804254/2805010 [22:30<00:00, 2076.57it/s]
100%|█████████▉| 2804657/2805010 [22:30<00:00, 2076.69it/s]
100%|██████████| 2805010/2805010 [22:30<00:00, 2076.84it/s]

2번째
100%|█████████▉| 2801841/2805010 [18:29<00:01, 2524.32it/s]
100%|█████████▉| 2802551/2805010 [18:30<00:00, 2524.69it/s]
100%|█████████▉| 2803225/2805010 [18:30<00:00, 2525.05it/s]
100%|█████████▉| 2803920/2805010 [18:30<00:00, 2525.45it/s]
100%|█████████▉| 2804593/2805010 [18:30<00:00, 2525.74it/s]
100%|██████████| 2805010/2805010 [18:30<00:00, 2525.88it/s]

멀티쓰레딩(3개)

List를 사용 안했을 경우

 96%|█████████▌| 2664284/2789569 [00:07<00:00, 377145.39it/s]
100%|██████████| 2690228/2690228 [00:11<00:00, 226615.12it/s]
100%|██████████| 2805010/2805010 [00:09<00:00, 284780.62it/s]
 98%|█████████▊| 2745558/2789569 [00:07<00:00, 383870.05it/s]
100%|██████████| 2789569/2789569 [00:08<00:00, 345637.54it/s]
36.15

디버깅으로 실행시 :
 99%|█████████▉| 2761917/2789569 [00:45<00:00, 60586.04it/s]
 99%|█████████▉| 2770462/2789569 [00:45<00:00, 60626.48it/s]
100%|█████████▉| 2779615/2789569 [00:45<00:00, 60693.44it/s]
100%|██████████| 2789569/2789569 [00:45<00:00, 60784.47it/s]
100%|██████████| 2805010/2805010 [00:45<00:00, 61459.50it/s]
Exiting Main Thread
65.96

List를 사용하고 append했을 경우

 99%|█████████▉| 2788969/2805010 [06:27<00:02, 7198.46it/s]
100%|█████████▉| 2793281/2805010 [06:27<00:01, 7207.73it/s]
100%|█████████▉| 2797397/2805010 [06:27<00:01, 7215.99it/s]
100%|█████████▉| 2802001/2805010 [06:27<00:00, 7225.95it/s]
100%|██████████| 2805010/2805010 [06:29<00:00, 7200.06it/s]

디버깅으로 실행시:
100%|█████████▉| 2786107/2789569 [11:40<00:00, 3977.83it/s]
100%|█████████▉| 2787206/2789569 [11:40<00:00, 3978.83it/s]
100%|█████████▉| 2788291/2789569 [11:40<00:00, 3979.75it/s]
100%|██████████| 2789569/2789569 [11:40<00:00, 3980.53it/s]
100%|██████████| 2805010/2805010 [11:43<00:00, 3985.64it/s]
726.7834770679474

하나의 프로세스 설정하고 3개의 쓰레드

List를 사용하고 append했을 경우

99%|█████████▉| 2672578/2690228 [22:10<00:08, 2009.24it/s]�[A�[A
 96%|█████████▌| 2683970/2789569 [22:11<00:52, 2015.98it/s]�[A

97%|█████████▋| 2716164/2805010 [23:41<00:46, 1910.37it/s]
 97%|█████████▋| 2716582/2805010 [23:41<00:46, 1910.53it/s]
 97%|█████████▋| 2716988/2805010 [23:42<00:46, 1910.66it/s]
 97%|█████████▋| 2717419/2805010 [23:42<00:45, 1910.83it/s]
 97%|█████████▋| 2717903/2805010 [23:42<00:45, 1911.04it/s]
100%|█████████▉| 2788819/2789569 [23:41<00:00, 1961.70it/s]�[A
 97%|█████████▋| 2718331/2805010 [23:42<00:45, 1911.19it/s]
100%|██████████| 2805010/2805010 [24:03<00:00, 1943.40it/s]

2개의 프로세스, 3개의 쓰레드

List를 사용하고 append했을 경우

 1%|          | 8514/1380802 [00:08<21:39, 1056.05it/s]

  0%|          | 3627/1380802 [00:03<20:05, 1142.17it/s]�[A�[A
  0%|          | 3263/1380802 [00:03<21:20, 1076.09it/s]�[A
  0%|          | 6067/1380797 [00:04<16:50, 1360.77it/s]�[A

  0%|          | 3268/1380802 [00:02<20:50, 1101.25it/s]�[A�[A

  1%|          | 8652/1380802 [00:08<21:38, 1056.50it/s]

REFERENCE_CODE

  1. 같은 코드라도 디버깅모드에서의 Run과 일반 Run과는 차이가 있다.
  2. 각 라인에 대해서 Dict insert 또는 List append 를 추가시 파일 입력속도가 최고 2배까지 느려진다.(멀티 프로세스에서 확인 할 수 있었음. 멀티프로세스에서 공유 자원을 활용하기 때문)
  3. 많은 프로세스와 쓰레드를 사용한다고 해서 속도가 빨라지는 것은 아니다.
  4. 하나의 공유되는자원이 있다면 쓰레드를 쓰는 것이 현명하다.

https://www.tutorialspoint.com/python3/python_multithreading.htm

https://medium.com/@bfortuner/python-multithreading-vs-multiprocessing-73072ce5600b