Code in the book-Write your own test

Source: Internet
Author: User
Tags addall

Package Com.fenghao.loadbalancing;import Java.util.arraylist;import Java.util.hashmap;import java.util.Iterator; Import java.util.list;import java.util.map;import java.util.random;import java.util.set;import org.junit.Test;/** * *  <P> load Balancing algorithm simple write </P> * @ClassName: loadTest * @author Feng Hao November 9, 2016 morning 9:12:49 * @see TODO */public class LoadTest {Private final map<string,integer> serverweightmap=new hashmap<string,integer> ();p ublic loadTest () { Serverweightmap.put ("192.168.1.100", 1), Serverweightmap.put ("192.168.1.101", 1); Serverweightmap.put (" 192.168.1.102 ", 4); Serverweightmap.put (" 192.168.1.103 ", 1); Serverweightmap.put (" 192.168.1.104 ", 1); Serverweightmap.put ("192.168.1.105", 3), Serverweightmap.put ("192.168.1.106", 1); Serverweightmap.put (" 192.168.1.107 ", 2); Serverweightmap.put (" 192.168.1.108 ", 1); Serverweightmap.put (" 192.168.1.109 ", 1); Serverweightmap.put ("192.168.1.110", 1);} @Testpublic void Test () {int le=4;//roundrobintest (LE); Randomtest ();} /** * * <p>title:roundrobintest</p> * <p> Polling </p> * @param pos * @return * @author Feng Hao November 9, 2016 PM 7:41:52 */public String Roundrobintest (Integer pos) {map<string,integer> servermap=new hashmap<string,integer> (); Servermap.putall (SERVERWEIGHTMAP); set<string> KeySet = Servermap.keyset (); Arraylist<string> keylist=new arraylist<string> (); Keylist.addall (KeySet); String server=null;synchronized (POS) {if (Pos>=keyset.size ()) {pos=0;} Server=keylist.get (POS);p os++; System.out.println ("\npos is" +pos); System.out.println ("\nserver is" +server); return server;} /** * * <p>Title:RandomTest</p> * <p> Random </p> * @return * @author Feng Hao November 9, 2016 PM 7:49:24 */publ IC String randomtest () {map<string,integer> servermap=new hashmap<string,integer> (); ServerMap.putAll ( SERVERWEIGHTMAP); set<string> KeySet = Servermap.keyset (); Arraylist<string> keylist=new arraylist<string> (); Keylist.addall (KeySet); Random random=new random (); int POS= Random.nextint (Keylist.size ()); System.out.println ("\npos is" +pos); String Server = Keylist.get (POS); System.out.println ("\nserver is" +server); return server;} /** * * <p>Title:HashTest</p> * <p> Source Address hash Method </p> * @author Feng Hao November 10, 2016 morning 8:46:43 */public Str ing hashtest (String remoteip) {map<string,integer> servermap=new hashmap<string,integer> (); Servermap.putall (SERVERWEIGHTMAP); set<string> KeySet = Servermap.keyset (); Arraylist<string> keylist=new arraylist<string> (); Keylist.addall (keySet); int hashcode = Remoteip.hashcode (); int size = Keylist.size (); int pos=hashcode% size; System.out.println ("\npos is" +pos); return Keylist.get (POS);} /** * * <p>Title:weightRoundRobin</p> * <p> weighted Polling </p> * @return * @author Feng Hao November 10, 2016 8:57: */public String weightroundrobin (Integer pos) {map<string,integer> servermap=new Hashmap<string,integer > (); Servermap.putall (SERVERWEIGHTMAP); set<string> KeySet = SERvermap.keyset ();iterator<string> it = Keyset.iterator (); List<string> serverlist=new arraylist<string> (); while (It.hasnext ()) {String server=it.next (); Integer Weight = Servermap.get (server), for (int i = 0; I <weight; i++) {serverlist.add (server);}} String server=null;synchronized (POS) {if (Pos>=serverlist.size ()) {pos=0;} Server=serverlist.get (POS);p os++;} return server;} /** * * <p>Title:WeightRandom</p> * <p> weighted random method </p> * @return * @author Feng Hao November 11, 2016 8:43:33 * /public String weightrandom () {map<string,integer> servermap=new hashmap<string,integer> (); Servermap.putall (SERVERWEIGHTMAP); set<string> KeySet = Servermap.keyset ();iterator<string> it = Keyset.iterator (); List<string> serverlist=new arraylist<string> (); while (It.hasnext ()) {String server = It.next (); Integer Weight = servermap.get (server); for (int i = 0; i < weight; i++) {serverlist.add (server);}} Random random=new random (); int randompos = random.Nextint (Serverlist.size ()); String Server = Serverlist.get (randompos); return server;}}

Code in the book-Write your own test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.