function checkOnlyCharacter(data) {
for (var i = 0; i < data.length; i++) {
var temp = data.substring(i, i + 1);
if (!temp.match(/[0-9]|[a-z]|[A-Z]/)) {
return true;
}
}
}


private String getBarcodeCheckBit(String barcodeNumber) {
StringBuffer result = new StringBuffer();
if (barcodeNumber.length() <= NORMAL_BARCODE_LENGTH) {
for (int i = 0; i < NORMAL_BARCODE_LENGTH - barcodeNumber.length(); i++) {
result.append("0");
}
result.append(barcodeNumber);
}
String barcode = result.toString();
int s = 0;
int d = 0;
for (int i = 0; i < NORMAL_BARCODE_LENGTH - 1; i++) {
if (i % 2 == 0) {
s += Character.getNumericValue(barcode.charAt(i));
} else {
d += Character.getNumericValue(barcode.charAt(i)) * 3;
}
}

int chk = (10 - ((s + d) % 10)) % 10;

return String.valueOf(chk);
}


List<EditHistory> getEditHistory(String userId, Obj source, Obj target, Class clazz) {
List<EditHistory> editHistoryList = new ArrayList<>();
try {

for (Method sourceMethod : clazz.getMethods()) {
for (Method targetMethod : clazz.getMethods()) {
String sourceMethodName = sourceMethod.getName();
String targetMethodName = targetMethod.getName();
String startWithGet = "get";
String startWithIs = "is";
boolean methodGet = sourceMethodName.startsWith(startWithGet);
boolean methodIs = sourceMethodName.startsWith(startWithIs);
if ((methodGet || methodIs)
&& !sourceMethodName.equals("getClass")) {
if (sourceMethodName.equals(targetMethodName)) {
Object sourceResult = MethodHandles.lookup().findVirtual(clazz, sourceMethodName,
MethodType.methodType(sourceMethod.getReturnType())).invoke(source);
Object targetResult = MethodHandles.lookup().findVirtual(clazz, targetMethodName,
MethodType.methodType(targetMethod.getReturnType())).invoke(target);

if (sourceResult != null && !sourceResult.equals(targetResult)) {
EditHistory skuEditHistory = new EditHistory();
editHistory.setEditField(sourceMethodName.substring(methodGet ? startWithGet.length() : startWithIs.length()));
editHistory.setOriginValue(sourceResult.toString());
editHistory.setChangedValue(targetResult != null ? targetResult.toString() : null);
editHistory.setCreatedId(userId);
editHistoryList.add(editHistory);
}
}
}
}
}

return editHistoryList;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}


'java' 카테고리의 다른 글

java script 숫자 또는 영문자만 허용하기  (0) 2015.03.19
바코드 체크 비트 검증 Java 코드  (0) 2015.03.06
JVM에 관한 토론 기록  (0) 2015.01.21
Java MethodHandle Example (Java 1.7)  (0) 2014.10.07
Java reflection example.  (0) 2014.10.07

JVM (Visualizer eclipse plugin 사용시 보기편함)

참고자료 http://www.slideshare.net/javajigi/java-virtual-machine-call-stack-java-byte-code

  • JVM Stacks (N) - Call Stack
    • Operand Stack
      • 자바는 cpu 레지스터를 사용하지 않고 Operand Stack을 사용한다.
      • 논리적으로 연산을 하는데 물리적 연산을 하는 C보다 가끔 빠른 이유는?
        • 내부적으로 컴파일을 했을때 native code로 변환을 하면 최적화가 일어난다.
        • VM에서 외부 시스템에 호출할때 최적화된 CPU레지스터를 사용한다.
        • C로 짜면 성능 최적화가 어렵다. Java로 최적화를 잘하면 역전 현상도 일어난다.
        • Java byte code -> JVM Interpreter Code
        • C byte code -> H/W Code
        • Java는 메모리 매니지먼트가 빠르다
          • 자바는 핫스팟(GC)관리가 메모리 관리에 뛰어나다.
        • C는 malloc
          • 파편화, 쓰레드 세이프, 직렬화
          • 메모리를 빈번하게 생성하면 성능이 떨어진다.
    • Local Variables
    • 호출했다 빠졌다가 하기때문에 Call Stack
    • 명령을 받았을때 실행되기 때문에 Operand Stack
    • opcode
      • dup
        • 메소드를 호출할때 마다 dup opcode를 호출한다 (관례)
      • invoke special 구상메소드에 호출메소드를 알고 있을때 사용하는 호출방식
        • 모든 메소드의 첫번째 인자는 this 이다.
      • load opcode
        • stack에 값을 넣는다.
        • 값을 빼서 처리 후 store로 stack에 넣는다.
      • iconst
      • invokevirtual 다형성이 지원되지 않는다.
        • 런타임에 어떤 오브젝트를 호출할지 결정된다.
      • store opcode
        • stack에서 값을 뺀다.
        • local 변수로 돌려준다.
  • JVM Heap (1)
    • 모두가 공유하는 메모리이다.
  • Method Area (1)

 

주요 주제

  • 어노테이션의 특성
    • 컴파일이 될때 사용처를 알려주어 컴파일
    • 리플렉션 사용을 위한 마킹을 해놓는다.
  • 롬복 Lombok
    • 어노테이션으로 마킹하여 필요한 메소드를 바이트 코드를 적용하여 생성한다.
    • JavaOne Lambda: A peek under the hood (람다 초기 자료)
    • From Lambdas to Bytecode - Brian Goetz
    • 익명 클래스와 다른점은?
      • 익명클래스로 하던일을 쉽게 한다.
    • 익명 클래스로 하던 패턴을 바로 쓰겠다.
    • 메소드 하나만 있는 인터페이스 -> 람다적용 대상이 된다.
      • SAM (Single Abstract Method) -> Functional Abstract Method

  • invoke dynamic
    • 특징
      • 다형성 x
      • 어떤클래스를 사용하는지 런타임에 결정된다
    • 순서
      • indy botstrap 메소드 호출
        • 어떤 bootstrap메소드를 호출할지는 컴파일타임에 정해진다.
      • Callsite 라는 값을 반환한다.
        • 무엇을 호출할지 값을 가지고있다
      • bootstrap 을 통해 callsite 값 을 받은후에는 callsite 메소드를 호출
    • 장점 
      • 펌젬이오버플로우 되는 문제를 해결
      • 첫실행에는 시간이 조금 더걸리나, 첫실행후에는 빠르다. 
      • 리플렉션에 비해 절반정도 빠르다.

  • Method Handle 
    • 최종적으로 Functional Interface 로 Mapping 된다.
    • 어떤메소드를 호출할지 알려준다.
  • 람다 표현식에서 Invoke dynamic의 역할
    • 람다가쓰이는곳에 Invoke dynamic 방식으로 쓰고,
    • bootstrap 이 Method Factory 를 반환한다.
    • 런타임 중 4가지 방법중에 최적화된 방법을 찾아 수행한다.
  • 키워드
    • Lambda
    • Inner class
    • Proxy
    • Method handle
    • Invoke virtual
      • 다형성
    • Invoke interface
      • 다형성
    • invoke static
      • 다형성 x
  • Lambda 의 동작 방식
    • 한번 더 절차를 만들어 desugar
    • 람다를 람다클래스를 사용하는 멤버변수를생성
    • 코드를 Invoke dynamic 으로 생성
    • 메소드 팩토리(부트스트랩)를 생성
    • callsite를 생성하여 4가지 방법 중 가장 좋은것으로 생성
  • 정보
    • 자바닷컴은 내년초에 자바8로 바뀌게된다. (대중화가 이뤄지는 시기가 아닐까 하는 관측)

 


Class clazz : 클래스 변수

List<String> methodList : 메소드 리스트

Object source : 메소드를 호출할 객체

CustomMethodInfo : 메소드 정보를 담은 객체


for (CustomMethodInfo methodInfo : methodInfoList) {

MethodHandle methodHandle = MethodHandles.lookup().findVirtual(clazz, changeAccessibleMethodName(methodInfo.getMethodName()),

MethodType.methodType(methodInfo.getMethodClass()));

Object result = methodHandle.invoke(source);

}



Class clazz : 클래스 변수

List<String> methodList : 메소드 리스트

Object source : 메소드를 호출할 객체

                       try {

for (String methodStr : methodInfoList) {

Object result = clazz.getMethod(methodStr).invoke(source);

}

} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {

log.error("error." + e.getMessage());

throw new Exception("");

}

'java' 카테고리의 다른 글

JVM에 관한 토론 기록  (0) 2015.01.21
Java MethodHandle Example (Java 1.7)  (0) 2014.10.07
Java String equals / == 속도 비교  (0) 2014.07.14
Java 8 신기능 정리 - Stream  (0) 2014.07.02
Java 8 신기능 정리 - Default Method  (0) 2014.07.02


0. 구조
 - AbstractJavaSamplerClient 상속받은(extends) 클래스 를 기준으로 테스트 생성
 - SampleResult의 sampleStart() / sampleEnd() 메소드로 측정
 
1. 빌드
 1.1 프로젝트 우클릭 > Export > Runnable JAR file > Copy required libraries ~ 선택 > 생성

2. 배포
 2.1 생성된 라이브러리 폴더, jar 파일을 apache-jmeter-2.9\lib\ext\ 복사

3. 테스트
 3.1 jmeter.properties 수정 - 159 라인 remote_hosts= 수정 (원격으로 실행시킬 jmeter PC ip)
 3.2 jmeter-server.bat 실행
 3.3 jmeterw.cmd 실행(Master만)
 3.4 시작 및 모니터링


'java > test' 카테고리의 다른 글

GCM 테스트 쉘 스크립트  (0) 2014.05.12
jmeter 테스트 - JavaRequest, Remote Start  (0) 2013.04.15
jmeter 테스트 - 생성  (0) 2013.04.15
기본지식 :
equals 는 데이터 자체를 비교한다.
== 는 주소값을 비교한다.

즉 아래의 "hello" 같은 고정된 값을 연속적으로 비교했을때는 당연히 ==가 빠르다.
new String("hello") 으로 비교하면 의도치 않은 연산을 했을 것이다. 


결론 : 
==가 빠르다. 주소값 비교이므로 조심해서 사용하자.



출력값
Sample Count : 1000000
== is slow: 33077
equals is slow: 51856
same: 915067
Result : == is Win. 63.786254242517735 %



  1. public class CompareEqualsSpeed {
  2.     private void compareSpeed(int count) {
  3.         int countCompare = 0;
  4.         int countEquals = 0;
  5.         long start = 0;
  6.         long end = 0;
  7.         int same = 0;
  8.         if (count < 0) {
  9.             return;
  10.         }
  11.         for (int i = 0; i < count * 2;) {
  12.  
  13.             start = System.nanoTime();
  14.             if ("Hello" == "Hello") {
  15.                 i++;
  16.             }
  17.             end = System.nanoTime();
  18.             long compare = end - start;
  19.  
  20.             start = System.nanoTime();
  21.             if ("Hello".equals("Hello")) {
  22.                 i++;
  23.             }
  24.             end = System.nanoTime();
  25.             long equals = end - start;
  26.  
  27.  
  28.             if (compare > equals) {
  29.                 countCompare++;
  30.             } else if (equals > compare) {
  31.                 countEquals++;
  32.             } else {
  33.                 same++;
  34.             }
  35.         }
  36.  
  37.         System.out.println("Sample Count : " + count);
  38.         System.out.println("== is slow: " + countCompare);
  39.         System.out.println("equals is slow: " + countEquals);
  40.         System.out.println("same: " + same);
  41.  
  42.         String result = "Result : ";
  43.         System.out.println((countCompare < countEquals)
  44.                 ? (result += "== is Win. " + (double) ((double) countCompare / (double) countEquals) * 100) + " %"
  45.                 : (result += "equals is Win" + (double) ((double) countEquals / (double) countCompare) * 100) + " %");
  46.     }
  47.  
  48.     public static void main(String... args) {
  49.         CompareEqualsSpeed main = new CompareEqualsSpeed();
  50.         main.compareSpeed(1000000);
  51.     }
  52. }




Stream 

 - 병렬성 (parallel 연산자)을 가질 수 있다.

 - 병렬처리가 가능하므로 기존의 방법보다 빠르다.

 - stream 은 내부적으로 지연된 실행을 원칙으로 한다.

 -> 실제 연산을 하기 전 까지는 수행하지 않는다.

 -> 지연된 연산

 - 생성되는 모든 객체는 불변객체가 아니라 새로 생성해서 사용한다.

 - 병렬 객체의 이슈에 대해 대응이 가능하다.


  1. public class Ex4 {
  2.  
  3.         public static void main(String[] args) throws IOException {
  4.                 String contetns = new String(Files.readAllBytes(Paths.get("text.txt")), StandardCharsets.UTF_8);
  5.                 List<String> word = Arrays.asList(contetns.split("[\\P{L}}]+"));
  6.                
  7.                 int count = 0;
  8.                 for(String w : word) {
  9.                         if(w.length() > 10) {
  10.                                 count++;
  11.                         }
  12.                 }
  13.                
  14. //              long lc = word.stream().filter(w->w.length() > 10).count(); // 파이프라인을 통해 사용. C#LINQ
  15.                 long lc = word.parallelStream().filter(w->w.length() > 10).count(); // 파이프라인을 통해 사용. C#LINQ
  16.                 Stream<String> list =  word.stream().map(String::toLowerCase);
  17.                 Stream<Character> firstChar=  word.stream().map(->s.charAt(0));              
  18.                 System.out.println(count + lc);
  19.                 list.forEach(System.out::println);
  20.                 firstChar.forEach(System.out::println);
  21.         }
  22.  
  23. }


Default Method 와 추상 메소드와 다른점

 - 추상 클래스는 필드를 가질 수 있다.

 - 자식은 상속받은 필드를 사용 할 수 있다.


Deafult Method

 - 기본 연산자를 가질 수 있다.

 - static 표현 또 한 가능하다.

 


  1. interface IFoo {
  2.         void foo();
  3.         default void goo() { // 기본 연산자를 가질수 있다.
  4.                 // 추상클래스는 필드를 가질 수 있다. (자식이 상속받은 필드를 사용가능하다.)
  5.                 System.out.println("G");
  6.         }
  7.         static void sfoo() { // 정적 메소드 표현도 가능
  8.                 System.out.println("...");
  9.         }
  10. }
  11.  
  12. // Array, Arrays
  13. // Collection, Collections 정적 메소드 추가
  14.  
  15. class A implements IFoo { // goo와 sfoo를 사용가능
  16.  
  17.         @Override
  18.         public void foo() {
  19.                 System.out.println("A");
  20.         }
  21.  
  22. }
  23. class B implements IFoo {
  24.  
  25.         @Override
  26.         public void foo() {
  27.                 System.out.println("B");
  28.                
  29.         }
  30.        
  31. }
  32. public class Ex3 {
  33.  
  34.         public static void main(String[] args) {
  35.         }
  36.  
  37. }


+ Recent posts