Friday, 30 September 2016

spring

1. @componentScan
  • ask spring to scan @component/@configuration/@service class in specified base package

2. @configuration
  • tell spring that this class defines one or more @bean
  • spring will load beans into it’s context before we even request it
  • this is to make sure all the beans are properly configured and application fail-fast if something goes wrong
  • the default scope of spring beans is singleton, meaning that the bean will be created only once for the whole application

3. @component
  • spring will: scan our application for classes annotated with @Component. 
  • spring will: instantiate them and inject any specified dependencies into them

4. @autowired
  • auto dependency injection

5. core problem that spring resolve - DI

before
@RestController
public class WelcomeController {
    private WelcomeService service = new WelcomeService();
    @RequestMapping("/welcome")
    public String welcome() {
        return service.retrieveWelcomeMessage();
    }
}

after
@Component
public class WelcomeService {
    //Bla Bla Bla
}
@RestController
public class WelcomeController {
    @Autowired
    private WelcomeService service;
    @RequestMapping("/welcome")
    public String welcome() {
        return service.retrieveWelcomeMessage();
    }
}

6. Instantiate bean
  • explicitly - use applicationContext 
  • implicitly - use autowire 

7. spring use jackson json library to marshal object into json

8. yaml file

reference
1. sprint boot vs. spring mvc vs. spring
2. yaml file example

Tuesday, 6 September 2016

unix

1. vi
  • move within line: 0(begin), $(end), w(next), b(previous)
  • move within file: 1G(begin), G(end), nG(line) 
  • move between screen: ctrl+f(forward), ctrl+b(back), ctrl+d(forward half), ctrl+u(back half)
  • u(undo) 
  • edit: i(insert), x(delete), o(insert line before), O(insert line after), d0(delete from 0), d$(delete to $), dd(delete line), ndd(delete n line), yy(copy line), nyy(copy n line), p(paste),
  • search: /(search forward), ?(search backward), n(next forward), N(next backward)
  •  replace: :%s/text1/text2/g
  • line number: ^g(show current line number and total line number), :set number, :set nonumber, :1,10y(copy line 1-10)

2. ls
  • ls -altr  //a=all, l=long_format, t=sort_by_time, r=reverse_order

3. less
  • F (become tail -f)
  • ctrl+g (show statistics)
  • v (edit)
  • h (help)
  • -N (display line number)
  • G (go to end of file)
  • g (go to start of file)

4. grep
  • grep -n pattern file

5. cron

6. get information of linux machine
  • primary ip address: /sbin/ifconfig -a
  • OS version: uname -a
  • ps -ef | grep proc_name
  • hostname ip conversion: nslookup [address] [hostname]

7. ssh tunnelling
       ~/.ssh/config

       host wstasapp016
       LocalForward 50404 10.200.10.222:1521

       to test: ssh -p 50404 localhost

8. keep console busy
  • prstat
  • while sleep 300; do echo -ne "\a"; done &

9. sudo and su
  • sudo     //ask for root permission
  • su         //switch to another user
  • sudo su - user  //open a login shell as user

10. shell argument
  • "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}
  • "$#" is the number of positional parameters
  • "$0" is the name of the shell or shell script
  • -z string true if the string is null
  • -n is the opposite of -z
  • foo=${variable:-default}  # If variable not set or null, use default.

11. ping -c 3                               //ping with 3 packet


reference
1. setup ssh tunnel using putty
2. ssh tunnelling using config file
3. dollar sign shell variable
4. linux test command

Monday, 5 September 2016

regex

1. examples
  • regexr.com                 //playground
  • /pattern/g                   //global
  • /pattern/                     //1st occurrence
  • [enl]                           //individual match of e n l
  • [^enl]                         //individual match except e n l
  • [a-z]
  • [A-Z]
  • [0-9]
  • .                                  //everything except newline
  • \n                                //new line
  • \w                               //word char (including digits), 1 char is 1 match
  • \W                              //non word char
  • \d                                //digit, 1 digit is 1 match
  • \D                               //non digit
  • \s                                //white space
  • \S                               //non white space
  • [\s\S]                          //everything including newline
  • [^i]                             //i as 1st char in 1st line
  • [^i]/m                         //i as 1st char in multi line
  • \.                                 //period
  • (old)                           //capture group that select 'old'
  • (?:old)                        //non capture group that select 'old'
  • \1 or $1                      //1st capture group
  • g(?=old)                     //look ahead, 'g' that's followed by 'old'
  • g(?!old)                      //negative look ahead, g' that's not followed by 'old'
  • +                                 //one or more of a pattern
  • *                                 //zero or more of a pattern
  • ?                                 //zero or one of a pattern
  • {3}                             //3 copies of a pattern
  • {3,}                            //3 or more copies of a pattern
  • {3,4}                          //3 or 4 copies of a pattern


