Improve closing of files in error situations.
[egit/imyousuf.git] / org.spearce.jgit.test / exttst / org / spearce / jgit / lib / SpeedTestBase.java
blob36a5e0e6335b82fb3131d22af511eb46461fa1ad
1 /*
2 * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2007, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
9 * conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
22 * written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org.spearce.jgit.lib;
41 import java.io.BufferedReader;
42 import java.io.File;
43 import java.io.FileReader;
44 import java.io.IOException;
45 import java.io.InputStream;
47 import junit.framework.TestCase;
49 /**
50 * Base class for performance unit test.
52 public abstract class SpeedTestBase extends TestCase {
54 /**
55 * The time used by native git as this is our reference.
57 protected long nativeTime;
59 /**
60 * Reference to the location of the Linux kernel repo.
62 protected String kernelrepo;
64 /**
65 * Prepare test by running a test against the Linux kernel repo first.
67 * @param refcmd
68 * git command to execute
70 * @throws Exception
72 protected void prepare(String[] refcmd) throws Exception {
73 try {
74 BufferedReader bufferedReader = new BufferedReader(new FileReader("kernel.ref"));
75 try {
76 kernelrepo = bufferedReader.readLine();
77 bufferedReader.close();
78 timeNativeGit(kernelrepo, refcmd);
79 nativeTime = timeNativeGit(kernelrepo, refcmd);
80 } finally {
81 bufferedReader.close();
83 } catch (Exception e) {
84 System.out.println("Create a file named kernel.ref and put the path to the Linux kernels repository there");
85 throw e;
89 private static long timeNativeGit(String kernelrepo, String[] refcmd) throws IOException,
90 InterruptedException, Exception {
91 long start = System.currentTimeMillis();
92 Process p = Runtime.getRuntime().exec(refcmd, null, new File(kernelrepo,".."));
93 InputStream inputStream = p.getInputStream();
94 InputStream errorStream = p.getErrorStream();
95 byte[] buf=new byte[1024*1024];
96 for (;;)
97 if (inputStream.read(buf) < 0)
98 break;
99 if (p.waitFor()!=0) {
100 int c;
101 while ((c=errorStream.read())!=-1)
102 System.err.print((char)c);
103 throw new Exception("git log failed");
105 inputStream.close();
106 errorStream.close();
107 long stop = System.currentTimeMillis();
108 return stop - start;