Status: New
Owner: ----

New issue 572 by [email protected]: Performance problem or wrong usage?
http://code.google.com/p/google-guice/issues/detail?id=572

Hello,
this is my first contact mit DI and Guice.
I have a question about the performance from Guice.

The following small JUnit is creating some object with Guice
and by a own factory implementation.

If I measure the object creation (and some setters), Guice is
up to 50 times slower then the own code.

Do I use Guice in a wrong way or is the performance as bad as I have measured?

Uwe
-----
package sandbox.guice.performance;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

public class PerfTest {

        static private final int max = 1000000;
        
        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
        }

        @AfterClass
        public static void tearDownAfterClass() throws Exception {
        }

        @Before
        public void setUp() throws Exception {
        }

        @After
        public void tearDown() throws Exception {
        }
        
        static interface Person {

                public abstract String getFirstName();

                public abstract void setFirstName(String firstName);

                public abstract String getLastName();

                public abstract void setLastName(String lastName);

        }

        static class RealPerson implements Person {
                private String firstName;
                private String lastName;
                @Override
                public String getFirstName() {
                        return firstName;
                }
                @Override
                public void setFirstName(String firstName) {
                        this.firstName = firstName;
                }
                @Override
                public String getLastName() {
                        return lastName;
                }
                @Override
                public void setLastName(String lastName) {
                        this.lastName = lastName;
                }
                
        }
        
        static abstract class PersonFactory {
                static Person createPerson() {return new RealPerson();}
        }

        @Test
        public void testClassicWay () throws Exception {
                long t1 = System.currentTimeMillis();
                for (int i = 0; i < max; i++) {
                        Person person = PersonFactory.createPerson();
//                      person.setFirstName("Guice");
//                      person.setLastName("Google");
//                      
//                      assertEquals(RealPerson.class, person.getClass());
//                      assertEquals("Guice" , person.getFirstName());
//                      assertEquals("Google", person.getLastName());
                }
                long t2 = System.currentTimeMillis();
                System.out.format("%-20s %6d %5dms\n", "ClassicWay", max, 
(t2-t1));
        }

        @Test
        public void testGuiceSimple () throws Exception {
                Injector injector = Guice.createInjector(new Module() {
                        @Override
                        public void configure(Binder binder) {
                                binder.bind(Person.class).to(RealPerson.class);
                        }
                });
                
                long t1 = System.currentTimeMillis();
                for (int i = 0; i < max; i++) {
                        Person person = injector.getInstance(Person.class);
//                      person.setFirstName("Guice");
//                      person.setLastName("Google");
//                      
//                      assertEquals(RealPerson.class, person.getClass());
//                      assertEquals("Guice" , person.getFirstName());
//                      assertEquals("Google", person.getLastName());
                }
                long t2 = System.currentTimeMillis();
                System.out.format("%-20s %6d %5dms\n", "GuiceSimple", max, 
(t2-t1));
        }
}


--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en.

Reply via email to