greedy vs lazy
greedy:
<.+>                                              //matches <em>Hello World</em>
\d+                                                //matches 12345

lazy
<.+?>                                            //? after + tells it to repeat as few as possible and matches <em>
\d+?                                              //matches 1

special character
  • ^ $                                       //begin end of line
  • \< \>                                    //begin end of word in vim
  • \                                          //give special meaning to next character
  • ?                                         //change preceding quantifier to lazy
  • /                                          //regex delimiter, mark the beginning and end of regex
common pattern
  • \b                                        //word boundary
  •  .|\n                                                       //any character or new line
  • <tag[^>]*>((.|\n)*?)<\/tag>                 //html tag
  • :%s/\([a-zA-Z]*\):/"\1":/g                    //search and replace through out whole file
  • :%s/'/"/g
assertion
  • is a condition that needs to be met for preceding or following characters, but is NOT part of the regex match result

in java
  • \\                                         //\ is special character in java, need to escape it

online playground: https://regex101.com/
best tutorial: i-wanna-use-regex-but-what-does-it-all-mean


vi search and replace example
  • :%s/old/new/g                           //all lines
  • :s/old/new/gi                             //current line, ignore case
  • :s/\<old\>/new/                         //old is a whole word
  • %s:/g:abc:g                               //special char, use any char as delimit, in this case :
  • :g/^baz/s/foo/bar/g                   //change in each line starting with 'baz'
when searching:

