Loading Logic 리팩토링결과

Yellta·2024년 5월 16일
0

Projects

목록 보기
2/6

OBJECT:

LoadingLogic의 리팩토링 실시 후 결과를 확인해보자

ANALYSIS:

LoadingV1

  
  
  WebDriver driver = new ChromeDriver(options);
  driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
  
  List<WebElement> fields = driver.findElements(By.className("form-field"));
  
  for (WebElement field : fields) {
       Food contents = extractIdTitle(field);
                    if (contents.getName() == null) {
                        //if map is empty then not save the data
                    } else {
                        //log.info("contents =?", contents);
                        result.put(contents.getId(), contents);
                    }

로딩의 핵심 로직이다.

selenium라이브러리를 통해서 form-field class를 갖는 항목을 모두 가져와서 조사했다.

가져온 항목중에서는 조사가 필요없는 부분도 있었다.

해당 부분은

For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
Build info: version: '4.8.3', revision: 'e5e76298c3'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '17.0.4'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [b744ea508210551a456d9f05f6e20801, findChildElement {id=f.80BABB3A6EA9CC4DAD0887AB1A9F19D6.d.335A6CD633CAC6A0ED0B6BA53FFC4032.e.147, using=tag name, value=input}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 124.0.6367.207, chrome: {chromedriverVersion: 124.0.6367.207 (a9001a6e39f..., userDataDir: C:\Users\bbubb\AppData\Loca...}, fedcm:accounts: true, goog:chromeOptions: {debuggerAddress: localhost:62411}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:62411/devtoo..., se:cdpVersion: 124.0.6367.207, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true}
Session ID: b744ea508210551a456d9f05f6e20801

이런 noSuchElements 에러를 일으켰는데 문제는 해당 에러가 터지려면 20초의 대기시간이 필요했다.

그 이유는

 WebDriver driver = new ChromeDriver(options);
 driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));

웹 드라이버의 최대 로딩 시간을 20초로 지정했기 때문이다. 즉 웹 페이지에서 해당 엘레멘트를 찾을 시간을 최대 20초 준다는 의미이다.

V1은 noSuchElements 에러를 총 5회 발생시키고 있었는데 이는 최소 100초의 시간을 낭비한다는 의미였다.

실제로 해당 부분은 에러가 터져도 상관이 없는(no such Element가 터져도 넘기면 되는 부분)이었다. 이유는 내가 해당 부분의 element가 필요하지 않았기 때문이다.

CONCLUSION:

불필요한 요소의 로딩을 제거하기로 했다.

웹 사이트 구조 분석

데이터를 가져오려는 웹 사이트의 구조를 분석했다.

ul(form_container_wrapper) < li (form-field)형식으로 구성된 부분이 내가 필요한 부분이란것을 찾을 수 있었다.

SOLVE:

 List<WebElement> section = driver.findElements(By.className("form_container_wrapper"));

            for (WebElement fields : section) {
                List<WebElement> elements = fields.findElements(By.className("form-field"));

                for (WebElement field : elements) {
                    //extract vaild id list logic
                    String id = field.getAttribute("id");
                    if (id.equals("field_0") | id.equals("field_1") | id.equals("field_84")) {

                    }else{
//                        enterValue(field, dummyStore);
                    }
                }
            }

form_container_wrapper의 부분을 가져온다. 내가 원하는 ul의 부분이다.

해당 부분에서 form-field class를 갖는 li를 추출한다.

뽑아낸 li리스트에서는 필요없는 요소는 없었다. 따라서 20초를 기다리지 않아도 됐었다.

120secs→ 16secs… 약 8배나 개선되어 벌임

비약적인 성과를 내버렸다.

REVIEW:

에러가 터졌을 때 어떤식으로 처리할지는 순전히 개발자에게 달려있다. 처음에는 단순히 프로그램이 잘 돌아가는 방향으로만 작업을 수행했기 때문에 시간, 자원이 낭비되는 경우를 미처 생각하지 못했었다.
하지만 이런 경험을 바탕으로 시간, 자원까지도 생각하는 개발자가 되도록 하자!! 실제로 OS의 특징은 빠른시간내에 원하는 작업을 처리하는 것이니까 프로그램도 별 다른바가 없다고 생각한다. 그리고 자원은 덜쓰면 덜 쓸 수록 좋으니까 말이다.

profile
Yellta가 BE개발해요! 왜왜왜왜왜왜왜왜왜왜왜왜왜왜왜왜왜왜왜 가 제일 중요하죠

0개의 댓글