Project

General

Profile

1
/*  '$RCSfile$'
2
 *  Copyright: 2010 Regents of the University of California and the
3
 *              National Center for Ecological Analysis and Synthesis
4
 *  Purpose: To test the Access Controls in metacat by JUnit
5
 *
6
 *   '$Author: berkley $'
7
 *     '$Date: 2010-05-03 14:26:08 -0700 (Fri, 14 Aug 2009) $'
8
 * '$Revision: 5027 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat;
26

    
27
import java.util.*;
28
import java.io.*;
29

    
30
import edu.ucsb.nceas.MCTestCase;
31
import edu.ucsb.nceas.metacat.MetacatProfiler.Profile;
32
import junit.framework.Test;
33
import junit.framework.TestSuite;
34

    
35

    
36
/**
37
 * A JUnit test for testing profiler
38
 */
39
public class MetacatProfilerTest extends MCTestCase 
40
{   
41
    private int createCount = 0;
42
    
43
    /**
44
    * consstructor for the test
45
    */
46
    public MetacatProfilerTest(String name)
47
    {
48
        super(name);
49
    }
50
  
51
    /**
52
	 * Establish a testing framework by initializing appropriate objects
53
	 */
54
	public void setUp() throws Exception 
55
	{
56
		super.setUp();
57
	}
58

    
59
	/**
60
	 * Release any objects after tests are complete
61
	 */
62
	public void tearDown() 
63
	{
64
	}
65

    
66
	/**
67
	 * Create a suite of tests to be run together
68
	 */
69
	public static Test suite() 
70
	{
71
		TestSuite suite = new TestSuite();
72
		suite.addTest(new MetacatProfilerTest("testProfiling"));
73
		suite.addTest(new MetacatProfilerTest("testSort"));
74
		return suite;
75
	}
76
	
77
	public void testSort()
78
	{
79
	    MetacatProfiler mp = MetacatProfiler.getInstance();
80
	    mp.reset();
81
        mp.startTime("test1");
82
        sleep(100);
83
        mp.stopTime("test1");
84
        mp.startTime("test2");
85
        sleep(200);
86
        mp.stopTime("test2");
87
        mp.startTime("test3");
88
        sleep(500);
89
        mp.stopTime("test3");
90
        mp.startTime("test4");
91
        sleep(150);
92
        mp.stopTime("test4");
93
        mp.startTime("test3");
94
        sleep(150);
95
        mp.stopTime("test3");
96
        try
97
        {
98
          mp.printSortedCSV(null, "total");
99
          System.out.println("");
100
          mp.printSortedCSV(null, "callorder");
101
          System.out.println("");
102
          mp.printSortedCSV(null, "callcount");
103
        }
104
        catch(Exception e)
105
        {
106
            e.printStackTrace();
107
            fail("error printing sorted csv");
108
        }
109
	}
110
	
111
	/**
112
	 * insert an invalid document and make sure create fails and does not
113
	 * leave any artifacts behind
114
	 */
115
	public void testProfiling()
116
	{
117
	    MetacatProfiler mp = MetacatProfiler.getInstance();
118
	    mp.startTime("test1");
119
	    sleep(100);
120
	    mp.stopTime("test1");
121
	    Hashtable<String, Profile> h = mp.getProfiles();
122
	    Profile p = h.get("test1");
123
	    System.out.println("p: " + p.toString());
124
	    assertTrue(p.total >= 100 && p.total < 105);
125
	    assertTrue(p.methodcalls == 1);
126
	    
127
	    
128
	    mp.reset();
129
	    mp.startTime("test2");
130
	    sleep(200);
131
	    mp.stopTime("test2");
132
	    mp.startTime("test2");
133
	    sleep(100);
134
	    mp.stopTime("test2");
135
	    mp.startTime("test3");
136
	    sleep(50);
137
	    mp.stopTime("test3");
138
	    h = mp.getProfiles();
139
	    assertTrue(h.size() == 2);
140
	    p = h.get("test2");
141
	    System.out.println("p: " + p.toString());
142
	    assertTrue(p.total >= 300 && p.total < 310);
143
	    assertTrue(p.methodcalls == 2);
144
	    Profile p2 = h.get("test3");
145
	    System.out.println("p2: " + p2.toString());
146
	    assertTrue(p2.total >= 50 && p2.total < 55);
147
	    assertTrue(p2.methodcalls == 1);
148
	}
149
	
150
	private void sleep(long millis)
151
	{
152
	    try
153
	    {
154
	        Thread.currentThread().sleep(millis);
155
	    }
156
	    catch(Exception e)
157
	    {
158
	    }
159
	}
160
}
    (1-1/1)