., *, \, [, ^, and $ are metacharacters.
+, ?, |, &, {, (, and ) must be escaped to use their special function


reference
1. best regex website
2. good article explaining regex in javascript 
3. java regular expression
4. rexegg tutorial
5. greedy and lazy match
6. difference between :g and :%s commands in vim
7. regex online
8. i-wanna-use-regex-but-what-does-it-all-mean

Friday, 8 July 2016

jmeter

1. terms
  • thread group (user)
  • ramp-up period (defines concurrency behaviour, if 0, means start all users immediately; if 5 users in 5 sec, then interval between each user is 1 sec, normally there's ramp-up, a flat top where all users running, and a ramp-down)

  • sampler (request definition, http request is just one type of request, there're others)
  • http request defaults (default http request properties)
  • http cookie manager (cookie is shared between all http requests of a thread)
  • header manager (control things like user agent, application/json)

  • timer (add wait before each request)
  • synchronising timers (all threads are blocked until a specified number of threads is reached)
  • throughput shape timer (define expected throughput)

2. how jmeter works
  • when a test plan is started, Jmeter kicks off a thread per virtual user
  • each thread starts executing samplers (request) in sequential order (or according to the Logic Controllers, if any) as fast as it can
  • when no samplers are left to execute and no loops to iterate the thread is being shut down
  • timer is to add wait in between the samplers

3. if multiple users concurrent run, it's better to have ramp-up time or using synchronizing timer.
  • because when the earliest thread finish, it will try to grab the 1st usrid/passwd and run
  • at this time, the previous thread that use this usrid/passwd may not finish yet

4. install plugin
  • download plugin manager jar file and put the jar file under lib/ext directory, use it to install other plugins, or brew install jmeter will automatically install plugin manager
  • on mac, use 'open $path/jmeter/bin/jmeter' to open jmeter
  • check java version when encounter problem with plugin manager

5. generate dashboard from jtl
  • ui listener such as view result tree
  • headless listener such as simple data writer (non-ui mode, fast less memory, large # of concurrent users)
  • both can write to log files like jtl

6. different ramp up (ultimate thread group plugin)
  • linear
  • step
  • spike

7. use certificate
  • jmeter -n -t uhl.jmx -l testresults.jtl -D javax.net.ssl.keyStore=nonprod.p12 -D javax.net.ssl.keyStorePassword=password
  • from ui, options -> ssl manager

8. taurus

9. vars vs props vs $
  • props has global scope, vars is limited to current thread group, $ is limited to current sampler

10. multi-thread
  • user login thread, run once, save token and cookie
  • user action thread, add cookie to cookie manager
  • each login thread and corresponding action thread use its own set of cookie

11. jmeter UI
  • cookie manage: store cookie from each response and use in next request
  • click logviewer icon can see jmeter logs
  • view result tree request tab: raw tab can see the cookie data, http tab can see decoded data
  • view result tree response tab: response body if html can render it to webpage to view, response header can see the 'set-cookie', i.e. new cookie that's set to next request 

12. cookie manager
  • cookies and added from server via response header, e.g. set-cookie: ebproxy=amp1
  • cookie follows domain and path matching rule, cookie manager only send cookies that match domain and path of current request

13. jmeter.properties
  • CookieManager.allow_variable_cookies=true
  • CookieManager.save.cookies=true


reference
1. jmeter 2 min tutorial
2. using jmeter timer
4. top 10 jmeter plugins
5. generate dashboard report from gui
6. rest api testing with jmeter
7. extract data from json response using jmeter
8. how to use JMeter for performance & load testing
9. how to choose number of threads for jmeter plan
10. jmeter-ramp-up-the-ultimate-guide
11. generate timestamp in jmeter
12. how-to-run-jmeter-in-non-gui-mode
13. 3-easy-ways-to-monitor-jmeter-non-gui-test-results
14. how-to-analyze-jmeter-results
15. how-set-your-jmeter-load-test-use-client-side-certificates

Friday, 19 February 2016

maven

1. surefire-plugin vs. failsafe-plugin
  • surefire-plugin is for running unit test, if any test fail it will fail the build immediately
  • surefire-plugin can use <testFailureIgnore>true</testFailureIgnore>  to run all test
  • failsafe-plugin is for running integration test, it will run all tests and fail at verify stage
  • surefire-plugin use "mvn test" and failsafe-plugin use "mvn verify"

2. mvn eclipse:eclipse (obsolete)
  • this command will read pom.xml and generate metadata for eclipse
  • run 'mvn eclipse:eclipse' to update eclipse project, then refresh project
  • afterwards you can use eclipse 'import existing project' to import a maven project
  • this command is no longer needed with latest version of eclipse, where you can import 'exisitng maven project'

3. add oracle jdbc driver to maven
  • due to oracle license restriction, there is NO public maven repository provides oracle jdbc driver
  • you have to install it manually into your maven local repository

4. groupid, artifactid, version, packaging
  • groupid is domain name, e.g. com.example.foo
  • artifactid is name of artifact (jar or war or other executable file)
  • when no packaging is declared, maven assume default is 'jar'

5. standard alone maven and maven in eclipse
  • they will use same repo in ~/.m2 folder, if artifact is download by one, it will not be downloaded by the other
  • if seeing 'could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved' error, run 'mvn compile', if it works, it's maven setting issue in eclipse

6. settings.xml
  • global settings: ${maven.home}/conf/settings.xml
  • user settings: ${user.home}/.m2/settings.xml
  • by default user settings is not created
  • if both exist, a merge of global and user will be used by standalone maven
  • in both m2eclipse plugin and intellij maven plugin, user can specify the setting to use

7. dependency vs. module
  • dependency is library need to build the project
  • module means when you build main project, sub module project will be built too

8. project lombok
  • reduce boilerplate code for model/data object
  • generate getter/setter automatically by adding annotations

9. parent tag in pom.xml
  • maven read parent pom content and concatenate with current pom

10. access maven command line argument from java class
  • -Dexec.args
  • you can use System.getProperty("exec.args") to get any environment variable set on command line with the flag -D

11. plugin
  • define an 'action'
  • has 'groupId', 'artifactId' and 'version'

12. intellij
  • intellij has maven plugin bundled by default
  • in run/debug configuration, can set maven command line parameter
  • maven option on the right hand side of intellij
  • open settings.xml in intellij by right click in the pom.xml file -> maven ->  open 'settings.xml'
  • note that brew install maven's settings.xml location is different from default intellij' settings.xml location (~/.m2/settings.xml)
  • sometimes after mvn clean install, it is still showing package missing, and in 'project structure -> project settings -> modules', the dependency modules are missing too. then you can click the refresh button in maven tab to 'reimport all maven projects'

13. mvn -v    //show maven home details


reference

Friday, 5 February 2016

Database

1. connect to local sql server from DBVisualizer
  • find host name for sql server
  1. localhost or under cmd window type host to find out
  • find port number for sql server
  1. open sql server configuration manager
  2. sql server network configuration -> protocols for (dbinstance) -> enable tcp/ip -> properties -> ipall (can see the port number, change to default 1433)
  3. restart server

references:
1. solving sql server connection problem