Blue74 | New and Evolving Projects

Archive for July 2010

I created a PHP class that allows key storage (insert/update/delete/retrieve) that does not use a database, or a tool such as memcache.

If you can use memcache, do so, this was something I put together to use on a system in which I couldn’t use memcache. A simpler file cache may have been more appropriate, bu I prefer that all my key values stay in one file.

It could use a lot of optimizations, however, this is my first working version. It’s GPL’d so, if there is anyone that has a use for it, be my guest!

Usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
include 'cerealDb.php';
$x->collectionBaseDir('/tmp');
$x->useCollection('testing.cdb');
$x->insert("test","1234567890");
echo $x->read("test")."\n";
$x->update("test","0987654321");
echo $x->read("test")."\n";
$x->delete("test");
echo $x->read("test")."\n";
 
 
/**
* cerealDb, Serialized Array Storage
* @author Mike Curry
* @version 1.0
*
* Copyright (C) 2010 Mike Curry
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or any later
* version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
// CerealDb - Serialized array storage
Class cerealDb {
 
// location of storage
private $_collectionBaseDir;
private $_collectionName;
private $_lastWriteDate;
 
public $data = array();
 
public function __construct() {
}
 
public function collectionBaseDir($directory) {
if (!is_dir($directory)) {
echo $directory."\n";
return false;
}
 
if (substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
$directory .= DIRECTORY_SEPARATOR;
}
 
$this->_collectionBaseDir = $directory;
 
return true;
}
 
public function useCollection($collection) {
 
$this->_collectionName = trim($collection);
 
$file = $this->_collectionBaseDir.$collection;
 
// open for write, to create if missing
if ($fhandle = fopen($file, 'a+')) {
if (flock($fhandle, LOCK_EX)) {
if (filesize($file) > 0) {
$data = fread($fhandle, filesize($file));
if (strlen($data)) {
$this->data = unserialize($data);
} else {
$this->data = array();
}
}
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
$this->_lastWriteDate = filemtime($file);
}
 
return true;
}
 
public function insert($key, $data) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only update if it doesn't exist
if (!isset($this->data[$key])) {
$this->data[$key] = $data;
 
// trunicate file, and update
ftruncate($fhandle, 0);
fwrite($fhandle, serialize($this->data));
$result = true;
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
 
public function update($key, $data) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only update if exists
if (isset($this->data[$key])) {
$this->data[$key] = $data;
 
// trunicate file, and update
ftruncate($fhandle, 0);
fwrite($fhandle, serialize($this->data));
$result = true;
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
 
public function delete($key) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only delete if exists
if (isset($this->data[$key])) {
unset($this->data[$key]);
 
// trunicate file, and update
ftruncate($fhandle, 0);
fwrite($fhandle, serialize($this->data));
$result = true;
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
 
public function read($key) {
 
if (strlen($this->_collectionName) == 0 ||
strlen($this->_collectionBaseDir) == 0) {
return false;
}
 
$result = false;
$file = $this->_collectionBaseDir.$this->_collectionName;
 
if ($fhandle = fopen($file, 'r+')) {
if (flock($fhandle, LOCK_EX)) {
 
// refresh file?
if (filemtime($file) != $this->_lastWriteDate) {
if (filesize($file) > 0) {
$this->data = unserialize(fread($fhandle, filesize($file)));
}
}
 
// only return data if exists
if (isset($this->data[$key])) {
$result = $this->data[$key];
}
 
flock($fhandle, LOCK_UN); // release the lock
}
fclose($fhandle);
 
// update file time
$this->_lastWriteDate = filemtime($file);
}
return $result;
}
}>

No tags

We’re happy to announce the launch of our latest start-up FixBurnIn.com. We’ve created a DVD that will fix stuck Pixels, remove Burn-in and sharpen your LCD/Plasma’s screen.

We’ve tested it with 5 LCD televisions, 5 flat screen monitors, and 5 Plasma TV’s that either had several stuck pixels, or serious burn in.

Result: Success! All of the Screens we tested were all restored, all of the stuck pixels were released, and any burn-in that the screens suffered was reset.

Grab a DVD for yourself, or a friend, while shipping is free!

http://www.fixburnin.com/

No tags

First off, I do not want to discount the 30 Day Challenge website, it’s great! However, I don’t have a lot of time these days. What I’ve done, is take the end objective from each day and summarized it. If you have a lot of patience, and want to insure this works for you, I really suggest that you just take the 30 day challenge and stop here. In fact, I am going to continue after the break for those who want to do it right…

This is by no means an alternative way of taking the 30 day challenge, nor am I, or any of my sites, or companies affiliated with the 30 day challenge. I was simply curious about it, and didn’t want to sink 30 days into something without a brief summary.

Enjoy.

(more…)

· · · · · · · · ·

*Original article can be found here.

For a recent edition of the Swiss Computerworld Magazine we listed our Top 10 Performance Problems as we have seen them over the years when working with our clients. I hope this list is enlightening – and I’ve included follow-up links to the blogs to help better understand how to solve these problems:

#1: Too Many Database Calls

The problem we see the most are too many database query per request/transaction. There are 3 specific phenomena to witness

  1. More data is requested is than actually required in the context of the current transaction, e.g.: requesting all account information instead of those that we need to display on the current screen.
  2. The same data is requested multiple times. This usually happens when different components involved in the same transaction act independently from one another and each requests the same set of data. It is unknown what type of data has already been loaded in the current context so we end up with the same queries multiple times.
  3. Multiple queries are executed to retrieve a certain set of data. This is often a result of not taken full advantage of complex SQL statements or stored procedures to retrieve the data in one batch.

Further Reading: Blog on Linq2Sql Performance Issues on Database, Video on Performance Anti-Patterns

#2: Synchronized to Death

There is no question that synchronization is necessary to protect shared data in an application. Too often developers make the mistake to over-synchronize, e.g.: excessively-large code sequences are synchronized. Under low load (on the local developers workstation) performance won’t be a problem. In a high-load or production environment over-synchronization results in severe performance and scalability problems.

Further Reading: How to identify synchronization problems under load

#3: Too chatty on the remoting channels

Many libraries out there make remote communication seem like a piece of cake. There is hardly any difference for the developer to call a local vs. remote method. The lack of understanding of what is really going on under the remoting-hood makes people forget about things like latency, serialization, network traffic and memory usage that come with every remoting call. The easy way of using these technologies results in too many calls across these remoting boundaries and in the end causes performance and scalability problems.

Further Reading: Performance Considerations in Distributed Applications

#4: Wrong usage of O/R-Mappers

Object-Relational Mappers take a big burden off developers’ shoulders – loading and persisting objects in the database. As with any framework there usually are many configuration options to optimize the usage of the O/R Mapper for current application use cases. Faulty settings and incorrect usage of the framework itself too often results in unexpected performance and scalability problems within these frameworks. Make sure you make yourself familiar with all options and learn about the internals of these libraries that you rely on.

Further Reads: Understanding Hibernate Session Cache, Understanding the Query Cache, Understanding the Second Level Cache

#5: Memory Leaks

Managed runtime environments such as Java and .NET have the advantage of helping with memory management by offering Garbage Collectors. A GC, however, does not prevent memory leaks. Objects that are “forgotten” will stick around in memory and ultimately lead to a memory leak that may cause an OutOfMemoryException. It is important to release object references as soon as they are no longer needed.

Further Read: Understanding and finding Memory Leaks

#6: Problematic 3rd Party Code/Components

Nobody is writing all of the functionality of a new application on their own. We use existing 3rd party libraries to speed up our development process. Not only do we speed up our output – but we also increase performance risks introduced by these components. Even though most frameworks are well documented and have been thoroughly tested, there is no guarantee that these frameworks run as expected in every use case they are included. 3rd party code is often used incorrectly or in ways that have not been tested. It is therefore important to make an in-depth check of every framework before introducing it into your code.

Further Read: Top SharePoint Performance Mistakes

#7: Wasteful handling of scarce resources

Resources such as memory, CPU, I/O or the database are scarce. Wasteful handling of these resources results in lack of access to these resources by others and ultimately leads to performance and scalability issues. A good example: database connections that are kept open for too long. Connections must only be used for the time period they are really needed, e.g.: for a query – and then returned to the connection pool. We often see that connections are requested early on in the request handler and are not released until the very end which leads to a classic bottleneck situation.

Further Read: Resource Leak detection in .NET Applications

#8: Bloated web frontends

Thanks to high-speed web access many users have a better end-user experience in the World Wide Web. The downside of this trend is that many applications get packed with too much stuff – they become bloated – which ultimately leads to bad browsing behavior. Particularly users that do not yet have high-speed internet access suffer the most. Too many images that are too large; failure to use or incorrect usage of the browser cache; or overly-aggressive usage of JavaScript/AJAX – all result in performance problems in the browser. Following the existing Best Practices on Web Site Performance Optimization can solve most of these problems:

Further Read: How Better Caching would help speed up Frankfurt Airport Web Site

#9: Wrong Cache Strategy leads to excessive Garbage Collection

Caching objects in memory to avoid constant roundtrips to the database is one way to boost performance. Caching too many objects – or objects that are hardly ever used quickly changes the advantage of caching into a disadvantage due to higher memory usage and increased GC activity. Before implementing a caching strategy you have to figure out which objects to cache and which objects not to cache in order to avoid these types of performance and scalability problems:

Further Reads: Java Memory Problems, Identify GC Bottlenecks in Distributed Applications

#10: Intermittent Problems

Intermittent problems are hard to find. These are usually problems that occur with specific input parameters or only happen under certain load conditions. Full test coverage – functional as well as load and performance coverage – will uncover most of these problems early on before they become real problems for real users.

Further Reads: Tracing Intermittent Errors by Lucy Monahan from Novell, How to find invisible performance problems

(Bonus Problem) #11: Expensive Serialization

With remoting communication – such as Web Services, RMI or WCF – objects need to serialized by the caller in order to be transferred over the network. The callee on the other side needs to de-serialize the object before it can be used. Transformation therefore happens on both sides of the call resulting in some overhead while doing so. It is important to understand what type of serialization is required by both ends and what the optimal choice of serialization and transport type is. Different types of serialization have a different impact on performance, scalability, memory usage and network traffic.

Further Read: Performance Considerations in Distributed Applications

· ·

We just finished launching our latest product NZ1.us, its a new News Site. Check it out, and please, feedback is always welcome (good or bad)

Best,

Blue74 Team

No tags

First of all, I should say that we used 1and1.com for a long time, nearly 5 years. We had nothing but great service, and pretty good pricing. We were using their Linux developer hosting package, shared hosting. The deals and prices they offer, are all but amazing, but the quality of service has declined over the last few years.

Last year, we needed a more customizable solution (memcache/apc/mongodb), and realized that the solution was to take on a VPS (virtual private server). Seeing as we had great service, and no slow downs, we decided on 1and1 and took the VPS III (4gb burst-able ram, 2tb space). We received the VPS very quick, everything seemed to be going well… I would get several performance alerts a day… we had nothing but problems, however I could not track down exactly what was failing. I went through logs, setup custom alerts with Nagios, hired a few people to review our code. I started recording cpu, memory and disk usage using a custom cron tool I wrote in perl. I had enough ammunition to approach them with data.

What I gave them was 25 isolated incidents, all having the same properties- < 3% cpu use, low memory usage, and virtually no disk I/O. These periods of time went anywhere from 1-2 minutes, to 10-15 minutes. We were thinking that another VPS was using a lot of resources… which in the end was the case. When talking to a really cool guy from 1and1 support (I will leave the persons name out) he suggested that the system we were on had more than the intended amount of VPS servers. He had us moved to another node. Everything seemed to be going well, for about 3 weeks. Turns out, the server we were on was oversold again. We could have called support again to be moved, or perhaps spent the extra money on a non-VPS, however we immediately started looking for someone else as we didn’t NEED our own server, nor could we budget it in on our start-up. There is nothing more annoying than typing ion a website, and waiting, for no reason, other than your server was oversold.

We looked around, and found a few candidates. The list we came up with was Amazon’s EC2, Slicehost, Rackspace, and a few others. After reviewing all of the specifications, and options, Rackspace Cloud won. The ease of upgrading to a larger system, creating servers on-the-fly, the price, the iPhone management and monitoring, as well as the CDN services they offer.

We found Rackspace Cloud! We pay around $20.00 a month (plans start around $12.00/month), the same as the original Developer package we had at 1and1, except we are getting VPS for the same price, except, we are able to scale our services with the cloud APIs. We have had nothing but good service, and have not yet called their support lines as everything has been working as expected. If I need a server for a short time, or even long-term, I create a new server in about 2-3 minutes (which can be done right from my iPhone via their application).

What I learned trying to leave 1and1.com… domain transfers, and the entire cancellation process is brutal. I spent about an hour looking for the cancel button, I ended up looking on google to find that I was not alone. They do not provide the correct means of domain transfer, and make you wait 5 days before they will hand over the domain, and in that period, they reset your DNS server names to point to their advertising pages. They require that you visit cancel.1and1.com, there is no link or mention of it, unless you dig. I have often found, the best providers of a service, have an easy way to cancel your service.

TLDR: Use a service like GoDaddy for your domain names- never use your web hosting provider. Check out Rackspace Cloud, its cheap, reliable, and it can grow with your site.

If you have any questions about my experience, or comments with your experiences with any other providers, leave a comment!

· · · · · · · ·

Our payment system is implemented, and we are now accepting payments for our API on algXchange.com. And yes, We’re still giving away free full access API keys to bloggers, interested?!

Our website design is still being worked on, and we are adding new featured daily!

· · ·

Theme Design by devolux.nh2.me