#!/bin/bash
#
# Copyright (c) Members of the EGEE Collaboration. 2004-2010.
# See http://www.eu-egee.org/partners/ for details on the copyright holders.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

for i in /tmp/test_ORG /tmp/test_ORG.LB_BUF /tmp/test_ORG.LB_BUF_LB_DAG_EMBRIONIC /tmp/test_ORG.LB_DAG_EMBRIONIC /tmp/test_ORG.LB_BUF_LB_DAG_EMBRIONIC_*[0-9] /tmp/test_ORG.LB_BUF_LB_DAG_EMBRIONIC_10; 
#for i in /tmp/test_ORG.LB_BUF_LB_DAG_EMBRIONIC_10; 
do 

	#cat $i | grep 1000| sed 's/(//'| awk 'BEGIN {a=8002}; // {print a " "  $8 " " sqrt(500000/a); a=a+16004}' > $i.plot; 
	lines=`cat $i | wc -l| sed 's/ //g'`
	cat $i | grep 1000| sed 's/(//'| awk -v lines=$lines 'BEGIN {a=500}; // {print a " "  $8 " " lines*1000/a; a=a+1000}' > $i.plot; 
#	cat $i | grep 1000| sed 's/(//'| awk -v lines=$lines 'BEGIN {a=500}; // {print a " "  $8 " " 1; a=a+1000}' > $i.plot; 


cat <<EOF >/tmp/plot.gp
f(x) = a*log(x+b) + c
a=0
b=1
c=0
fit f(x) '$i.plot' using 1:2:3 via a, b, c
print "f(100000) = ", f(100000)
EOF

a=`gnuplot /tmp/plot.gp 2>&1| awk '/^a.*=.*%/ {print $3}'`
b=`gnuplot /tmp/plot.gp 2>&1| awk '/^b.*=.*%/ {print $3}'`
c=`gnuplot /tmp/plot.gp 2>&1| awk '/^c.*=.*%/ {print $3}'`
f100000=`gnuplot /tmp/plot.gp 2>&1| awk '/^f\(100000\)/ {print $3}'`


cat <<EOF >/tmp/plot_final.gp
f(x) = a*log(x+b) + c
a=0
b=1
c=0
fit f(x) "$i.plot" using 1:2:3 via a, b, c
set xlabel "Number of jobs in DB"
set ylabel "Millions of registrations per day"
set title "Megajob challenge performance measurement"
set out "$i.ps"
set terminal postscript landscape "Helvetica" 14 
plot '$i.plot', f(x) title "f(x) = a * log(x+b) + c   [a=$a  b=$b  c=$c] ",\
	f(100000) title "f(100000) = $f100000"
print f(100000)
print f(1000000)
print f(10000000)
EOF

gnuplot /tmp/plot_final.gp

done
